Commit 623d90a5 authored by jan.koester's avatar jan.koester
Browse files

<string>: stoi/stol/stoll/stoul/stoull/stof/stod/stold throw as specified

They returned strtoX's clamped value and never threw: no
invalid_argument without digits, no out_of_range on ERANGE (or, for stoi,
outside int). mammut parses integer literals with stoll and falls back to
exact NUMERIC on out_of_range -- on FenrirOS it got LLONG_MAX for a 26-digit
literal and its SQL test failed in the package build ("bigint out of
range").

Now as the standard has them; pos as before, errno left alone on success.
<stdexcept> includes <string> first, so <string> only declares the two
throw helpers (std::__sto) and <stdexcept> defines them after its classes.
New e2e_sto_exceptions; ctest 174/174.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 123c0ddb
Loading
Loading
Loading
Loading
+6 −0
Changes for include/cxx/stdexcept: 6 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -62,4 +62,10 @@ class range_error : public runtime_error { public: explicit range_error(const st
class overflow_error : public runtime_error { public: explicit overflow_error(const string& w) : runtime_error(w) {} };
class underflow_error : public runtime_error { public: explicit underflow_error(const string& w) : runtime_error(w) {} };

/* for std::stoi & co. in <string> (declared there) */
namespace __sto {
[[noreturn]] inline void __throw_invalid_argument(const char* name) { throw invalid_argument(name); }
[[noreturn]] inline void __throw_out_of_range(const char* name) { throw out_of_range(name); }
}

}
+61 −43
Changes for include/cxx/string: 61 added lines, 43 removed lines.
Original line number Diff line number Diff line
@@ -38,6 +38,8 @@
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
/* libstdc++'s <string> also brings in <cstdlib>/<cstdio> (via
 * ext/string_conversions.h, for stoi()/to_string()), so real code calls
@@ -1685,49 +1687,6 @@ inline string to_string(long double v) {
    return s;
}

/* The pos-and-base overloads real callers use for incremental parsing
 * (e.g. walking a comma-separated list of numbers) -- pos, when given,
 * is set to how many characters strtoX() actually consumed. */
inline int stoi(const string& s, size_type* pos = nullptr, int base = 10) {
    char* end; long v = strtol(s.c_str(), &end, base);
    if (pos) *pos = (size_type)(end - s.c_str());
    return (int)v;
}
inline long stol(const string& s, size_type* pos = nullptr, int base = 10) {
    char* end; long v = strtol(s.c_str(), &end, base);
    if (pos) *pos = (size_type)(end - s.c_str());
    return v;
}
inline long long stoll(const string& s, size_type* pos = nullptr, int base = 10) {
    char* end; long long v = strtoll(s.c_str(), &end, base);
    if (pos) *pos = (size_type)(end - s.c_str());
    return v;
}
inline unsigned long stoul(const string& s, size_type* pos = nullptr, int base = 10) {
    char* end; unsigned long v = strtoul(s.c_str(), &end, base);
    if (pos) *pos = (size_type)(end - s.c_str());
    return v;
}
inline unsigned long long stoull(const string& s, size_type* pos = nullptr, int base = 10) {
    char* end; unsigned long long v = strtoull(s.c_str(), &end, base);
    if (pos) *pos = (size_type)(end - s.c_str());
    return v;
}
inline float stof(const string& s, size_type* pos = nullptr) {
    char* end; float v = strtof(s.c_str(), &end);
    if (pos) *pos = (size_type)(end - s.c_str());
    return v;
}
inline double stod(const string& s, size_type* pos = nullptr) {
    char* end; double v = strtod(s.c_str(), &end);
    if (pos) *pos = (size_type)(end - s.c_str());
    return v;
}
inline long double stold(const string& s, size_type* pos = nullptr) {
    char* end; long double v = strtold(s.c_str(), &end);
    if (pos) *pos = (size_type)(end - s.c_str());
    return v;
}

}

@@ -1748,6 +1707,65 @@ inline long double stold(const string& s, size_type* pos = nullptr) {

namespace std __attribute__((__visibility__("default"))) {

/* stoi/stol/stoll/stoul/stoull/stof/stod/stold, as the standard has them:
 * std::invalid_argument when nothing could be converted, std::out_of_range
 * when the value doesn't fit (strtoX's ERANGE, for stoi also outside int).
 * They used to return strtoX's clamped value without a word -- mammut,
 * which falls back to exact NUMERIC on out_of_range, got LLONG_MAX for a
 * 26-digit literal instead. Here, after <stdexcept>: they need its
 * classes. pos, when given, is how many characters were consumed; errno
 * is left as it was unless the conversion fails. */
namespace __sto {
/* defined in <stdexcept>, after its classes: <stdexcept> includes <string>
 * first, so here they can be only declared (the two always come together) */
[[noreturn]] inline void __throw_invalid_argument(const char* name);
[[noreturn]] inline void __throw_out_of_range(const char* name);

template <class R, class F>
inline R __convert(const char* name, F fn, const string& s, size_t* pos) {
    const char* p = s.c_str();
    char* end;
    int saved = errno;
    errno = 0;
    R v = fn(p, &end);
    if (end == p) {
        errno = saved;
        __throw_invalid_argument(name);
    }
    if (errno == ERANGE) __throw_out_of_range(name);
    errno = saved;
    if (pos) *pos = (size_t)(end - p);
    return v;
}
}

inline int stoi(const string& s, size_t* pos = nullptr, int base = 10) {
    long v = __sto::__convert<long>("stoi", [base](const char* p, char** e) { return strtol(p, e, base); }, s, pos);
    if (v < INT_MIN || v > INT_MAX) __sto::__throw_out_of_range("stoi");
    return (int)v;
}
inline long stol(const string& s, size_t* pos = nullptr, int base = 10) {
    return __sto::__convert<long>("stol", [base](const char* p, char** e) { return strtol(p, e, base); }, s, pos);
}
inline long long stoll(const string& s, size_t* pos = nullptr, int base = 10) {
    return __sto::__convert<long long>("stoll", [base](const char* p, char** e) { return strtoll(p, e, base); }, s, pos);
}
inline unsigned long stoul(const string& s, size_t* pos = nullptr, int base = 10) {
    return __sto::__convert<unsigned long>("stoul", [base](const char* p, char** e) { return strtoul(p, e, base); }, s, pos);
}
inline unsigned long long stoull(const string& s, size_t* pos = nullptr, int base = 10) {
    return __sto::__convert<unsigned long long>("stoull", [base](const char* p, char** e) { return strtoull(p, e, base); }, s, pos);
}
inline float stof(const string& s, size_t* pos = nullptr) {
    return __sto::__convert<float>("stof", [](const char* p, char** e) { return strtof(p, e); }, s, pos);
}
inline double stod(const string& s, size_t* pos = nullptr) {
    return __sto::__convert<double>("stod", [](const char* p, char** e) { return strtod(p, e); }, s, pos);
}
inline long double stold(const string& s, size_t* pos = nullptr) {
    return __sto::__convert<long double>("stold", [](const char* p, char** e) { return strtold(p, e); }, s, pos);
}

/* Real std::string user-defined literal ("" s) -- was missing (real
 * gap: smtpclient's securesmtpclientbase.cpp does `using namespace
 * std::literals::string_literals;`). Reopens the same inline
+21 −0
Changes for src/test/e2e/CMakeLists.txt: 21 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -4133,3 +4133,24 @@ target_link_libraries(e2e_execinfo_signalfd PRIVATE
add_dependencies(e2e_execinfo_signalfd crti Scrt1 crtbeginS crtn systempp)

add_test(NAME e2e_execinfo_signalfd COMMAND e2e_execinfo_signalfd)


add_executable(e2e_sto_exceptions sto_exceptions_test.cpp)
set_target_properties(e2e_sto_exceptions PROPERTIES OUTPUT_NAME "sto_exceptions_test")
target_compile_options(e2e_sto_exceptions PRIVATE -fPIE)
target_link_options(e2e_sto_exceptions PRIVATE
    -nostdlib -pie
    -Wl,--hash-style=sysv
    -Wl,--dynamic-linker,$<TARGET_FILE_DIR:systempp>/ld-linux-${CMAKE_SYSTEM_PROCESSOR}.so
    -Wl,-rpath,$<TARGET_FILE_DIR:systempp>
)
target_link_libraries(e2e_sto_exceptions PRIVATE
    $<TARGET_OBJECTS:crti>
    $<TARGET_OBJECTS:Scrt1>
    $<TARGET_OBJECTS:crtbeginS>
    $<TARGET_OBJECTS:crtn>
    systempp
)
add_dependencies(e2e_sto_exceptions crti Scrt1 crtbeginS crtn systempp)

add_test(NAME e2e_sto_exceptions COMMAND e2e_sto_exceptions)
+34 −0
Changes for src/test/e2e/sto_exceptions_test.cpp: 34 added lines, 0 removed lines.
Original line number Diff line number Diff line
/* std::stoi/stol/stoll/stoul/stoull/stof/stod as the standard has them:
 * invalid_argument without digits, out_of_range when the value doesn't
 * fit; pos; errno untouched on success. */
#include <string>
#include <stdexcept>
#include <errno.h>
#include <stdio.h>

#define CHECK(cond, ...) do { if (!(cond)) { printf("FAIL: " __VA_ARGS__); printf("\n"); return 1; } } while (0)

template <class F> static int throws(F f) {   /* 0 none, 1 invalid_argument, 2 out_of_range */
    try { f(); } catch (const std::invalid_argument&) { return 1; } catch (const std::out_of_range&) { return 2; }
    return 0;
}

int main() {
    size_t pos = 0;
    CHECK(std::stoll("  -123abc", &pos) == -123 && pos == 6, "stoll value/pos (pos=%zu)", pos);
    CHECK(std::stoi("0x1f", nullptr, 16) == 31, "stoi base 16");
    CHECK(std::stoul("777", nullptr, 8) == 511, "stoul base 8");
    CHECK(std::stod("2.5e3", &pos) == 2500.0 && pos == 5, "stod");
    CHECK(throws([] { std::stoll("99999999999999999999999999"); }) == 2, "stoll overflow -> out_of_range");
    CHECK(throws([] { std::stoll("-99999999999999999999999999"); }) == 2, "stoll underflow -> out_of_range");
    CHECK(throws([] { std::stoi("3000000000"); }) == 2, "stoi beyond int -> out_of_range");
    CHECK(throws([] { std::stoull("18446744073709551616"); }) == 2, "stoull overflow");
    CHECK(throws([] { std::stod("1e999"); }) == 2, "stod overflow");
    CHECK(throws([] { std::stoi("abc"); }) == 1, "stoi no digits -> invalid_argument");
    CHECK(throws([] { std::stod(""); }) == 1, "stod empty -> invalid_argument");
    errno = 1234;
    (void)std::stol("42");
    CHECK(errno == 1234, "errno changed on success (%d)", errno);
    printf("OK: sto* exceptions\n");
    return 0;
}