diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 06c00a5..c78f8ae 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -6,3 +6,4 @@ * [Shang Yuanchun](https://github.com/ideal) * [Shreyas Balakrishna](https://github.com/shreyasbharath) * [Jim Keener](https://github.com/jimktrains) +* [Dean T](https://github.com/deanoburrito) diff --git a/nanoprintf.h b/nanoprintf.h index 081fc47..8273764 100644 --- a/nanoprintf.h +++ b/nanoprintf.h @@ -744,7 +744,7 @@ int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) { cbuf = va_arg(args, char *); #if NANOPRINTF_USE_PRECISION_FORMAT_SPECIFIERS == 1 for (char const *s = cbuf; - *s && ((fs.prec_opt == NPF_FMT_SPEC_OPT_NONE) || (cbuf_len < fs.prec)); + ((fs.prec_opt == NPF_FMT_SPEC_OPT_NONE) || (cbuf_len < fs.prec)) && *s; ++s, ++cbuf_len); #else for (char const *s = cbuf; *s; ++s, ++cbuf_len); // strlen diff --git a/tests/unit_vpprintf.cc b/tests/unit_vpprintf.cc index 05cbcd1..ac029ec 100644 --- a/tests/unit_vpprintf.cc +++ b/tests/unit_vpprintf.cc @@ -10,6 +10,8 @@ #if NANOPRINTF_CLANG #pragma GCC diagnostic ignored "-Wformat-pedantic" #pragma GCC diagnostic ignored "-Wold-style-cast" + #else + #pragma GCC diagnostic ignored "-Wformat-overflow" #endif #pragma GCC diagnostic ignored "-Wformat" #pragma GCC diagnostic ignored "-Wformat-zero-length" @@ -78,6 +80,11 @@ TEST_CASE("npf_vpprintf") { REQUIRE(r.String() == std::string{"abcdefgh"}); } + SUBCASE("string precision zero null pointer") { + REQUIRE(npf_pprintf(r.PutC, &r, "%.0s", nullptr) == 0); + REQUIRE(r.String() == std::string{""}); + } + SUBCASE("signed int zero") { REQUIRE(npf_pprintf(r.PutC, &r, "%i", 0) == 1); REQUIRE(r.String() == std::string{"0"});