Don't leak/taint `errno` outside `rcutils_log()` #615
All Vouch review confidence checks passed
Residual risk 0.17 = Change Risk (0.43) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.43 · 391 lines diff across 4 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.357
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #615"
general
weight: 0.35
fujitatomoya
Description
rcutils_log()can changeerrnoeven when logging succeeds. WithRCUTILS_COLORIZED_OUTPUTunset,rcutils_logging_console_output_handler()callsisatty(fileno(stream))for every message to decide on colours. When the output is not a terminal,isatty()returns 0 and setserrnotoENOTTY. That is always the case for processes started byros2 launch, which gives them pipes unlessemulate_ttyis set, and for output redirected to a file or run under systemd.The C standard allows that, but it causes problems in two common situations:
open(), a call toRCUTILS_LOG_ERROR(...)(orRCLCPP_ERROR) followed byreturn errno;hands backENOTTYinstead of the real error, but only when the output isn't a terminal.errnoafter running callbacks. Some libraries readerrnoafter running user callbacks and treat any non-zero value as a failure. We hit this with the Lely CANopen stack: itsev::Loop::run()clearserrno, runs the loop, and throwsstd::system_erroriferrnois set afterwards. A driver callback that logged throughRCLCPP_INFOmade the process abort at shutdown withrun: Inappropriate ioctl for device, but only when started viaros2 launch.This PR saves
errnobefore the output handler is called and restores it afterwards. It's done invrcutils_log_internal(), so it covers bothrcutils_log()andrcutils_log_internal(), and therefore everyRCUTILS_LOG_/RCLCPP_macro, whatever output handler is installed (console, rosout, external file loggers). The header documents the guarantee.Two tests are added in
test/test_logging.cpp:test_log_preserves_errno: a custom output handler setserrno = EBADF. The test checks thatrcutils_log()andrcutils_log_internal()leave both a pendingerrno(EINTR) and a cleared one (0) unchanged.test_console_log_to_pipe_preserves_errno(POSIX only): this is the real-world case. It uses the default console handler with colours on auto and stderr redirected to a pipe, and checks that the message reached the pipe and thaterrnois still 0.Both fail without the change; the pipe test sees
errno == ENOTTY. Both pass with it.Is this user-facing behavior change?
Logging no longer changes
errno. Nothing else changes: the output is the same, and colours are chosen the same way.Did you use Generative AI?
Claude Code (Claude Opus 5.5) was used to investigate the original crash and to draft the change, the tests and this description. All of it was reviewed by the author.
Additional Information
Tested on Ubuntu 24.04 (glibc) against a ROS 2 Lyrical underlay. The existing logging tests still pass:
test_logging,test_logging_console_output_handler,test_logging_custom_env,test_logging_custom_env2,test_logging_enable_for,test_logging_bad_env,test_logging_long_messages.ament_uncrustifyandament_cpplintare clean on the changed files. The gmock-based tests andtest_logging_output_formatwere not run locally, so CI will cover them.