diff options
author | Marco Paland <marco@paland.com> | 2018-09-14 13:48:27 +0200 |
---|---|---|
committer | Marco Paland <marco@paland.com> | 2018-09-14 13:48:27 +0200 |
commit | 6dae1687b5fd4dd7ee104079da0df906fe393263 (patch) | |
tree | 9769d32b33e0ab86d2285b71d62398f589a5f296 | |
parent | f8a2be378d6df840ee16e0019435cadc659348c1 (diff) | |
download | printf-6dae1687b5fd4dd7ee104079da0df906fe393263.tar.gz printf-6dae1687b5fd4dd7ee104079da0df906fe393263.tar.bz2 printf-6dae1687b5fd4dd7ee104079da0df906fe393263.zip |
fix(printf): fix negative argument precision
Fixes #25
-rw-r--r-- | printf.c | 3 | ||||
-rw-r--r-- | test/test_suite.cpp | 3 |
2 files changed, 5 insertions, 1 deletions
@@ -464,7 +464,8 @@ static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const precision = _atoi(&format);
}
else if (*format == '*') {
- precision = (unsigned int)va_arg(va, int);
+ const int prec = (int)va_arg(va, int);
+ precision = prec > 0 ? (unsigned int)prec : 0U;
format++;
}
}
diff --git a/test/test_suite.cpp b/test/test_suite.cpp index 05b95f7..e693851 100644 --- a/test/test_suite.cpp +++ b/test/test_suite.cpp @@ -1226,6 +1226,9 @@ TEST_CASE("misc", "[]" ) { test::sprintf(buffer, "%.*f", 2, 0.33333333);
REQUIRE(!strcmp(buffer, "0.33"));
+ test::sprintf(buffer, "%.*d", -1, 1);
+ REQUIRE(!strcmp(buffer, "1"));
+
test::sprintf(buffer, "%.3s", "foobar");
REQUIRE(!strcmp(buffer, "foo"));
|