Skip to content

Commit

Permalink
explicitely null-terminate source_code read with fread()
Browse files Browse the repository at this point in the history
  • Loading branch information
flocks committed Jan 5, 2024
1 parent 4284266 commit ceb5590
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ char* readFile(char* file_name) {
exit(EXIT_FAILURE);
}
long size = ftell(f);
char* source_code = (char *)malloc(size+1);
char* source_code = (char *)malloc((size+1) * sizeof(char));
rewind(f);

fread(source_code, 1, size, f);
source_code[size] = '\0';
fclose(f);

return source_code;
Expand All @@ -69,7 +70,7 @@ char* get_node_content(TSNode node, char* source_code) {
uint32_t end_offset = ts_node_end_byte(node);
uint32_t length = end_offset - start_offset;

char *name = (char *)malloc(length + 1);
char *name = (char *)malloc((length + 1) * sizeof(char));
if (name == NULL) {
fprintf(stderr, "Error allocating for name");
exit(EXIT_FAILURE);
Expand Down

0 comments on commit ceb5590

Please sign in to comment.