incubator-graphar icon indicating copy to clipboard operation
incubator-graphar copied to clipboard

fix(cpp): Handle npos in PathToDirectory for S3 URIs without query strings

Open shivendra-dev54 opened this issue 9 months ago • 6 comments

Reason for this PR

Fixes #881

PathToDirectory was crashing with a std::out_of_range exception when parsing valid S3 URIs that do not contain a query string (?). This happens because find_last_of('?') returns std::string::npos, which was being passed directly into path.substr().

What changes are included in this PR?

Added an explicit check for std::string::npos in PathToDirectory. When an S3 URI without a query string is processed, it now correctly identifies the entire path as the prefix and sets the suffix to an empty string, safely avoiding the out-of-bounds crash.

Are these changes tested?

Yes, the C++ test suite was compiled and run locally, and all tests pass successfully.

Are there any user-facing changes?

No.

Critical Fix: Fixes a bug that causes a crash (std::out_of_range exception) when parsing valid S3 URIs without query strings.

shivendra-dev54 avatar Feb 28 '26 18:02 shivendra-dev54

@Sober7135 check the code and also the title of the PR

shivendra-dev54 avatar Feb 28 '26 18:02 shivendra-dev54

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 80.67%. Comparing base (b21da29) to head (b5b723b). :warning: Report is 1 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #888      +/-   ##
============================================
+ Coverage     80.60%   80.67%   +0.07%     
  Complexity      615      615              
============================================
  Files            94       94              
  Lines         10709    10709              
  Branches       1055     1055              
============================================
+ Hits           8632     8640       +8     
+ Misses         1837     1829       -8     
  Partials        240      240              
Flag Coverage Δ
cpp 71.05% <100.00%> (+0.15%) :arrow_up:

Flags with carried forward coverage won't be shown. Click here to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov-commenter avatar Feb 28 '26 18:02 codecov-commenter

Yes, the C++ test suite was compiled and run locally, and all tests pass successfully.

This is not quite accurate, since no test case was added for this bug.

Testing this bug is also somewhat complicated, because PathToDirectory is a static helper inside an anonymous namespace rather than a public API. In practice, we cannot test it directly in the current test suite.

More importantly, this bug is only exercised through GraphInfo::Load(const std::string& path): Load() first reads the graph metadata file, and then calls PathToDirectory(path). So to reproduce this bug in a test, we need to drive the full GraphInfo::Load code path rather than test the helper in isolation. https://github.com/apache/incubator-graphar/blob/d535a437667c6e2bf28f7f376930b13ed5aed63d/cpp/src/graphar/graph_info.cc#L1405-L1416

I can think of two possible solutions:

  • Set up a mock S3 server so that we can exercise the full workflow and reliably trigger this bug.
  • Expose this function — for example, move it into a namespace like graphar::details / graphar::internal, and avoid installing that header into the user system.

Actually, I am not familiar with this kind of problem.

Any advice? CC @yangxk1

Sober7135 avatar Mar 01 '26 17:03 Sober7135

@Sober7135 I have added test as you mentioned in the 2nd possible solution

here is what I did

  • moved the PathToDirectory function to namespace util to expose it for tests.
  • updated util.h with the function declaration.
  • added test as you suggested.
  • added it to the cpp/test/CMakeLists.txt.
  • checked with the new tests.

But I have added that function to graphar::util namespace let me know if you want me to change that

shivendra-dev54 avatar Mar 03 '26 08:03 shivendra-dev54

@yangxk1 I have incorporated all the changes mentioned by the copilot But the python mac build is still failing just like the #906

shivendra-dev54 avatar Mar 10 '26 14:03 shivendra-dev54

@yangxk1 please do review this if it meets the issue expectation or not

shivendra-dev54 avatar Mar 14 '26 06:03 shivendra-dev54