Skip to content

fix: move to_string(ErrorCode) into namespace cpr to avoid UB (#1332) - #1339

Merged
COM8 merged 3 commits into
libcpr:masterfrom
Rajpal14:fix/1332-to-string-ub
Nov 19, 2026
Merged

COM8 merged 3 commits into
libcpr:masterfrom
Rajpal14:fix/1332-to-string-ub

Conversation

@Rajpal14

@Rajpal14 Rajpal14 commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Closes #1332.

The existing to_string overload for cpr::ErrorCode was defined inside namespace std:

namespace std {
inline std::string to_string(const cpr::ErrorCode& code) { ... }
} // namespace std

Adding overloads to namespace std for user-defined types is undefined behavior per the C++ standard ([namespace.std], C++11 §17.6.4.2.1) and was flagged by the cert-dcl58-cpp CERT rule.

Fix

Per review feedback from @COM8, completely removed the namespace std overload and the cert-dcl58-cpp / bugprone-std-namespace-modification suppressions.

Added cpr::to_string(const ErrorCode&) as a free function inside namespace cpr. Callers can call cpr::to_string(error.code) or use Argument-Dependent Lookup (ADL) via using namespace cpr; to_string(error.code);.

Tests

All existing ErrorTests pass. In test/error_tests.cpp, the std::to_string calls were replaced with tests verifying cpr::to_string() and ADL-resolved to_string().

…#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
@COM8
COM8 self-requested a review November 14, 2026 13:07
@COM8 COM8 added this to the CPR 1.16.0 milestone Nov 14, 2026
Comment thread include/cpr/error.h Resolved
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.
@Rajpal14

Copy link
Copy Markdown
Contributor Author

Updated in commit fd216e8: removed the namespace std { to_string(...) } block and the cert-dcl58-cpp NOLINT suppression as suggested. cpr::to_string(const ErrorCode&) in namespace cpr is now the only overload. The two std::to_string(error.code) assertions in test/error_tests.cpp have been removed; cpr::to_string and ADL-based to_string tests remain.

This eliminates the undefined behaviour (cert-dcl58-cpp) while still fixing #1332.

@Rajpal14
Rajpal14 requested a review from COM8 November 14, 2026 13:48
@Rajpal14

Copy link
Copy Markdown
Contributor Author

Hi @COM8, just checking in politely on this. The namespace std overload and cert-dcl58-cpp suppression have been completely removed as requested, and all CI check suites have finished passing. Whenever you have a moment, could you please take another look and merge if everything looks good? Thanks so much!

@Rajpal14

Rajpal14 commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Hi @COM8! Just a gentle follow-up on this PR.

All requested modifications from your previous review have been implemented (removed the
namespace std overload and the cert-dcl58-cpp suppression), and all 45 CI suites are passing green.

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!!

@COM8 COM8 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Rajpal14 thanks!
I do not see the point in EXPECT_EQ(to_string(error.code), "UNSUPPORTED_PROTOCOL"); but I have approved it. It's just a test.

@COM8
COM8 merged commit 7ff2947 into libcpr:master Nov 19, 2026
45 checks passed
@Rajpal14

Copy link
Copy Markdown
Contributor Author

Thank you @COM8 for reviewing and merging the PR! Really appreciate your time and guidance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Overloading std::to_string

2 participants