主要内容

MISRA C++:2023 Rule 25.5.1

The setlocale and std::locale::global functions shall not be called

自 R2024b 起

描述

The setlocale and std::locale::global functions shall not be called. 1

理由

调用 setlocale()std::locale::global() 函数可能会与对 setlocale() 函数的其他调用或与调用使用区域设置的函数(如 fprintf()tolower())产生数据争用。

Polyspace 实现

检查项会针对显式调用 setlocale()std::locale::global() 函数报告违规。

故障排除

如果您预期会出现违规,而 Polyspace® 未报告该违规,请参阅诊断为何编码规范违规未按预期显示

示例

全部展开

在此示例中,所有使用 setlocale() 的情形都违反了规则。

#include <stdio.h>
#include <locale.h>
#include <time.h>
#include <wchar.h>
 
int main(void)
{
    setlocale(LC_ALL, "en_US.UTF-8");     //Noncompliant
    setlocale(LC_NUMERIC, "de_DE.utf8");  //Noncompliant
    setlocale(LC_TIME, "ja_JP.utf8");     //Noncompliant
 
    wchar_t timeString[100];
    time_t currentTime = time(NULL);
    wcsftime(timeString, 100, L"%A %c", localtime(&currentTime));
    wprintf(L"Number: %.2f\nDate: %ls\n", 3.14, timeString);
}

检查信息

组: 本地化库
类别:必需
PQL 名称:std.misra_cpp_2023.R25_5_1

版本历史记录

在 R2024b 中推出


1 All MISRA coding rules and directives are © Copyright The MISRA Consortium Limited 2021.

The MISRA coding standards referenced in the Polyspace Bug Finder™ documentation are from the following MISRA standards:

  • MISRA C:2004

  • MISRA C:2012

  • MISRA C:2023

  • MISRA C++:2008

  • MISRA C++:2023

MISRA and MISRA C are registered trademarks of The MISRA Consortium Limited 2021.