Skip to content

<locale>: locale initialization incorrect in debug mode #1175

Description

@fsb4000

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

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 workingvNextBreaks binary compatibility

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions