Skip to content

<format>: Incorrect handling of UTF-8 encoded format strings #1820

Description

Describe the bug

std::format throws on formatting a valid UTF-8 string when the literal (execution) encoding is UTF-8 and the locale encoding is Shift-JIS (and possibly other cases). This is wrong because format strings are almost always literals and therefore the locale encoding shouldn't affect parsing.

Command-line test case

C:\Temp>type test.cc
#include <format>
#include <iostream>

int main() {
  setlocale(LC_ALL, ".932");
  try {
    auto s = std::format("\xe2\x82\xa0{}", 42);
    std::cout << (s == "\xe2\x82\xa0" "42") << "\n";
  } catch (const std::exception& e) {
    std::cout << e.what() << "\n";
  }
}

C:\Temp>cl /I STL/stl/inc /std:c++latest /utf-8 test.cc
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.29917 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

/std:c++latest is provided as a preview of language features from the latest C++
working draft, and we're eager to hear about bugs and suggestions for improvements.
However, note that these features are provided as-is without support, and subject
to changes or removal as the working draft evolves. See
https://go.microsoft.com/fwlink/?linkid=2045807 for details.

test.cc
test.cc(6): warning C4530: C++ exception handler used, but unwind semantics are not enabled. Specify /EHsc
Microsoft (R) Incremental Linker Version 14.29.29917.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:test.exe
test.obj

C:\Temp>test
Invalid encoded character in format string.

Expected behavior
The test program should print 1.

STL version
https://github.com/microsoft/STL/commit/4d7d4f1

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!formatC++20/23 format

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions