aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--printf.c3
-rw-r--r--test/test_suite.cpp3
2 files changed, 5 insertions, 1 deletions
diff --git a/printf.c b/printf.c
index 50af893..d71665f 100644
--- a/printf.c
+++ b/printf.c
@@ -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"));