Skip to content

cpp(localtime): unsafe function #124

@wenchy

Description

@wenchy

Compiling waring in Windows (Visual Studio 2022):

D:\GitHub\loader\test\cpp-tableau-loader\src\protoconf\logger.pc.cc(106): warning C4996: 'localtime': This function or variable may be unsafe. Consider using localtime_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

Note: std::localtime is not thread-safe as it returns a pointer to a static internal buffer. For thread-safe alternatives, consider using localtime_r (on POSIX systems) or the C++20 library's std::chrono::zoned_time for more robust and modern time handling.

See https://en.cppreference.com/w/c/chrono/localtime

Notes
The function localtime may not be thread-safe. The Microsoft CRT implementation is thread-safe.

POSIX requires that localtime and localtime_r set errno to EOVERFLOW if it fails because the argument is too large.

POSIX specifies that the timezone information is determined by localtime and localtime_r as if by calling tzset, which reads the environment variable TZ.

The implementation of localtime_s in Microsoft CRT is incompatible with the C standard since it has reversed parameter order and returns errno_t.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions