Skip to content

Stack corruption with snprintf(3) and format %hu #5130

Description

@krytarowski

PAL tests fall down with

src/pal/tests/palsuite/c_runtime/vsprintf/test11/test11.c

with the following example call:

DoNumTest("foo %hu", 0xFFFF, "foo 65535");

There is stack corruption here:
https://github.com/dotnet/coreclr/blob/master/src/pal/src/cruntime/printfcpp.cpp#L1822

                else if (Type == PFF_TYPE_INT && Prefix == PFF_PREFIX_SHORT)
                {
                    // Convert explicitly from int to short to get
                    // correct sign extension for shorts on all systems.
                    int n;
                    short s;

                    n = va_arg(ap, int);
                    s = (short) n;

                    TempInt = snprintf(BufferPtr, TempCount, TempBuff, s); // <--- here
                }
(gdb) list
1814                        // Convert explicitly from int to short to get
1815                        // correct sign extension for shorts on all systems.
1816                        int n;
1817                        short s;
1818    
1819                        n = va_arg(ap, int);
1820                        s = (short) n;
1821    
1822                        TempInt = snprintf(BufferPtr, TempCount, TempBuff, s);
1823                    }
(gdb) n
1820                        s = (short) n;
(gdb) p n
$8 = 65535
(gdb) n
1822                        TempInt = snprintf(BufferPtr, TempCount, TempBuff, s);
(gdb) p s
$9 = -1
(gdb) p BufferPtr
$10 = (LPSTR) 0x7f7fffffd924 ""
(gdb) p BufferPtr-1
$11 = (CHAR *) 0x7f7fffffd923 " "
(gdb) p BufferPtr-2
$12 = (CHAR *) 0x7f7fffffd922 "o "
(gdb) p BufferPtr-3
$13 = (CHAR *) 0x7f7fffffd921 "oo "
(gdb) p BufferPtr-4
$14 = (CHAR *) 0x7f7fffffd920 "foo "
(gdb) p TempBuff
$15 = "%hu\000\177\177\000\000\006\362E\000\000\000\000\000\000\222\377\367\177\177\000\000\000P�\367\177\177\000\000�\322\377\377\177\177\000\000\006\362E\000\000\000\000\000�\322\377\377\177\177\000\000\000P�\367\177\177\000\000\020\323\377\377\177\177\000\000\006\362E\000\000\000\000\000��D\000\000\000\000\000\377\232N\000\000\000\000\000\000X\377\367\016\000\000\000\000P�\367\177\177\000\000@\323\377\377\177\177\000\000\006\362E\000\000\000\000\000\004\323\377\377\177\177\000\000L2�\367\177\177\000\000\000X\377\367\177\177\000\000��N\000\000\000\000\000\000X\377\367\177\177\000\000L2�\367\177\177\000\000\020\323\377\377\001\000\000\000�a�\367\177\177\000\000 \323\377\377\177\177\000\000"...
(gdb) n

Breakpoint 2, CoreVsnprintf (pthrCurrent=0x7f7ff7b05000, Buffer=0x7f7fffffd924 "", Count=2147483643, Format=0x7f7fffffd240 "%hu", aparg=0x7f7fffffcdb8)
    at /tmp/pkgsrc-tmp/wip/coreclr-git/work/coreclr/src/pal/src/cruntime/printfcpp.cpp:1819
1819                        n = va_arg(ap, int);
(gdb) 

I'm trying to determine what's going on.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions