From edcee003e5a79386653c8092af3577a112e95451 Mon Sep 17 00:00:00 2001 From: Jose Luis Duran Date: Thu, 27 Oct 2022 07:01:24 -0300 Subject: [PATCH] strfmon_test: Reserve space for the null terminator Otherwise strfmon(3) could overflow the buffer. Here is mostly done for correctness and illustrative purposes, as there is no chance it could actually happen. Reviewed by: kib PR: 267410 Github PR: #620 MFC after: 1 week --- lib/libc/tests/stdlib/strfmon_test.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/libc/tests/stdlib/strfmon_test.c b/lib/libc/tests/stdlib/strfmon_test.c index d8e4f478547..c2fa6250dc5 100644 --- a/lib/libc/tests/stdlib/strfmon_test.c +++ b/lib/libc/tests/stdlib/strfmon_test.c @@ -55,7 +55,7 @@ ATF_TC_BODY(strfmon_locale_thousands, tc) atf_tc_skip("multi-byte thousands-separator not found"); n = 1234.56; - strfmon(actual, sizeof(actual), "%i", n); + strfmon(actual, sizeof(actual) - 1, "%i", n); strcpy(expected, "1"); strlcat(expected, ts, sizeof(expected)); @@ -95,7 +95,7 @@ ATF_TC_BODY(strfmon_examples, tc) for (i = 0; i < nitems(tests); ++i) { snprintf(format, sizeof(format), "[%s] [%s] [%s]", tests[i].format, tests[i].format, tests[i].format); - strfmon(actual, sizeof(actual), format, + strfmon(actual, sizeof(actual) - 1, format, 123.45, -123.45, 3456.781); ATF_CHECK_STREQ_MSG(tests[i].expected, actual, "[%s]", tests[i].format); @@ -135,7 +135,7 @@ ATF_TC_BODY(strfmon_cs_precedes_0, tc) for (j = 0; j < 5; ++j) { lc->n_sign_posn = j; - strfmon(buf, sizeof(buf), "[%n] ", -123.0); + strfmon(buf, sizeof(buf) - 1, "[%n] ", -123.0); strlcat(actual, buf, sizeof(actual)); } @@ -178,7 +178,7 @@ ATF_TC_BODY(strfmon_cs_precedes_1, tc) for (j = 0; j < 5; ++j) { lc->n_sign_posn = j; - strfmon(buf, sizeof(buf), "[%n] ", -123.0); + strfmon(buf, sizeof(buf) - 1, "[%n] ", -123.0); strlcat(actual, buf, sizeof(actual)); } @@ -206,7 +206,7 @@ ATF_TC_BODY(strfmon_international_currency_code, tc) if (setlocale(LC_MONETARY, tests[i].locale) == NULL) atf_tc_skip("unable to setlocale()"); - strfmon(actual, sizeof(actual), "[%i]", 123.45); + strfmon(actual, sizeof(actual) - 1, "[%i]", 123.45); ATF_CHECK_STREQ(tests[i].expected, actual); } }