Skip to content

Commit

Permalink
attempt to fix UB
Browse files Browse the repository at this point in the history
  • Loading branch information
yangchi committed Sep 10, 2024
1 parent 088363a commit af683f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
5 changes: 5 additions & 0 deletions kotlin/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -512,8 +512,13 @@ void tree_sitter_kotlin_external_scanner_destroy(void *payload) {
ts_free(stack);
}

// Modified by Poolside fork to fix an UB
unsigned tree_sitter_kotlin_external_scanner_serialize(void *payload, char *buffer) {
Stack *stack = (Stack *)payload;
// Poolside: in memcpy if src is nullptr it is a UB even if size is 0
if (stack->size == 0) {
return 0;
}
memcpy(buffer, stack->contents, stack->size);
return stack->size;
}
Expand Down
12 changes: 8 additions & 4 deletions scala/scanner.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,27 @@ void tree_sitter_scala_external_scanner_destroy(void *payload) {
ts_free(scanner);
}

// Modified by Poolside fork to fix an UB
unsigned tree_sitter_scala_external_scanner_serialize(void *payload, char *buffer) {
if (!payload) {
return 0;
}
Scanner *scanner = (Scanner*)payload;

if ((scanner->indents.size + 3) * sizeof(int16_t) > TREE_SITTER_SERIALIZATION_BUFFER_SIZE) {
return 0;
}

size_t size = 0;
*(int16_t *)&buffer[size] = scanner->last_indentation_size;
memcpy(buffer + size, &scanner->last_indentation_size, sizeof(int16_t));
size += sizeof(int16_t);
*(int16_t *)&buffer[size] = scanner->last_newline_count;
memcpy(buffer + size, &scanner->last_newline_count, sizeof(int16_t));
size += sizeof(int16_t);
*(int16_t *)&buffer[size] = scanner->last_column;
memcpy(buffer + size, &scanner->last_column, sizeof(int16_t));
size += sizeof(int16_t);

for (unsigned i = 0; i < scanner->indents.size; i++) {
*(int16_t *)&buffer[size] = scanner->indents.contents[i];
memcpy(buffer + size, &scanner->indents.contents[i], sizeof(int16_t));
size += sizeof(int16_t);
}

Expand Down

0 comments on commit af683f7

Please sign in to comment.