Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for accessing string content when precision is 0. #253

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion nanoprintf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions tests/unit_vpprintf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"});
Expand Down
Loading