From: Mark Wooding Date: Thu, 22 Feb 2024 19:43:58 +0000 (+0000) Subject: utils/gprintf.c: Return the correct number of output characters. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/commitdiff_plain/912f6431247d27e2144c5c078bd47396d95f5175 utils/gprintf.c: Return the correct number of output characters. Also fix the test program to check that the output count is correct. --- diff --git a/struct/t/dstr-putf-test.c b/struct/t/dstr-putf-test.c index 416b164..044da71 100644 --- a/struct/t/dstr-putf-test.c +++ b/struct/t/dstr-putf-test.c @@ -18,13 +18,16 @@ static char strbuf[1024]; #define TESTGROUP(name) TVEC_TESTGROUP_TAG(grp, &tvstate, name) -static void PRINTF_LIKE(1, 2) format(const char *fmt, ...) +static int PRINTF_LIKE(1, 2) format(const char *fmt, ...) { va_list ap; + int n; + va_start(ap, fmt); dstr_reset(&d); - dstr_vputf(&d, fmt, &ap); + n = dstr_vputf(&d, fmt, &ap); va_end(ap); + return (n); } static void PRINTF_LIKE(1, 2) prepare(const char *fmt, ...) @@ -42,14 +45,18 @@ static void PRINTF_LIKE(1, 2) prepare(const char *fmt, ...) } #define TEST_KNOWN(fmtargs, want) do { \ - format fmtargs; \ + int n; n = format fmtargs; \ + tvec_claimeq_int(&tvstate, n, strlen(want), \ + __FILE__, __LINE__, "format " #fmtargs); \ tvec_claimeq_string(&tvstate, d.buf, d.len, want, strlen(want), \ __FILE__, __LINE__, "format " #fmtargs); \ } while (0) #define TEST_REF(fmtargs) do { \ - format fmtargs; \ + int n = format fmtargs; \ prepare fmtargs; \ + tvec_claimeq_int(&tvstate, n, strlen(strbuf), \ + __FILE__, __LINE__, "format " #fmtargs); \ tvec_claimeq_string(&tvstate, d.buf, d.len, strbuf, strlen(strbuf), \ __FILE__, __LINE__, "format " #fmtargs); \ } while (0) diff --git a/utils/gprintf.c b/utils/gprintf.c index b575e1f..8825bd1 100644 --- a/utils/gprintf.c +++ b/utils/gprintf.c @@ -416,9 +416,13 @@ int vgprintf(const struct gprintf_ops *ops, void *out, if (fs->fmt == fmt_unset) { switch (fs->ch) { - case 0: break; - case '%': ops->putch(out, '%'); break; - default: abort(); + case 0: + break; + case '%': + if (ops->putch(out, '%')) return (-1); + tot++; break; + default: + abort(); } continue; } @@ -497,7 +501,7 @@ int vgprintf(const struct gprintf_ops *ops, void *out, #else # define MSG "" if (ops->putm(out, MSG, sizeof(MSG) - 1)) return (-1); - continue; + tot += sizeof(MSG) - 1; continue; # undef MSG #endif case 's':