From 39d75ae5bffd507135831674976269c15701b2a4 Mon Sep 17 00:00:00 2001 From: Doyle T Date: Sat, 7 Jul 2018 01:10:43 +1000 Subject: [PATCH] Fix DqnFixedString len bug --- DqnFixedString.cpp | 11 ++++++++++- dqn.h | 24 +++++++++++------------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/DqnFixedString.cpp b/DqnFixedString.cpp index c9b1f54..2827b3c 100644 --- a/DqnFixedString.cpp +++ b/DqnFixedString.cpp @@ -34,7 +34,7 @@ void DqnFixedString_Test() DqnFixedString<512> str = DQN_SLICE("hello world"); DQN_ASSERT(str.Sprintf("hello %s", "sailor")); str += DQN_SLICE(".end"); - DQN_ASSERT(DqnStr_Cmp(str.str, "hello sailor.end") == 0); + DQN_ASSERTM(DqnStr_Cmp(str.str, "hello sailor.end") == 0, "Result: %s", str.str); } { @@ -65,4 +65,13 @@ void DqnFixedString_Test() Log(Status::Ok, "Operator +"); } + + { + DqnFixedString<512> str = "localhost"; + str.SprintfAppend(":%d", 16832); + str += "/json_rpc"; + DQN_ASSERT(str.len == 24 && DqnStr_Cmp("localhost:16832/json_rpc", str.str) == 0); + + Log(Status::Ok, "Copy constructor, sprintf, operator +="); + } } diff --git a/dqn.h b/dqn.h index e5feee0..477938e 100644 --- a/dqn.h +++ b/dqn.h @@ -1856,10 +1856,10 @@ struct DqnFixedString DqnFixedString(DqnSlice const &other) { this->len = DqnFixedString__Append(this->str, MAX, other.data, other.len); } DqnFixedString(DqnFixedString const &other) { if (this != &other) this->len = DqnFixedString__Append(this->str, MAX, other.str, other.len); } - DqnFixedString &operator+=(char const *other) { this->len = DqnFixedString__Append(this->str + this->len, MAX - this->len, other); return *this; } - DqnFixedString &operator+=(DqnSlice const &other) { this->len = DqnFixedString__Append(this->str + this->len, MAX - this->len, other.data, other.len); return *this; } - DqnFixedString &operator+=(DqnSlice const &other) { this->len = DqnFixedString__Append(this->str + this->len, MAX - this->len, other.data, other.len); return *this; } - DqnFixedString &operator+=(DqnFixedString const &other) { this->len = DqnFixedString__Append(this->str + this->len, MAX - this->len, other.str, other.len); return *this; } + DqnFixedString &operator+=(char const *other) { this->len += DqnFixedString__Append(this->str + this->len, MAX - this->len, other); return *this; } + DqnFixedString &operator+=(DqnSlice const &other) { this->len += DqnFixedString__Append(this->str + this->len, MAX - this->len, other.data, other.len); return *this; } + DqnFixedString &operator+=(DqnSlice const &other) { this->len += DqnFixedString__Append(this->str + this->len, MAX - this->len, other.data, other.len); return *this; } + DqnFixedString &operator+=(DqnFixedString const &other) { this->len += DqnFixedString__Append(this->str + this->len, MAX - this->len, other.str, other.len); return *this; } DqnFixedString operator+ (char const *other) { DqnFixedString result = *this; result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, other); return result; } DqnFixedString operator+ (DqnSlice const &other) { DqnFixedString result = *this; result.len += DqnFixedString__Append(result.str + result.len, MAX - result.len, other.data, other.len); return result; } @@ -1885,7 +1885,7 @@ DqnFixedString__VSprintf(DqnFixedString *me, char const *fmt, va_list argLi char *bufStart = me->str + bufOffset; i32 const remainingSpace = static_cast((me->str + MAX) - bufStart); int result = Dqn_vsnprintf(bufStart, remainingSpace, fmt, argList); - me->len = result; + me->len = bufOffset + result; return result; } @@ -6215,7 +6215,6 @@ wchar_t *DqnString::ToWChar(DqnMemAPI *api) const // return: The number of bytes written to dest FILE_SCOPE int DqnFixedString__Append(char *dest, int destSize, char const *src, int len) { - i32 realLen = 0; if (len <= -1) { char *ptr = dest; @@ -6229,17 +6228,16 @@ FILE_SCOPE int DqnFixedString__Append(char *dest, int destSize, char const *src, DQN_ASSERT(!src[0]); } - realLen = static_cast(ptr - dest); + len = static_cast(ptr - dest); } else { - realLen = DQN_MIN(len, destSize - 1); - DqnMem_Copy(dest, src, realLen); + DqnMem_Copy(dest, src, len); } - DQN_ASSERT(realLen <= destSize && realLen >= 0); - dest[realLen] = 0; - return realLen; + DQN_ASSERT(len < destSize && len >= 0); + dest[len] = 0; + return len; } // #Dqn @@ -6364,7 +6362,7 @@ TryNext: { if (locate[-1] != '"' || locate[findPropertyLen] != '"') { - bufLen -= ((locate - buf) + findPropertyLen); + bufLen -= static_cast(((locate - buf) + findPropertyLen)); buf = locate + findPropertyLen; goto TryNext; }