Skip to content

Commit 49628bd

Browse files
Fix nul char (#324)
* special case for the NUL char -- treat it as if it was an empty string, so that we do not emit NUL terminators in the middle of the output, and the result is consistent across pprintf and snprintf * commented (new) non-standard tests for %lc --------- Co-authored-by: Stefano Zanotti <[email protected]>
1 parent f0687bc commit 49628bd

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

nanoprintf.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) {
844844

845845
case NPF_FMT_SPEC_CONV_CHAR:
846846
*cbuf = (char)va_arg(args, int);
847-
cbuf_len = 1;
847+
cbuf_len = (*cbuf) ? 1 : 0;
848848
break;
849849

850850
case NPF_FMT_SPEC_CONV_STRING: {

tests/conformance.cc

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,33 @@ TEST_CASE("conformance to system printf") {
7979
}
8080

8181
SUBCASE("char") {
82-
// every char
82+
// every char except the NUL char
8383
for (int i = CHAR_MIN; i < CHAR_MAX; ++i) {
84-
char output[2] = {(char)i, 0};
85-
require_conform(output, "%c", i);
84+
if(i != '\0') {
85+
char output[2] = {(char)i, 0};
86+
require_conform(output, "%c", i);
87+
}
8688
}
8789

8890
require_conform("A", "%+c", 'A');
89-
require_conform("", "%+c", 0);
91+
92+
// These should be added to non-standard tests (when we add length-checks to the tests)
93+
//require_conform("", "%c", '\0'); // ret 0
94+
//require_conform("AB", "A%cB", '\0'); // ret 0
95+
//require_conform("", "%+c", '\0'); // ret 0
96+
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
97+
//require_conform("A B", "A%10cB", '\0'); // ret 12
98+
//require_conform("AB", "A%.5cB", '\0'); // ret 2
99+
//require_conform(" ", "%-5c", '\0'); // ret 5
100+
//require_conform(" ", "% 4c", '\0'); // ret 4
101+
#endif
102+
//for (int i = CHAR_MIN; i < CHAR_MAX; ++i) {
103+
// if(i != '\0') {
104+
// char output[2] = {(char)i, 0};
105+
// require_conform(output, "%lc", i);
106+
// }
107+
//}
108+
// end of non-standard tests
90109

91110
#if NANOPRINTF_USE_FIELD_WIDTH_FORMAT_SPECIFIERS == 1
92111
// right justify field width

0 commit comments

Comments
 (0)