Skip to content

Commit 3070ba8

Browse files
committedMar 10, 2025
reallocate memory if reach capacity.
1 parent 829fe6b commit 3070ba8

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed
 

‎csv.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ static int _push_entry(arena_t* arena,
4141
const char* const source,
4242
size_t length) {
4343

44+
if (source == NULL) {
45+
csv->rows.content[csv->rows.count++] = NULL;
46+
return 0;
47+
}
48+
49+
if (csv->rows.count + 1 >= csv->rows.capacity) {
50+
size_t capacity = csv->rows.capacity * 2;
51+
csv->rows.content = realloc(csv->rows.content, sizeof(char*) * capacity * csv->column_count);
52+
csv->rows.capacity = capacity;
53+
}
54+
4455
csv->rows.content[csv->rows.count++] =
4556
arena_string_with_null(arena, source, length + 1);
4657

@@ -168,14 +179,14 @@ int csv_parse_custom_delimiter(arena_t* arena,
168179
} break;
169180
case CSV_PARSER_EOL: {
170181
if (expecting_value) {
171-
csv->rows.content[csv->rows.count++] = NULL;
182+
_push_entry(arena, csv, NULL, 0);
172183
}
173184
csv->row_count += *(position + item_length) != 0;
174185
expecting_value = true;
175186
} break;
176187
case CSV_PARSER_SEPARATOR: {
177188
if (expecting_value) {
178-
csv->rows.content[csv->rows.count++] = NULL;
189+
_push_entry(arena, csv, NULL, 0);
179190
} else {
180191
expecting_value = true;
181192
}

0 commit comments

Comments
 (0)
Please sign in to comment.