From c6c72ca7c29590aa432c20c5ea5aebd8ecf3cff5 Mon Sep 17 00:00:00 2001 From: obligaron Date: Wed, 12 Feb 2025 13:52:19 +0100 Subject: [PATCH] Additional error handling edge cases (#356) * Add additional bad tests * [c] Optimise error handling for empty datatable rows * [Perl] Optimise error handling for unclosed DocStrings --- CHANGELOG.md | 2 ++ c/src/gherkin_line.c | 13 +++++++++---- perl/lib/Gherkin/TokenMatcher.pm | 3 +++ .../backslash_at_end_of_line_in_datatable.feature | 5 +++++ ...t_end_of_line_in_datatable.feature.errors.ndjson | 1 + testdata/bad/file_ends_with_open_docstring.feature | 4 ++++ ...e_ends_with_open_docstring.feature.errors.ndjson | 1 + testdata/bad/unexpected_end_of_file.feature | 2 ++ .../unexpected_end_of_file.feature.errors.ndjson | 1 + testdata/bad/unfinished_datatable.feature | 5 +++++ .../bad/unfinished_datatable.feature.errors.ndjson | 1 + 11 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 testdata/bad/backslash_at_end_of_line_in_datatable.feature create mode 100644 testdata/bad/backslash_at_end_of_line_in_datatable.feature.errors.ndjson create mode 100644 testdata/bad/file_ends_with_open_docstring.feature create mode 100644 testdata/bad/file_ends_with_open_docstring.feature.errors.ndjson create mode 100644 testdata/bad/unexpected_end_of_file.feature create mode 100644 testdata/bad/unexpected_end_of_file.feature.errors.ndjson create mode 100644 testdata/bad/unfinished_datatable.feature create mode 100644 testdata/bad/unfinished_datatable.feature.errors.ndjson diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e1ffa11d..b4ac93da0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt ### Fixed - [.NET] Fix NuGet package generation +- [c] Optimise error handling for empty datatable rows +- [Perl] Optimise error handling for unclosed DocStrings ## [31.0.0] - 2025-01-29 ### Added 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); diff --git a/perl/lib/Gherkin/TokenMatcher.pm b/perl/lib/Gherkin/TokenMatcher.pm index f69afa167..d2eac7332 100644 --- a/perl/lib/Gherkin/TokenMatcher.pm +++ b/perl/lib/Gherkin/TokenMatcher.pm @@ -238,6 +238,9 @@ sub match_StepLine { sub match_DocStringSeparator { my ( $self, $token ) = @_; + if ($token->is_eof) { + return 0; + } if ( !$self->_active_doc_string_separator ) { return $self->_match_DocStringSeparator( $token, '"""', 1 ) || $self->_match_DocStringSeparator( $token, '```', 1 ); diff --git a/testdata/bad/backslash_at_end_of_line_in_datatable.feature b/testdata/bad/backslash_at_end_of_line_in_datatable.feature new file mode 100644 index 000000000..671402836 --- /dev/null +++ b/testdata/bad/backslash_at_end_of_line_in_datatable.feature @@ -0,0 +1,5 @@ +Feature: Addition +Scenario: Add two numbers + When I press + | foo | + | bar \ \ No newline at end of file diff --git a/testdata/bad/backslash_at_end_of_line_in_datatable.feature.errors.ndjson b/testdata/bad/backslash_at_end_of_line_in_datatable.feature.errors.ndjson new file mode 100644 index 000000000..5a8e4216b --- /dev/null +++ b/testdata/bad/backslash_at_end_of_line_in_datatable.feature.errors.ndjson @@ -0,0 +1 @@ +{"parseError":{"message":"(5:3): inconsistent cell count within the table","source":{"location":{"column":3,"line":5},"uri":"../testdata/bad/backslash_at_end_of_line_in_datatable.feature"}}} diff --git a/testdata/bad/file_ends_with_open_docstring.feature b/testdata/bad/file_ends_with_open_docstring.feature new file mode 100644 index 000000000..0be1f7b10 --- /dev/null +++ b/testdata/bad/file_ends_with_open_docstring.feature @@ -0,0 +1,4 @@ +Feature: Addition +Scenario: Add two numbers + Given I have added + ``` \ No newline at end of file diff --git a/testdata/bad/file_ends_with_open_docstring.feature.errors.ndjson b/testdata/bad/file_ends_with_open_docstring.feature.errors.ndjson new file mode 100644 index 000000000..4ed66e0ee --- /dev/null +++ b/testdata/bad/file_ends_with_open_docstring.feature.errors.ndjson @@ -0,0 +1 @@ +{"parseError":{"message":"(5:0): unexpected end of file, expected: #DocStringSeparator, #Other","source":{"location":{"line":5},"uri":"../testdata/bad/file_ends_with_open_docstring.feature"}}} diff --git a/testdata/bad/unexpected_end_of_file.feature b/testdata/bad/unexpected_end_of_file.feature new file mode 100644 index 000000000..ddfbafe10 --- /dev/null +++ b/testdata/bad/unexpected_end_of_file.feature @@ -0,0 +1,2 @@ +Feature: Addition +@tag \ No newline at end of file diff --git a/testdata/bad/unexpected_end_of_file.feature.errors.ndjson b/testdata/bad/unexpected_end_of_file.feature.errors.ndjson new file mode 100644 index 000000000..a8139af42 --- /dev/null +++ b/testdata/bad/unexpected_end_of_file.feature.errors.ndjson @@ -0,0 +1 @@ +{"parseError":{"message":"(3:0): unexpected end of file, expected: #TagLine, #RuleLine, #Comment, #Empty","source":{"location":{"line":3},"uri":"../testdata/bad/unexpected_end_of_file.feature"}}} diff --git a/testdata/bad/unfinished_datatable.feature b/testdata/bad/unfinished_datatable.feature new file mode 100644 index 000000000..b0b48841c --- /dev/null +++ b/testdata/bad/unfinished_datatable.feature @@ -0,0 +1,5 @@ +Feature: Addition +Scenario: Add two numbers + When I press + | foo | + | bar \ No newline at end of file diff --git a/testdata/bad/unfinished_datatable.feature.errors.ndjson b/testdata/bad/unfinished_datatable.feature.errors.ndjson new file mode 100644 index 000000000..d200fefc8 --- /dev/null +++ b/testdata/bad/unfinished_datatable.feature.errors.ndjson @@ -0,0 +1 @@ +{"parseError":{"message":"(5:3): inconsistent cell count within the table","source":{"location":{"column":3,"line":5},"uri":"../testdata/bad/unfinished_datatable.feature"}}}