diff --git a/kotlin/scanner.c b/kotlin/scanner.c index 4f531eb..c1faa98 100644 --- a/kotlin/scanner.c +++ b/kotlin/scanner.c @@ -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; } diff --git a/scala/scanner.c b/scala/scanner.c index 58adb7d..c746389 100644 --- a/scala/scanner.c +++ b/scala/scanner.c @@ -51,7 +51,11 @@ 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) { @@ -59,15 +63,15 @@ unsigned tree_sitter_scala_external_scanner_serialize(void *payload, char *buffe } 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); }