-
Notifications
You must be signed in to change notification settings - Fork 865
web: fix whitespace-fragile extract_bool_value in request_handler #10139
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -363,13 +363,32 @@ static double extract_double_value(const std::string& json) | |||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| static bool extract_bool_value(const std::string& json) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| if (json.find("\"value\":true") != std::string::npos) { | ||||||||||||||||||||||||||||
| const std::string needle = "\"value\""; | ||||||||||||||||||||||||||||
| auto pos = json.find(needle); | ||||||||||||||||||||||||||||
| if (pos == std::string::npos) { | ||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| pos = json.find(':', pos + needle.size()); | ||||||||||||||||||||||||||||
| if (pos == std::string::npos) { | ||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| // Skip whitespace after colon | ||||||||||||||||||||||||||||
| pos++; | ||||||||||||||||||||||||||||
| while (pos < json.size() && (json[pos] == ' ' || json[pos] == '\t')) { | ||||||||||||||||||||||||||||
| pos++; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| if (pos < json.size() && json[pos] == 't') { | ||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| if (json.find("\"value\":false") != std::string::npos) { | ||||||||||||||||||||||||||||
| if (pos < json.size() && json[pos] == 'f') { | ||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
Comment on lines
+380
to
+385
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current implementation only checks the first character ('t' or 'f') to determine the boolean value. This is significantly less robust than the previous implementation and could lead to incorrect parsing if the value starts with these letters but isn't a valid JSON boolean (e.g., "value": "total" would be parsed as true). It is better to check for the full literal.
Suggested change
|
||||||||||||||||||||||||||||
| // Fall back to integer interpretation (0/1) | ||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||
| return std::stoi(json.substr(pos)) != 0; | ||||||||||||||||||||||||||||
| } catch (...) { | ||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| return extract_int_or(json, "value", 0) != 0; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| static void writeColorArray(JsonBuilder& builder, | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic for finding the key, locating the colon, and skipping whitespace is identical to the implementation in
extract_intandextract_float_or. Consider extracting this into a common helper function (e.g.,find_value_start) to reduce code duplication and improve maintainability. Note that helper logic should be defined as a free function in a namespace rather than as a local lambda.References