fix(cpp): Handle npos in PathToDirectory for S3 URIs without query strings#888
fix(cpp): Handle npos in PathToDirectory for S3 URIs without query strings#888shivendra-dev54 wants to merge 14 commits intoapache:mainfrom
Conversation
|
@Sober7135 |
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This is not quite accurate, since no test case was added for this bug. Testing this bug is also somewhat complicated, because More importantly, this bug is only exercised through incubator-graphar/cpp/src/graphar/graph_info.cc Lines 1405 to 1416 in d535a43 I can think of two possible solutions:
Actually, I am not familiar with this kind of problem. Any advice? CC @yangxk1 |
Signed-off-by: devadhe sb <[email protected]>
|
@Sober7135 here is what I did
But I have added that function to |
There was a problem hiding this comment.
Pull request overview
This PR fixes a crash (std::out_of_range exception) in PathToDirectory when parsing valid S3 URIs that don't contain a query string (?). The function previously passed std::string::npos directly to path.substr(), causing the exception. The fix adds an explicit check for npos and moves the function from a file-local static in graph_info.cc to the graphar::util namespace to improve reusability and testability.
Changes:
- Added a
nposguard inPathToDirectoryso S3 URIs without?are handled safely - Moved
PathToDirectoryfrom anonymous namespace ingraph_info.cctographar::util, declared inutil.h - Added a new test file
test_util.cccovering S3 with/without query strings, local paths, and relative paths
Reviewed changes
Copilot reviewed 4 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
cpp/src/graphar/graph_info.cc |
Removed old static PathToDirectory, added new graphar::util::PathToDirectory with the npos fix, updated call sites |
cpp/src/graphar/util.h |
Added declaration for PathToDirectory in graphar::util |
cpp/test/test_util.cc |
New test file with 5 test cases for PathToDirectory |
cpp/test/CMakeLists.txt |
Registered the new test target |
cpp/CMakeLists.txt |
Trivial trailing newline addition |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
cpp/src/graphar/graph_info.cc
Outdated
| std::string PathToDirectory(const std::string& path) { | ||
| if (path.rfind("s3://", 0) == 0) { | ||
| size_t t = path.find_last_of('?'); | ||
| // Your fix here |
There was a problem hiding this comment.
This is a leftover debug/placeholder comment that should be removed before merging. It doesn't describe the logic and reads like a TODO marker from development.
cpp/src/graphar/graph_info.cc
Outdated
| namespace util { | ||
| std::string PathToDirectory(const std::string& path) { | ||
| if (path.rfind("s3://", 0) == 0) { | ||
| size_t t = path.find_last_of('?'); | ||
| // Your fix here | ||
| std::string prefix = (t == std::string::npos) ? path : path.substr(0, t); | ||
| std::string suffix = (t == std::string::npos) ? "" : path.substr(t); | ||
|
|
||
| const size_t last_slash_idx = prefix.rfind('/'); | ||
| if (std::string::npos != last_slash_idx) { | ||
| return prefix.substr(0, last_slash_idx + 1) + suffix; | ||
| } | ||
| } else { | ||
| const size_t last_slash_idx = path.rfind('/'); | ||
| if (std::string::npos != last_slash_idx) { | ||
| return path.substr(0, last_slash_idx + 1); | ||
| } | ||
| } | ||
| return path; | ||
| } | ||
| } // namespace util |
There was a problem hiding this comment.
The PathToDirectory function definition is placed in graph_info.cc, but it is declared in util.h and the natural implementation file for graphar::util functions is util.cc (which already exists at cpp/src/graphar/util.cc and contains the other graphar::util function definitions). Moving the definition to util.cc would be consistent with the existing codebase organization and avoid mixing unrelated concerns in graph_info.cc.
cpp/test/test_util.cc
Outdated
|
|
||
| namespace graphar { | ||
|
|
||
| TEST_CASE_METHOD(GlobalFixture, "PathUtilTest") { |
There was a problem hiding this comment.
Using GlobalFixture here means these pure string-manipulation tests will throw a std::runtime_error if the GAR_TEST_DATA environment variable is not set, even though these tests don't use any test data files at all. Consider using a plain TEST_CASE instead of TEST_CASE_METHOD(GlobalFixture, ...) so these unit tests can run without that environment dependency.
…t to >=3.9 in README
|
@yangxk1 |
Reason for this PR
Fixes #881
PathToDirectorywas crashing with astd::out_of_rangeexception when parsing valid S3 URIs that do not contain a query string (?). This happens becausefind_last_of('?')returnsstd::string::npos, which was being passed directly intopath.substr().What changes are included in this PR?
Added an explicit check for
std::string::nposinPathToDirectory. 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_rangeexception) when parsing valid S3 URIs without query strings.