From e5fad2ff1ba876838baf264014cfe46627b96394 Mon Sep 17 00:00:00 2001 From: dean Date: Wed, 10 Jan 2024 16:21:24 +1100 Subject: [PATCH 1/4] fix for accessing string content when precision is 0. --- nanoprintf.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nanoprintf.h b/nanoprintf.h index 081fc47..59a6e1f 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)); + s && *s && ((fs.prec_opt == NPF_FMT_SPEC_OPT_NONE) || (cbuf_len < fs.prec)); ++s, ++cbuf_len); #else for (char const *s = cbuf; *s; ++s, ++cbuf_len); // strlen From 0dd4bbf1f3188cf3c606fe728144ea7af7054bd8 Mon Sep 17 00:00:00 2001 From: dean Date: Thu, 11 Jan 2024 11:08:51 +1100 Subject: [PATCH 2/4] added test for null strings with precision of 0, updated contributors. --- CONTRIBUTORS.md | 1 + nanoprintf.h | 2 +- tests/unit_vpprintf.cc | 5 +++++ 3 files changed, 7 insertions(+), 1 deletion(-) 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 59a6e1f..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 && *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..04ebb73 100644 --- a/tests/unit_vpprintf.cc +++ b/tests/unit_vpprintf.cc @@ -78,6 +78,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"}); From 3dbc7dd61fe6f5de9bd1410b61db94541f2d146a Mon Sep 17 00:00:00 2001 From: dean Date: Thu, 11 Jan 2024 12:41:10 +1100 Subject: [PATCH 3/4] added ignore pragma for -Wformat-overflow to tests --- tests/unit_vpprintf.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit_vpprintf.cc b/tests/unit_vpprintf.cc index 04ebb73..1e2c17d 100644 --- a/tests/unit_vpprintf.cc +++ b/tests/unit_vpprintf.cc @@ -14,6 +14,7 @@ #pragma GCC diagnostic ignored "-Wformat" #pragma GCC diagnostic ignored "-Wformat-zero-length" #pragma GCC diagnostic ignored "-Wformat-security" + #pragma GCC diagnostic ignored "-Wformat-overflow" #endif struct Recorder { From 3f3733bb6e4904e3d98a505d5cbc31ae371aa549 Mon Sep 17 00:00:00 2001 From: dean Date: Thu, 11 Jan 2024 13:05:10 +1100 Subject: [PATCH 4/4] moved -Wformat-overflow ignore pragma into else clause --- tests/unit_vpprintf.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit_vpprintf.cc b/tests/unit_vpprintf.cc index 1e2c17d..ac029ec 100644 --- a/tests/unit_vpprintf.cc +++ b/tests/unit_vpprintf.cc @@ -10,11 +10,12 @@ #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" #pragma GCC diagnostic ignored "-Wformat-security" - #pragma GCC diagnostic ignored "-Wformat-overflow" #endif struct Recorder {