Repository navigation
fix: move to_string(ErrorCode) into namespace cpr to avoid UB (#1332) - #1339
Conversation
…#1332) The existing overload of std::to_string inside namespace std for cpr::ErrorCode violates cert-dcl58-cpp (adding overloads to std namespace is undefined behavior per C++11 [namespace.std]). This commit: - Adds cpr::to_string(const ErrorCode&) as a proper free function inside namespace cpr (the idiomatic C++ approach) - Makes the existing namespace std overload delegate to cpr::to_string() for backward compatibility with callers using std::to_string(error.code) - Extends the StringRepr tests to verify cpr::to_string(), ADL to_string(), and std::to_string() all return the correct string Fixes libcpr#1332
Per reviewer suggestion, remove the namespace std overload and the cert-dcl58-cpp NOLINT exception entirely. cpr::to_string(const ErrorCode&) in namespace cpr is the idiomatic approach; ADL lookup (using namespace cpr) already works for callers who want unqualified to_string access. This eliminates the UB (cert-dcl58-cpp) while keeping the fix for libcpr#1332.
|
Updated in commit fd216e8: removed the This eliminates the undefined behaviour (cert-dcl58-cpp) while still fixing #1332. |
|
Hi @COM8, just checking in politely on this. The |
|
Hi @COM8! Just a gentle follow-up on this PR. All requested modifications from your previous review have been implemented (removed the Today is the final submission deadline for the PR component of my university course. If the changes look good to you, would you mind reviewing/approving or merging when you have a moment? I really appreciate your time and help in reviewing this!! |
|
Thank you @COM8 for reviewing and merging the PR! Really appreciate your time and guidance. |
Problem
Closes #1332.
The existing
to_stringoverload forcpr::ErrorCodewas defined insidenamespace std:Adding overloads to
namespace stdfor user-defined types is undefined behavior per the C++ standard ([namespace.std], C++11 §17.6.4.2.1) and was flagged by thecert-dcl58-cppCERT rule.Fix
Per review feedback from @COM8, completely removed the
namespace stdoverload and thecert-dcl58-cpp/bugprone-std-namespace-modificationsuppressions.Added
cpr::to_string(const ErrorCode&)as a free function insidenamespace cpr. Callers can callcpr::to_string(error.code)or use Argument-Dependent Lookup (ADL) viausing namespace cpr; to_string(error.code);.Tests
All existing ErrorTests pass. In
test/error_tests.cpp, thestd::to_stringcalls were replaced with tests verifyingcpr::to_string()and ADL-resolvedto_string().