Skip to content

gherkin C: Segfault when one placeholder is a prefix of another #730

Open
@wintonpc

Description

@wintonpc

Summary

gherkinexe segfaults when processing a feature file that has a scenario outline where

  1. one placeholder is a prefix of the other, and
  2. the prefix column appears to the left of the longer-named column in the example table.

Possible Solution

diff --git a/gherkin/c/src/compiler.c b/gherkin/c/src/compiler.c
index e7011310..7b9ec51a 100644
--- a/gherkin/c/src/compiler.c
+++ b/gherkin/c/src/compiler.c
@@ -283,10 +283,14 @@ static const wchar_t* create_expanded_text(const wchar_t* original_text, const T
     int i;
     for (i = 0; i < length; ++i) {
         if (original_text[i] == L'<') {
-            int j;
+            int j, closer;
+            for (closer = i + 1; closer < length; ++closer)
+                if (original_text[closer] == L'>')
+                    break;
+
             for (j = 0; j < example_header->table_cells->cell_count; ++j) {
                 int cell_text_length = wcslen(example_header->table_cells->table_cells[j].value);
-                if (cell_text_length < length - i - 1 &&
+                if (cell_text_length == closer - i - 1 &&
                         wcsncmp(original_text + i + 1, example_header->table_cells->table_cells[j].value, cell_text_length) == 0) {
                     ReplacementItem* item = (ReplacementItem*)malloc(sizeof(ReplacementItem));
                     item->item_delete = (item_delete_function)ReplacementItem_delete;

Steps to Reproduce

Run gherkinexe on the following feature

Feature: feature

  Scenario Outline: scenario
    Given the <parentflag>

    Examples:

      | parent | parentflag |
      | v      | v          |

Environment

  • Version used: 12f46e8
  • Operating System and version: Xubuntu 18.10

FWIW, I had to tweak CMakeLists.txt to link with -lm get it to compile in the first place.

diff --git a/gherkin/c/CMakeLists.txt b/gherkin/c/CMakeLists.txt
index 077bfecb..429c3304 100644
--- a/gherkin/c/CMakeLists.txt
+++ b/gherkin/c/CMakeLists.txt
@@ -70,11 +70,11 @@ target_include_directories(gherkin PUBLIC
     "$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include;${CMAKE_CURRENT_SOURCE_DIR}/src>")
 
 add_executable(gherkinexe ${GHERKIN_CLI})
-target_link_libraries(gherkinexe gherkin)
+target_link_libraries(gherkinexe gherkin m)
 target_include_directories(gherkinexe PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include ${CMAKE_CURRENT_SOURCE_DIR}/src)
 
 add_executable(gherkin_generate_tokens ${GENERATE_TOKEN})
-target_link_libraries(gherkin_generate_tokens gherkin)
+target_link_libraries(gherkin_generate_tokens gherkin m)
 
 file(GLOB GOOD_FEATURE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/testdata/good/*.feature

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions