diff --git a/include/bx/inline/string.inl b/include/bx/inline/string.inl index 7b6f650..0335410 100644 --- a/include/bx/inline/string.inl +++ b/include/bx/inline/string.inl @@ -43,8 +43,8 @@ namespace bx { Ty str = _str; typename Ty::size_type startPos = 0; - const typename Ty::size_type fromLen = strnlen(_from); - const typename Ty::size_type toLen = strnlen(_to); + const typename Ty::size_type fromLen = strLen(_from); + const typename Ty::size_type toLen = strLen(_to); while ( (startPos = str.find(_from, startPos) ) != Ty::npos) { str.replace(startPos, fromLen, _to); @@ -81,7 +81,7 @@ namespace bx if (NULL != _ptr) { - int32_t len = strnlen(_ptr, _len); + int32_t len = strLen(_ptr, _len); if (0 != len) { m_len = len; @@ -167,10 +167,10 @@ namespace bx if (0 != _len) { int32_t old = m_len; - int32_t len = m_len + strnlen(_ptr, _len); + int32_t len = m_len + strLen(_ptr, _len); char* ptr = (char*)BX_REALLOC(*AllocatorT, 0 != m_len ? const_cast(m_ptr) : NULL, len+1); m_len = len; - strlncpy(ptr + old, len-old+1, _ptr, _len); + strCopy(ptr + old, len-old+1, _ptr, _len); *const_cast(&m_ptr) = ptr; } diff --git a/include/bx/string.h b/include/bx/string.h index 440cbe1..c501332 100644 --- a/include/bx/string.h +++ b/include/bx/string.h @@ -133,14 +133,14 @@ namespace bx int32_t strincmp(const char* _lhs, const char* _rhs, int32_t _max = INT32_MAX); /// - int32_t strnlen(const char* _str, int32_t _max = INT32_MAX); + int32_t strLen(const char* _str, int32_t _max = INT32_MAX); /// Copy _num characters from string _src to _dst buffer of maximum _dstSize capacity /// including zero terminator. Copy will be terminated with '\0'. - int32_t strlncpy(char* _dst, int32_t _dstSize, const char* _src, int32_t _num = INT32_MAX); + int32_t strCopy(char* _dst, int32_t _dstSize, const char* _src, int32_t _num = INT32_MAX); /// - int32_t strlncat(char* _dst, int32_t _dstSize, const char* _src, int32_t _num = INT32_MAX); + int32_t strCat(char* _dst, int32_t _dstSize, const char* _src, int32_t _num = INT32_MAX); /// const char* strnchr(const char* _str, char _ch, int32_t _max = INT32_MAX); @@ -215,18 +215,6 @@ namespace bx /// Convert size in bytes to human readable string. void prettify(char* _out, int32_t _count, uint64_t _size); - /// Copy src to string dst of size siz. At most siz-1 characters - /// will be copied. Always NUL terminates (unless siz == 0). - /// Returns strlen(src); if retval >= siz, truncation occurred. - int32_t strlcpy(char* _dst, const char* _src, int32_t _max); - - /// Appends src to string dst of size siz (unlike strncat, siz is the - /// full size of dst, not space left). At most siz-1 characters - /// will be copied. Always NUL terminates (unless siz <= strlen(dst)). - /// Returns strlen(src) + MIN(siz, strlen(initial dst)). - /// If retval >= siz, truncation occurred. - int32_t strlcat(char* _dst, const char* _src, int32_t _max); - /// int32_t toString(char* _out, int32_t _max, double _value); diff --git a/scripts/toolchain.lua b/scripts/toolchain.lua index f196cf4..069d415 100644 --- a/scripts/toolchain.lua +++ b/scripts/toolchain.lua @@ -779,6 +779,7 @@ function toolchain(_buildDir, _libDir) "-msse2", "-Wunused-value", "-Wundef", + "-Wno-strict-overflow", } buildoptions_cpp { "-std=c++11", diff --git a/src/commandline.cpp b/src/commandline.cpp index 6ec2aad..fe2ea19 100644 --- a/src/commandline.cpp +++ b/src/commandline.cpp @@ -270,7 +270,7 @@ namespace bx ++arg; if (_short == *arg) { - if (1 == strnlen(arg) ) + if (1 == strLen(arg) ) { if (0 == _skip) { diff --git a/src/crtnone.cpp b/src/crtnone.cpp index 0e88551..dc7fdc2 100644 --- a/src/crtnone.cpp +++ b/src/crtnone.cpp @@ -39,29 +39,29 @@ extern "C" int32_t memcmp(const void* _lhs, const void* _rhs, size_t _numBytes) extern "C" size_t strlen(const char* _str) { - return bx::strnlen(_str); + return bx::strLen(_str); } -extern "C" size_t strnlen(const char* _str, size_t _max) +extern "C" size_t strLen(const char* _str, size_t _max) { - return bx::strnlen(_str, _max); + return bx::strLen(_str, _max); } extern "C" void* strcpy(char* _dst, const char* _src) { - bx::strlncpy(_dst, INT32_MAX, _src, INT32_MAX); + bx::strCopy(_dst, INT32_MAX, _src, INT32_MAX); return _dst; } extern "C" void* strncpy(char* _dst, const char* _src, size_t _num) { - bx::strlncpy(_dst, INT32_MAX, _src, _num); + bx::strCopy(_dst, INT32_MAX, _src, _num); return _dst; } extern "C" char* strcat(char* _dst, const char* _src) { - bx::strlncat(_dst, INT32_MAX, _src, INT32_MAX); + bx::strCat(_dst, INT32_MAX, _src, INT32_MAX); return _dst; } diff --git a/src/dtoa.cpp b/src/dtoa.cpp index 98640ed..57480ab 100644 --- a/src/dtoa.cpp +++ b/src/dtoa.cpp @@ -436,17 +436,17 @@ namespace bx if (isNan(_value) ) { - return (int32_t)strlncpy(_dst, _max, "nan") + sign; + return (int32_t)strCopy(_dst, _max, "nan") + sign; } else if (isInfinite(_value) ) { - return (int32_t)strlncpy(_dst, _max, "inf") + sign; + return (int32_t)strCopy(_dst, _max, "inf") + sign; } int32_t len; if (0.0 == _value) { - len = (int32_t)strlncpy(_dst, _max, "0.0"); + len = (int32_t)strCopy(_dst, _max, "0.0"); } else { diff --git a/src/os.cpp b/src/os.cpp index 0c7e9dc..291b281 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -235,12 +235,12 @@ namespace bx bool result = false; if (NULL != ptr) { - len = (uint32_t)strnlen(ptr); + len = (uint32_t)strLen(ptr); result = len != 0 && len < *_inOutSize; if (len < *_inOutSize) { - strlncpy(_out, len, ptr); + strCopy(_out, len, ptr); } } @@ -340,7 +340,7 @@ namespace bx if (stat("/tmp", fi) && FileInfo::Directory == fi.m_type) { - strlncpy(_out, *_inOutSize, "/tmp"); + strCopy(_out, *_inOutSize, "/tmp"); *_inOutSize = 4; return true; } @@ -418,7 +418,7 @@ namespace bx int32_t total = 0; for (uint32_t ii = 0; NULL != _argv[ii]; ++ii) { - total += (int32_t)strnlen(_argv[ii]) + 1; + total += (int32_t)strLen(_argv[ii]) + 1; } char* temp = (char*)alloca(total); diff --git a/src/string.cpp b/src/string.cpp index dbb59e5..480adc6 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -70,7 +70,7 @@ namespace bx void toLower(char* _inOutStr, int32_t _max) { - const int32_t len = strnlen(_inOutStr, _max); + const int32_t len = strLen(_inOutStr, _max); toLowerUnsafe(_inOutStr, len); } @@ -89,7 +89,7 @@ namespace bx void toUpper(char* _inOutStr, int32_t _max) { - const int32_t len = strnlen(_inOutStr, _max); + const int32_t len = strLen(_inOutStr, _max); toUpperUnsafe(_inOutStr, len); } @@ -134,7 +134,7 @@ namespace bx return strCmp(_lhs, _rhs, _max); } - int32_t strnlen(const char* _str, int32_t _max) + int32_t strLen(const char* _str, int32_t _max) { if (NULL == _str) { @@ -146,13 +146,13 @@ namespace bx return int32_t(ptr - _str); } - int32_t strlncpy(char* _dst, int32_t _dstSize, const char* _src, int32_t _num) + int32_t strCopy(char* _dst, int32_t _dstSize, const char* _src, int32_t _num) { BX_CHECK(NULL != _dst, "_dst can't be NULL!"); BX_CHECK(NULL != _src, "_src can't be NULL!"); BX_CHECK(0 < _dstSize, "_dstSize can't be 0!"); - const int32_t len = strnlen(_src, _num); + const int32_t len = strLen(_src, _num); const int32_t max = _dstSize-1; const int32_t num = (len < max ? len : max); memCopy(_dst, _src, num); @@ -161,20 +161,20 @@ namespace bx return num; } - int32_t strlncat(char* _dst, int32_t _dstSize, const char* _src, int32_t _num) + int32_t strCat(char* _dst, int32_t _dstSize, const char* _src, int32_t _num) { BX_CHECK(NULL != _dst, "_dst can't be NULL!"); BX_CHECK(NULL != _src, "_src can't be NULL!"); BX_CHECK(0 < _dstSize, "_dstSize can't be 0!"); const int32_t max = _dstSize; - const int32_t len = strnlen(_dst, max); - return strlncpy(&_dst[len], max-len, _src, _num); + const int32_t len = strLen(_dst, max); + return strCopy(&_dst[len], max-len, _src, _num); } const char* strnchr(const char* _str, char _ch, int32_t _max) { - for (int32_t ii = 0, len = strnlen(_str, _max); ii < len; ++ii) + for (int32_t ii = 0, len = strLen(_str, _max); ii < len; ++ii) { if (_str[ii] == _ch) { @@ -187,7 +187,7 @@ namespace bx const char* strnrchr(const char* _str, char _ch, int32_t _max) { - for (int32_t ii = strnlen(_str, _max); 0 < ii; --ii) + for (int32_t ii = strLen(_str, _max); 0 < ii; --ii) { if (_str[ii] == _ch) { @@ -203,8 +203,8 @@ namespace bx { const char* ptr = _str; - int32_t stringLen = strnlen(_str, _strMax); - const int32_t findLen = strnlen(_find, _findMax); + int32_t stringLen = strLen(_str, _strMax); + const int32_t findLen = strLen(_find, _findMax); for (; stringLen >= findLen; ++ptr, --stringLen) { @@ -251,7 +251,7 @@ namespace bx const char* strnl(const char* _str) { - for (; '\0' != *_str; _str += strnlen(_str, 1024) ) + for (; '\0' != *_str; _str += strLen(_str, 1024) ) { const char* eol = strnstr(_str, "\r\n", 1024); if (NULL != eol) @@ -271,7 +271,7 @@ namespace bx const char* streol(const char* _str) { - for (; '\0' != *_str; _str += strnlen(_str, 1024) ) + for (; '\0' != *_str; _str += strLen(_str, 1024) ) { const char* eol = strnstr(_str, "\r\n", 1024); if (NULL != eol) @@ -348,7 +348,7 @@ namespace bx const char* findIdentifierMatch(const char* _str, const char* _word) { - int32_t len = strnlen(_word); + int32_t len = strLen(_word); const char* ptr = strnstr(_str, _word); for (; NULL != ptr; ptr = strnstr(ptr + len, _word) ) { @@ -418,7 +418,7 @@ namespace bx static int32_t write(WriterI* _writer, const char* _str, int32_t _len, const Param& _param, Error* _err) { int32_t size = 0; - int32_t len = (int32_t)strnlen(_str, _len); + int32_t len = (int32_t)strLen(_str, _len); int32_t padding = _param.width > len ? _param.width - len : 0; bool sign = _param.sign && len > 1 && _str[0] != '-'; padding = padding > 0 ? padding - sign : 0; @@ -574,7 +574,7 @@ namespace bx int32_t write(WriterI* _writer, const char* _format, va_list _argList, Error* _err) { - MemoryReader reader(_format, uint32_t(strnlen(_format) ) ); + MemoryReader reader(_format, uint32_t(strLen(_format) ) ); int32_t size = 0; @@ -916,14 +916,4 @@ namespace bx snprintf(_out, _count, "%0.2f %c%c", size, "BkMGTPEZY"[idx], idx > 0 ? 'B' : '\0'); } - int32_t strlcpy(char* _dst, const char* _src, int32_t _max) - { - return strlncpy(_dst, _max, _src); - } - - int32_t strlcat(char* _dst, const char* _src, int32_t _max) - { - return strlncat(_dst, _max, _src); - } - } // namespace bx diff --git a/tests/string_test.cpp b/tests/string_test.cpp index 35e1dac..755384a 100644 --- a/tests/string_test.cpp +++ b/tests/string_test.cpp @@ -23,48 +23,48 @@ TEST_CASE("chars", "") } } -TEST_CASE("strnlen", "") +TEST_CASE("strLen", "") { const char* test = "test"; - REQUIRE(0 == bx::strnlen(test, 0) ); - REQUIRE(2 == bx::strnlen(test, 2) ); - REQUIRE(4 == bx::strnlen(test, INT32_MAX) ); + REQUIRE(0 == bx::strLen(test, 0) ); + REQUIRE(2 == bx::strLen(test, 2) ); + REQUIRE(4 == bx::strLen(test, INT32_MAX) ); } -TEST_CASE("strlncpy", "") +TEST_CASE("strCopy", "") { char dst[128]; size_t num; - num = bx::strlncpy(dst, 1, "blah"); + num = bx::strCopy(dst, 1, "blah"); REQUIRE(num == 0); - num = bx::strlncpy(dst, 3, "blah", 3); + num = bx::strCopy(dst, 3, "blah", 3); REQUIRE(0 == bx::strncmp(dst, "bl") ); REQUIRE(num == 2); - num = bx::strlncpy(dst, sizeof(dst), "blah", 3); + num = bx::strCopy(dst, sizeof(dst), "blah", 3); REQUIRE(0 == bx::strncmp(dst, "bla") ); REQUIRE(num == 3); - num = bx::strlncpy(dst, sizeof(dst), "blah"); + num = bx::strCopy(dst, sizeof(dst), "blah"); REQUIRE(0 == bx::strncmp(dst, "blah") ); REQUIRE(num == 4); } -TEST_CASE("strlncat", "") +TEST_CASE("strCat", "") { char dst[128] = { '\0' }; - REQUIRE(0 == bx::strlncat(dst, 1, "cat") ); + REQUIRE(0 == bx::strCat(dst, 1, "cat") ); - REQUIRE(4 == bx::strlncpy(dst, 5, "copy") ); - REQUIRE(3 == bx::strlncat(dst, 8, "cat") ); + REQUIRE(4 == bx::strCopy(dst, 5, "copy") ); + REQUIRE(3 == bx::strCat(dst, 8, "cat") ); REQUIRE(0 == bx::strncmp(dst, "copycat") ); - REQUIRE(1 == bx::strlncat(dst, BX_COUNTOF(dst), "------", 1) ); - REQUIRE(3 == bx::strlncat(dst, BX_COUNTOF(dst), "cat") ); + REQUIRE(1 == bx::strCat(dst, BX_COUNTOF(dst), "------", 1) ); + REQUIRE(3 == bx::strCat(dst, BX_COUNTOF(dst), "cat") ); REQUIRE(0 == bx::strncmp(dst, "copycat-cat") ); } @@ -134,7 +134,7 @@ static bool testToString(Ty _value, const char* _expected) { char tmp[1024]; int32_t num = bx::toString(tmp, BX_COUNTOF(tmp), _value); - int32_t len = (int32_t)bx::strnlen(_expected); + int32_t len = (int32_t)bx::strLen(_expected); if (0 == bx::strncmp(tmp, _expected) && num == len) { diff --git a/tests/vsnprintf_test.cpp b/tests/vsnprintf_test.cpp index 12aea7b..9bdd5ec 100644 --- a/tests/vsnprintf_test.cpp +++ b/tests/vsnprintf_test.cpp @@ -25,7 +25,7 @@ TEST_CASE("vsnprintf truncated", "Truncated output buffer.") static bool test(const char* _expected, const char* _format, ...) { - int32_t max = (int32_t)bx::strnlen(_expected) + 1; + int32_t max = (int32_t)bx::strLen(_expected) + 1; char* temp = (char*)alloca(max); va_list argList;