Ignore:
Timestamp:
Jan 29, 2022, 3:39:47 AM (3 years ago)
Author:
bird
Message:

lib: Changed the console-optimization wrappers to use the get_crt_codepage() function as that's more appropriate than GetConsoleCP() for use with _cputws(). Also made them avoid the heap for smaller writes that could fit into a reasonable (2KiB) stack buffer.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/maybe_con_write.c

    r3188 r3547  
    6363     * call WriteConsoleW directly.
    6464     */
    65     if (cbToWrite > 0)
     65    if (cbToWrite > 0 && cbToWrite < INT_MAX / 2)
    6666    {
    6767        HANDLE hCon = (HANDLE)_get_osfhandle(fd);
     
    7171            if (is_console_handle((intptr_t)hCon))
    7272            {
    73                 size_t   cwcTmp  = cbToWrite * 2 + 16;
    74                 wchar_t *pawcTmp = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t));
    75                 if (pawcTmp)
     73                wchar_t  awcBuf[1024];
     74                wchar_t *pawcBuf;
     75                wchar_t *pawcBufFree = NULL;
     76                size_t   cwcBuf      = cbToWrite * 2 + 16;
     77                if (cwcBuf < sizeof(awcBuf) / sizeof(awcBuf[0]))
    7678                {
    77                     int           cwcToWrite;
    78                     static UINT s_uConsoleCp = 0;
    79                     if (s_uConsoleCp == 0)
    80                         s_uConsoleCp = GetConsoleCP();
    81 
    82                     cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite,
    83                                                      pawcTmp, (int)(cwcTmp - 1));
     79                    pawcBuf = awcBuf;
     80                    cwcBuf  = sizeof(awcBuf) / sizeof(awcBuf[0]);
     81                }
     82                else
     83                    pawcBufFree = pawcBuf = (wchar_t *)malloc(cwcBuf * sizeof(wchar_t));
     84                if (pawcBuf)
     85                {
     86                    int cwcToWrite = MultiByteToWideChar(get_crt_codepage(), 0 /*dwFlags*/,
     87                                                         pvBuf, (int)cbToWrite,
     88                                                         pawcBuf, (int)(cwcBuf - 1));
    8489                    if (cwcToWrite > 0)
    8590                    {
     91                        int rc;
     92                        pawcBuf[cwcToWrite] = '\0';
     93
    8694                        /* Let the CRT do the rest.  At least the Visual C++ 2010 CRT
    8795                           sources indicates _cputws will do the right thing.  */
    88                         pawcTmp[cwcToWrite] = '\0';
    89                         if (_cputws(pawcTmp) >= 0)
     96                        rc = _cputws(pawcBuf);
     97                        if (pawcBufFree)
     98                            free(pawcBufFree);
     99                        if (rc >= 0)
    90100                            return cbToWrite;
    91101                        return -1;
    92102                    }
     103                    free(pawcBufFree);
    93104                }
    94105            }
Note: See TracChangeset for help on using the changeset viewer.