[refactor][utils] Replace thread-unsafe strerror() with C++ standard error message API#1753
Conversation
strerror() is not thread-safe and is a C API. Use the C++ standard library equivalent std::generic_category().message(errno) which is thread-safe and provides the same error description. Changed in: - container_cfg_builder.cpp: realpath and mount error messages - uab/loader main.cpp: fork() and waitpid() error messages Signed-off-by: zhaohaihzb <zhaohaihzb@126.com>
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: zhaohai666 The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
|
Hi @zhaohai666. Thanks for your PR. I'm waiting for a OpenAtom-Linyaps member to verify that this patch is reasonable to test. If it is, they should reply with Once the patch is verified, the new status will be reflected by the I understand the commands that are listed here. DetailsInstructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
There was a problem hiding this comment.
Code Review
This pull request replaces the usage of strerror(errno) with std::generic_category().message(errno) for error reporting across several system calls, including fork, waitpid, realpath, and mount. The reviewer feedback suggests that std::system_category() is conceptually more appropriate than std::generic_category() for these operating system-level errors, as it is specifically designed to handle and map system-specific error codes correctly.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| auto pid = fork(); | ||
| if (pid < 0) { | ||
| std::cerr << "fork() err: " << ::strerror(errno) << std::endl; | ||
| std::cerr << "fork() err: " << std::generic_category().message(errno) << std::endl; |
There was a problem hiding this comment.
For system call errors (such as those from fork()), std::system_category() is conceptually more appropriate than std::generic_category(). std::system_category() is specifically designed for errors reported by the operating system, whereas std::generic_category() is for portable, generic error codes (equivalent to std::errc). Using std::system_category() ensures that any system-specific error codes are correctly mapped to their corresponding error messages.
| std::cerr << "fork() err: " << std::generic_category().message(errno) << std::endl; | |
| std::cerr << "fork() err: " << std::system_category().message(errno) << std::endl; |
| int wstatus{ 0 }; | ||
| if (auto ret = ::waitpid(pid, &wstatus, 0); ret == -1) { | ||
| std::cerr << "waitpid() err:" << ::strerror(errno) << std::endl; | ||
| std::cerr << "waitpid() err:" << std::generic_category().message(errno) << std::endl; |
There was a problem hiding this comment.
For system call errors (such as those from waitpid()), std::system_category() is conceptually more appropriate than std::generic_category(). std::system_category() is specifically designed for errors reported by the operating system, whereas std::generic_category() is for portable, generic error codes (equivalent to std::errc). Using std::system_category() ensures that any system-specific error codes are correctly mapped to their corresponding error messages.
| std::cerr << "waitpid() err:" << std::generic_category().message(errno) << std::endl; | |
| std::cerr << "waitpid() err:" << std::system_category().message(errno) << std::endl; |
| return LINGLONG_ERR(fmt::format("Failed to read symlink {}: {}", | ||
| resolvConf, | ||
| std::generic_category().message(errno))); |
There was a problem hiding this comment.
For system call errors (such as those from realpath()), std::system_category() is conceptually more appropriate than std::generic_category(). std::system_category() is specifically designed for errors reported by the operating system, whereas std::generic_category() is for portable, generic error codes (equivalent to std::errc). Using std::system_category() ensures that any system-specific error codes are correctly mapped to their corresponding error messages.
return LINGLONG_ERR(fmt::format("Failed to read symlink {}: {}",
resolvConf,
std::system_category().message(errno)));| return LINGLONG_ERR(fmt::format("failed to mount {}: {}", | ||
| mount.source.value(), | ||
| std::generic_category().message(errno))); |
There was a problem hiding this comment.
For system call errors (such as those from ::mount()), std::system_category() is conceptually more appropriate than std::generic_category(). std::system_category() is specifically designed for errors reported by the operating system, whereas std::generic_category() is for portable, generic error codes (equivalent to std::errc). Using std::system_category() ensures that any system-specific error codes are correctly mapped to their corresponding error messages.
return LINGLONG_ERR(fmt::format("failed to mount {}: {}",
mount.source.value(),
std::system_category().message(errno)));4573678 to
d1a008c
Compare
Summary
The C
strerror()function for system error reporting violates C++ idioms and is not thread-safe under multi-threaded environments. This PR refactors all relevant error message generation to use standard C++ error utilities.Changes
apps/uab/loader/src/main.cppSubstitute
::strerror(errno)withstd::generic_category().message(errno)in error logging paths offork()andwaitpid().libs/oci-cfg-generators/src/linglong/oci-cfg-generators/container_cfg_builder.cppReplace
strerror(errno)withstd::generic_category().message(errno)for error reporting ofreadlink()andmount()invocations.Modified Files
apps/uab/loader/src/main.cpplibs/oci-cfg-generators/src/linglong/oci-cfg-generators/container_cfg_builder.cppAdvantages
strerror()