Skip to content

Commit

Permalink
Don't accept strings with '\0' for C operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
floitsch committed Oct 24, 2024
1 parent bfb1b64 commit fe3165e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
3 changes: 3 additions & 0 deletions src/primitive.h
Original file line number Diff line number Diff line change
Expand Up @@ -906,6 +906,9 @@ namespace toit {
if (_raw_##name != process->null_object()) { \
Blob _blob_##name; \
if (!_raw_##name->byte_content(process->program(), &_blob_##name, STRINGS_ONLY)) FAIL(WRONG_OBJECT_TYPE); \
if (memchr(_blob_##name.address(), '\0', _blob_##name.length()) != null) { \
FAIL(INVALID_ARGUMENT); \
} \
_nonconst_##name = unvoid_cast<char*>(calloc(_blob_##name.length() + 1, 1)); \
if (!_nonconst_##name) FAIL(MALLOC_FAILED); \
memcpy(_nonconst_##name, _blob_##name.address(), _blob_##name.length()); \
Expand Down
16 changes: 12 additions & 4 deletions src/primitive_core.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,23 @@ namespace toit {
MODULE_IMPLEMENTATION(core, MODULE_CORE)

PRIMITIVE(write_string_on_stdout) {
ARGS(cstring, message, bool, add_newline);
fprintf(stdout, "%s%s", message, add_newline ? "\n" : "");
ARGS(Blob, message, bool, add_newline);
auto bytes = message.address();
for (int i = 0; i < message.length(); i++) {
putchar(bytes[i]);
}
if (add_newline) putchar('\n');
fflush(stdout);
return process->null_object();
}

PRIMITIVE(write_string_on_stderr) {
ARGS(cstring, message, bool, add_newline);
fprintf(stderr, "%s%s", message, add_newline ? "\n" : "");
ARGS(Blob, message, bool, add_newline);
auto bytes = message.address();
for (int i = 0; i < message.length(); i++) {
putchar(bytes[i]);
}
if (add_newline) putchar('\n');
fflush(stderr);
return process->null_object();
}
Expand Down

0 comments on commit fe3165e

Please sign in to comment.