Skip to content

Commit cff625b

Browse files
Fix a bug in PAL version of _vsnprint_f (#103003)
When the formatted string cannot fully fit in the buffer (including its null terminator) `_vsnprint_f` should return -1. However, in the case where the number of chars was the same as the buffer size it was returning the buffer size. Co-authored-by: Jakob Botsch Nielsen <[email protected]>
1 parent 67133a6 commit cff625b

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

src/coreclr/pal/src/safecrt/vsprintf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ DLLEXPORT int __cdecl _vsnprintf_s (
9595
retvalue = vsnprintf(string, sizeInBytes, format, ap);
9696
string[sizeInBytes - 1] = '\0';
9797
/* we allow truncation if count == _TRUNCATE */
98-
if (retvalue > (int)sizeInBytes && count == _TRUNCATE)
98+
if (retvalue >= (int)sizeInBytes && count == _TRUNCATE)
9999
{
100100
if (errno == ERANGE)
101101
{

src/coreclr/pal/tests/palsuite/c_runtime/_vsnprintf_s/test1/test1.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,18 @@ PALTEST(c_runtime__vsnprintf_s_test1_paltest_vsnprintf_test1, "c_runtime/_vsnpri
4949
Fail("ERROR: expected %s (up to %d chars), got %s\n", checkstr, 8, buf);
5050
}
5151

52+
char buf8[8] = {0};
53+
54+
ret = Testvsnprintf(buf8, 8, "abcdefgh");
55+
if (ret >= 0)
56+
{
57+
Fail("ERROR: expected negative return value, got %d", ret);
58+
}
59+
if (memcmp(buf8, "abcdefg\0", 8) != 0)
60+
{
61+
Fail("ERROR: Expected 7 chars + null terminator");
62+
}
63+
5264
PAL_Terminate();
5365
return PASS;
5466
}

0 commit comments

Comments
 (0)