philips-software / philips-software/amp-cucumber-cpp-runner

CoPilot comment: Fix dangling `string_view` in `TreeRegexp` copy constructor/assignment

Open Beginner friendly
#341 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C++
Stars
22
Forks
7
Avg merge
2d 5h
Merged PRs (30d)
4

Description

GroupBuilder stores a std::string_view pattern that references the originating TreeRegexp::storedPattern. The current copy constructor and copy-assignment operator shallow-copy rootGroupBuilder, which means the new object's group builder tree still points into other.storedPattern rather than into this->storedPattern.

If other is destroyed (or its storedPattern is moved/mutated via copy-assignment) before the copy is used, the string_views dangle.

Fix: Rebuild the group tree from the freshly stored pattern in both the copy constructor and copy-assignment operator:

TreeRegexp::TreeRegexp(const TreeRegexp& other)
    : storedPattern{ other.storedPattern }
    , regexStrategy{ CreateRegexStrategy(storedPattern) }
    , rootGroupBuilder{ CreateGroupBuilder(storedPattern) }  // rebuild from this->storedPattern
{}

TreeRegexp& TreeRegexp::operator=(const TreeRegexp& other)
{
    storedPattern = other.storedPattern;
    regexStrategy = CreateRegexStrategy(storedPattern);
    rootGroupBuilder = CreateGroupBuilder(storedPattern);  // rebuild from this->storedPattern
    return *this;
}

This mirrors what the primary constructor does and ensures all string_views point into this->storedPattern.

File: cucumber_cpp/library/cucumber_expression/TreeRegexp.cpp (lines ~191–207)

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in cucumber_cpp/library/cucumber_expression/TreeRegexp.cpp around lines 191–207 and compare the copy constructor and assignment operator with the primary constructor. Confirm that both rebuild the group tree from the copied object's stored pattern, then verify copied and assigned TreeRegexp instances remain valid after the source is destroyed or changed.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
75/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.