Skip to content

fix: check the length limit before reading in strnlen - #23

Merged
bkueng merged 1 commit into
PX4:mainfrom
Abtektas:Abtektas/strnlen-bounds-check
Nov 20, 2026
Merged

bkueng merged 1 commit into
PX4:mainfrom
Abtektas:Abtektas/strnlen-bounds-check

Conversation

@Abtektas

Copy link
Copy Markdown
Contributor

Problem

ulog_cpp::strnlen() (ulog_cpp/utils.hpp) reads *ptr before it checks len < max_len. ULog strings carry no terminating zero, so a char[N] value that fills its field is read one byte past its end. For an empty char[0] value the backing vector is empty, ptr can be null, and Value::as<std::string>() crashes.

Real PX4 logs contain such empty info messages, for example char[0] perf_top_preflight-26 in pyulog's test/sample_appended.ulg. The ulog_info example exits with a segmentation fault on that file and on test/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 (old strnlen) the new test crashes.
  • examples/ulog_info on pyulog's sample_appended.ulg and sample_appended_multiple.ulg: segmentation fault before, clean run after. The other sample logs run clean before and after.
  • pre-commit run -a and make -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

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
bkueng merged commit ff74c6f into PX4:main Nov 20, 2026
2 checks passed
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.

2 participants