Repository navigation
fix: check the length limit before reading in strnlen - #23
Merged
Merged
Conversation
strnlen() read *ptr before it checked len < max_len. ULog strings carry no terminating zero, so a char[N] value that fills its field was read one byte past its end. For an empty char[0] value the backing vector is empty and ptr can be null, so reading a string value crashed. PX4 logs contain such empty info messages, and the ulog_info example crashed on them. Check the limit first. Add a test that reads back an empty and a full-length info string. Assisted-by: Claude:claude-opus-5-5
bkueng
approved these changes
Nov 20, 2026
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.
Problem
ulog_cpp::strnlen()(ulog_cpp/utils.hpp) reads*ptrbefore it checkslen < max_len. ULog strings carry no terminating zero, so achar[N]value that fills its field is read one byte past its end. For an emptychar[0]value the backing vector is empty,ptrcan be null, andValue::as<std::string>()crashes.Real PX4 logs contain such empty info messages, for example
char[0] perf_top_preflight-26in pyulog'stest/sample_appended.ulg. Theulog_infoexample exits with a segmentation fault on that file and ontest/sample_appended_multiple.ulg.Solution
Check the length limit first, then read the byte. The new test writes an empty and a full-length info string and reads both back.
Testing
On ubuntu:26.04 (linux/amd64, GCC 15.2.0, CMake 4.4.5, as on the CI runner):
make -C build run-unit-tests: 9 test cases pass. With only the test applied (oldstrnlen) the new test crashes.examples/ulog_infoon pyulog'ssample_appended.ulgandsample_appended_multiple.ulg: segmentation fault before, clean run after. The other sample logs run clean before and after.pre-commit run -aandmake -C build clang-tidy(clang-tidy 20.1.8 and 21.1.8) pass.With AddressSanitizer (macOS, Apple clang 21): the new test reports a SEGV on address 0 without the fix and runs clean with it.
Assisted-by: Claude:claude-opus-5-5