Repository navigation
engine+graph: thread-safe last_error + JSON option-parse hardening - #702
Merged
Merged
Conversation
Make last_error_message thread_local (utils.h declaration, cactus_graph.h declaration, last_error.cpp definition must match for ODR) so concurrent FFI calls don't corrupt each other's error string. utils.h: add try_parse_json_uint to reject negative/overflowing size fields (top_k, max_tokens, tool_rag_top_k, cloud_timeout_ms) that std::stoul would wrap. Signed-off-by: Noah Cylich <noahcylich@gmail.com>
Collaborator
|
LGTM @jakmro @HenryNdubuaku |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR aims to make error reporting safer under concurrency by moving the shared last_error_message state to thread-local storage, and to harden JSON option parsing so numeric fields can’t be silently mis-parsed via negative/overflowing inputs.
Changes:
- Make
last_error_messagethread_localin both declarations and the definition to avoid cross-thread clobbering in concurrent FFI usage. - Introduce
try_parse_json_uintand switch selected option fields (top_k,max_tokens,tool_rag_top_k,cloud_timeout_ms) to use it instead ofstd::stoul.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| cactus-graph/src/last_error.cpp | Defines last_error_message as thread_local to isolate per-thread error state. |
| cactus-graph/cactus_graph.h | Updates the public declaration of last_error_message to match the TLS definition. |
| cactus-engine/src/utils.h | Updates TLS declaration and adds hardened parsing for several unsigned numeric JSON options. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+1059
to
+1078
| inline bool try_parse_json_uint(const std::string& json, const std::string& key, size_t& out_value) { | ||
| std::string pattern = "\"" + key + "\":"; | ||
| size_t pos = json.find(pattern); | ||
| if (pos == std::string::npos) return false; | ||
|
|
||
| size_t start = pos + pattern.size(); | ||
| while (start < json.size() && std::isspace(static_cast<unsigned char>(json[start]))) ++start; | ||
|
|
||
| size_t end = start; | ||
| while (end < json.size() && std::string(",}] \t\n\r").find(json[end]) == std::string::npos) ++end; | ||
|
|
||
| try { | ||
| long long parsed = std::stoll(json.substr(start, end - start)); | ||
| if (parsed < 0) return false; | ||
| out_value = static_cast<size_t>(parsed); | ||
| return true; | ||
| } catch (...) { | ||
| return false; | ||
| } | ||
| } |
Comment on lines
+1419
to
1422
| size_t parsed_top_k = options.top_k; | ||
| if (try_parse_json_uint(json, "top_k", parsed_top_k)) { | ||
| options.top_k = parsed_top_k; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Make last_error_message thread_local (utils.h declaration, cactus_graph.h declaration, last_error.cpp definition must match for ODR) so concurrent FFI calls don't corrupt each other's error string. utils.h: add try_parse_json_uint to reject negative/overflowing size fields (top_k, max_tokens, tool_rag_top_k, cloud_timeout_ms) that std::stoul would wrap.
Signed-off-by: Noah Cylich noahcylich@gmail.com