diff --git a/src/crt.cpp b/src/crt.cpp index ab6af2b..a13400a 100644 --- a/src/crt.cpp +++ b/src/crt.cpp @@ -166,7 +166,11 @@ namespace bx size += writeRep(_writer, _param.fill, padding, _err); } - if (_param.upper) + if (NULL == _str) + { + size += write(_writer, "(null)", 6, _err); + } + else if (_param.upper) { for (int32_t ii = 0; ii < len; ++ii) { diff --git a/src/string.cpp b/src/string.cpp index 3e927f9..b003494 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -107,8 +107,13 @@ namespace bx size_t strnlen(const char* _str, size_t _max) { - const char* ptr; - for (ptr = _str; 0 < _max && *ptr != '\0'; ++ptr, --_max) {}; + if (NULL == _str) + { + return 0; + } + + const char* ptr = _str; + for (; 0 < _max && *ptr != '\0'; ++ptr, --_max) {}; return ptr - _str; } diff --git a/tests/vsnprintf_test.cpp b/tests/vsnprintf_test.cpp index d85fae0..eca14bb 100644 --- a/tests/vsnprintf_test.cpp +++ b/tests/vsnprintf_test.cpp @@ -38,7 +38,7 @@ static bool test(const char* _expected, const char* _format, ...) if (!result) { - printf("result (%d) %s, expected (%d) %s\n", len, temp, max-1, _expected); + printf("result (%d) '%s', expected (%d) '%s'\n", len, temp, max-1, _expected); } return result; @@ -89,6 +89,11 @@ TEST_CASE("vsnprintf p", "") REQUIRE(test("0xbadc0de ", "%-20p", (void*)0xbadc0de) ); } +TEST_CASE("vsnprintf s", "") +{ + REQUIRE(test("(null)", "%s", NULL) ); +} + TEST_CASE("vsnprintf", "") { REQUIRE(test("x", "%c", 'x') );