microsoft / microsoft/STL

`<locale>`: Calling `use_facet<collate<char>>(loc)` changes output of `strftime()` in a different thread

Open
#6,150 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
C++
Stars
11.1k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

Description

Originally reported as DevCom-246257 / internal VSO-275595 by constructing regex{"meow"}, but regex calls use_facet so the repro can be reduced:

[!IMPORTANT]
This repro requires you to temporarily modify your system's locale settings.

C:\Temp>type meow.cpp
// Settings > Time & language > Date & time > Change the date and time format > Long time
// Change "9:40:07 AM / 2:40:07 PM" to "09:40:07 / 14:40:07"

#include <clocale>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <locale>
#include <thread>
using namespace std;

void run_on_other_thread() {
    locale loc{};
    (void) use_facet<collate<char>>(loc);
}

int main() {
    tm t{};
    t.tm_hour = 14;
    t.tm_min  = 30;
    t.tm_sec  = 45;

    setlocale(LC_ALL, "");

    char out1[100];
    strftime(out1, sizeof(out1), "%X", &t);
    puts(out1);

    {
        jthread thread(&run_on_other_thread);
    }

    char out2[100];
    strftime(out2, sizeof(out2), "%X", &t);
    puts(out2);

    if (strcmp(out1, out2) == 0) {
        puts("PASS");
    } else {
        puts("FAIL");
    }
}
C:\Temp>cl /EHsc /nologo /W4 /std:c++latest /MTd /Od /Zi /Fdmeow.pdb meow.cpp
meow.cpp

C:\Temp>meow
14:30:45
2:30:45 PM
FAIL

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling and running the reported meow.cpp reproduction with the specified MSVC command and locale setting; compare the two strftime("%X") outputs before and after the thread calls use_facet<collate>. Trace the use_facet entry point and the strftime path. Done means the repro no longer changes output across the thread boundary.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
localization
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.