Skip to content

fix: guard optional accesses and numeric parse calls against absent/malformed values#116

Open
SoundMatt wants to merge 1 commit into
COVESA:mainfrom
SoundMatt:fix/optional-and-parse-guards
Open

fix: guard optional accesses and numeric parse calls against absent/malformed values#116
SoundMatt wants to merge 1 commit into
COVESA:mainfrom
SoundMatt:fix/optional-and-parse-guards

Conversation

@SoundMatt
Copy link
Copy Markdown

Problem

Four sites in triple_assembler.cpp and helper.cpp call .value() on optionals without checking .has_value(), or pass WebSocket-derived strings to std::stod/std::stoi without catching conversion errors. Any absent value or non-numeric string from the WebSocket causes std::bad_optional_access or an unhandled std::invalid_argument/std::out_of_range exception.

Root cause

The code assumes that node values and coordinate strings are always present and always numeric. WebSocket messages are external, untrusted input and can legitimately omit values or contain non-numeric data.

Fix

(a) Guard node.getValue().value() in generateTriplesFromNode with a has_value() check and throw std::runtime_error if absent. (b) Guard the latitude/longitude getValue().value() calls in generateTriplesFromCoordinates the same way. (c) Wrap std::stod in getCoordInNtm in try/catch, returning std::nullopt on failure. (d) Wrap std::stoi in parseISO8601ToTime in try/catch, assigning std::nullopt to milliseconds on failure.

…alformed values

Four related issues where WebSocket-derived values could be absent or
non-numeric: (a) node.getValue().value() in generateTriplesFromNode now
checks has_value() first; (b) coordinate getValue().value() calls in
generateTriplesFromCoordinates are guarded similarly; (c) std::stod in
getCoordInNtm is wrapped in try/catch returning std::nullopt on failure;
(d) std::stoi in parseISO8601ToTime is wrapped in try/catch assigning
std::nullopt to milliseconds on failure.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>

Signed-off-by: Matt Jones <47545907+SoundMatt@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant