From e57d6e024cd4e56fdfaf5d297c79740daf7fdde3 Mon Sep 17 00:00:00 2001 From: obligaron Date: Tue, 14 Jan 2025 21:18:17 +0100 Subject: [PATCH] [c] Optimise error handling for empty datatable rows --- CHANGELOG.md | 1 + c/src/gherkin_line.c | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d739a358b..cc9fcdfa5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt - [.NET] Improved parsing time - [.NET] Use string-ordinal comparison consistently and remove old Mono workaround - [.NET] Improved startup time +- [c] Optimise error handling for empty datatable rows ### Changed - [cpp] add generic support for ABI versioning with VERSION ([#328](https://github.com/cucumber/gherkin/pull/328)) diff --git a/c/src/gherkin_line.c b/c/src/gherkin_line.c index c47f6c0fb..fb2bebc05 100644 --- a/c/src/gherkin_line.c +++ b/c/src/gherkin_line.c @@ -76,17 +76,22 @@ wchar_t* GherkinLine_copy_line_text(const GherkinLine* line, int indent_to_remov const Items* GherkinLine_table_cells(const GherkinLine* line) { int item_count = calculate_cell_count(line->trimmed_line); - if (item_count == 0) - return (Items*)0; + Items* item_array = (Items*)malloc(sizeof(Items)); + item_array->count = item_count; + + if (item_count == 0) { + item_array->items = 0; + return item_array; + } + Span* items = (Span*)malloc(item_count * sizeof(Span)); int i; for (i = 0; i < item_count; ++i) { items[i].column = 0; items[i].text = 0; } - Items* item_array = (Items*)malloc(sizeof(Items)); - item_array->count = item_count; item_array->items = items; + const wchar_t* current_pos = line->trimmed_line; for (i = 0; i < item_count; ++i) { current_pos = populate_cell_data(&items[i], current_pos, current_pos - line->line_text);