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/msc_buffered_printf.c

    r3188 r3547  
    3939#include <conio.h>
    4040#include <malloc.h>
     41#include <locale.h>
    4142#include "console.h"
    4243
     
    169170int __cdecl puts(const char *pszString)
    170171{
    171     size_t cchString = strlen(pszString);
    172     size_t cch;
    173 
    174172    /*
    175173     * If it's a TTY, we convert it to a wide char string with a newline
     
    177175     * buffering due to the added newline.
    178176     */
    179     if (*pszString != '\0')
     177    size_t cchString = strlen(pszString);
     178    size_t cch;
     179    if (cchString > 0 && cchString < INT_MAX / 2)
    180180    {
    181181        int fd = fileno(stdout);
     
    188188                    && hCon != NULL)
    189189                {
    190                     /* We need to append a newline, so we can just as well do the conversion here. */
    191                     size_t   cwcTmp  = cchString * 2 + 16 + 2;
    192                     wchar_t *pawcTmp = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t));
    193                     if (pawcTmp)
     190                    wchar_t  awcBuf[1024];
     191                    wchar_t *pawcBuf;
     192                    wchar_t *pawcBufFree = NULL;
     193                    size_t   cwcBuf      = cchString * 2 + 16 + 1; /* +1 for added newline */
     194                    if (cwcBuf < sizeof(awcBuf) / sizeof(awcBuf[0]))
    194195                    {
    195                         int           cwcToWrite;
    196                         static UINT s_uConsoleCp = 0;
    197                         if (s_uConsoleCp == 0)
    198                             s_uConsoleCp = GetConsoleCP();
    199 
    200                         cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pszString, (int)cchString, pawcTmp,
    201                                                          (int)(cwcTmp - 2));
     196                        pawcBuf = awcBuf;
     197                        cwcBuf  = sizeof(awcBuf) / sizeof(awcBuf[0]);
     198                    }
     199                    else
     200                        pawcBufFree = pawcBuf = (wchar_t *)malloc(cwcBuf * sizeof(wchar_t));
     201                    if (pawcBuf)
     202                    {
     203                        int cwcToWrite = MultiByteToWideChar(get_crt_codepage(), 0 /*dwFlags*/,
     204                                                             pszString, (int)cchString,
     205                                                             pawcBuf, (int)(cwcBuf - 1));
    202206                        if (cwcToWrite > 0)
    203207                        {
    204208                            int rc;
    205 
    206                             pawcTmp[cwcToWrite++] = '\n';
    207                             pawcTmp[cwcToWrite]   = '\0';
     209                            pawcBuf[cwcToWrite++] = '\n';
     210                            pawcBuf[cwcToWrite]   = '\0';
    208211
    209212                            /* Let the CRT do the rest.  At least the Visual C++ 2010 CRT
    210                                sources indicates _cputws will do the right thing we want.  */
     213                               sources indicates _cputws will do the right thing.  */
    211214                            fflush(stdout);
    212                             rc = _cputws(pawcTmp);
    213                             free(pawcTmp);
    214                             return rc;
     215                            rc = _cputws(pawcBuf);
     216                            if (pawcBufFree)
     217                                free(pawcBufFree);
     218                            if (rc >= 0)
     219                                return 0;
     220                            return -1;
    215221                        }
    216                         free(pawcTmp);
     222                        free(pawcBufFree);
    217223                    }
    218224                }
Note: See TracChangeset for help on using the changeset viewer.