diff options
author | Håvard Pettersen <havardpe@yahooinc.com> | 2022-06-22 11:11:09 +0000 |
---|---|---|
committer | Håvard Pettersen <havardpe@yahooinc.com> | 2022-06-22 13:09:15 +0000 |
commit | 1dd2e8b25d4b093eee6a9899c4da2625b2cdb863 (patch) | |
tree | a0770a1aa4bf18894952d96f9832b67e17299a6a /vespalib | |
parent | 07c6eff98909eb10a3cad8c27acc46edec1198ed (diff) |
fix more undefined behavior
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/vespa/vespalib/stllike/asciistream.cpp | 20 |
1 files changed, 14 insertions, 6 deletions
diff --git a/vespalib/src/vespa/vespalib/stllike/asciistream.cpp b/vespalib/src/vespa/vespalib/stllike/asciistream.cpp index 6b673363d2d..c8ff6f4dd4b 100644 --- a/vespalib/src/vespa/vespalib/stllike/asciistream.cpp +++ b/vespalib/src/vespa/vespalib/stllike/asciistream.cpp @@ -429,22 +429,30 @@ printInt(unsigned long long r, char * tmp, uint8_t i) return i; } +unsigned long long normalize(long long v, bool &negative) { + if (v < 0) { + negative = true; + if (v == std::numeric_limits<long long>::min()) { + return v; + } + return -v; + } + return v; +} + } asciistream & -asciistream::operator << (long long v) +asciistream::operator << (long long v_in) { char tmp[72]; uint8_t i(sizeof(tmp)); bool negative(false); - if (v == 0) { + if (v_in == 0) { tmp[--i] = '0'; } else { - if (v < 0) { - v = -v; - negative = true; - } + unsigned long long v = normalize(v_in, negative); switch (_base) { case 2: i = printInt<2>(v, tmp, i); break; |