microsoft / microsoft/STL

<locale>: locale initialization incorrect in debug mode

Open
#1,175 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug
locale::id classes are used to lazily assign numbers to facets classes. They have non-trivial constructors and when compiled in debug mode they are not initialized at compile time. This means that the following could happen:

  1. Some static constructor (std::cin for example) uses locales and causes id assignment for some facets.
  2. locale::id constructors for this facets are called which reset facet ids to zero.
  3. The program tries to use locales and different ids are assigned to facets which leads to errors or crashes.

Command-line test case

C:\Temp>type main.cpp
#include <locale>
#include <iostream>

struct S {
	size_t id;
	S() {
		id = std::collate<char>::id;
	}
};

S v;

int main() {
	std::cout << v.id << std::endl;
	std::cout << std::collate<char>::id << std::endl;
}

C:\Temp>cl /EHsc /W4 /WX /MT main.cpp
Оптимизирующий компилятор Microsoft (R) C/C++ версии 19.28.29115 для x64
(C) Корпорация Майкрософт (Microsoft Corporation).  Все права защищены.

main.cpp
Microsoft (R) Incremental Linker Version 14.28.29115.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:main.exe
main.obj

C:\Temp>main.exe
2
5

Expected behavior
Either compiler must initialize locale::id's at compile time in debug mode or locale::id class should be rewritten to avoid this problem.

STL version
Microsoft Visual Studio Community 2019 Preview Version 16.8.0 Preview 1.0

Additional context
DevCom-256346 | Microsoft-internal VSO-620209 / AB#620209 and VSO-622201 / AB#622201

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 with the implementation of locale::id and reproduce the issue using the supplied example in debug and optimized builds. Check how static construction and facet-id assignment interact; done means locale::id values remain consistent and the regression is covered by a test based on the reported case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.