Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-10359

Do not change code pages in Windows version of mongo shell

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.4.6, 2.5.2
    • Affects Version/s: 2.4.0
    • Component/s: Shell
    • Labels:
    • Environment:
    • Minor Change
    • Windows

      The Windows version of the mongo shell changes the console code page from its initial setting to CP_UTF8 (65001) in order to accomodate UTF-8 output that is not routed through the writeUtf8ToWindowsConsole() function in src/mongo/util/text.cpp.

      This code page switching causes a problem if the shell is abnormally exited (segfault or some other abort) because the initial code page is not restored on exit in that case. This leaves the console set to the UTF-8 code page, and other programs (e.g. Python) may complain.

      We should remove the code page switching code and let non-ASCII text displayed with, for example, "cout" just display badly (as corrupted text). This may encourage us to remove any remaining code paths that don't go through writeUtf8ToWindowsConsole().

      Here's the code: src/mongo/shell/dbshell.cpp lines 1136 to 1154 in the current master branch:

      int wmain(int argc, wchar_t* argvW[], wchar_t* envpW[]) {
          static mongo::StaticObserver staticObserver;
          UINT initialConsoleInputCodePage = GetConsoleCP();
          UINT initialConsoleOutputCodePage = GetConsoleOutputCP();
          SetConsoleCP( CP_UTF8 );
          SetConsoleOutputCP( CP_UTF8 );
          int returnCode;
          try {
              WindowsCommandLine wcl(argc, argvW, envpW);
              returnCode = _main(argc, wcl.argv(), wcl.envp());
          catch ( mongo::DBException& e ) {
              cerr << "exception: " << e.what() << endl;
              returnCode = 1;
          SetConsoleCP( initialConsoleInputCodePage );
          SetConsoleOutputCP( initialConsoleOutputCodePage );

      Lines 3 to 6 and 16 through 17 in this excerpt should be removed.

            tad Tad Marshall
            tad Tad Marshall
            0 Vote for this issue
            2 Start watching this issue