主要内容

AUTOSAR C++14 Rule M27-0-1

R2026b

The stream input/output library <cstdio> shall not be used

描述

The stream input/output library <cstdio> shall not be used.

理由

cstdio 中的函数(例如 gets、fgetpos、fopen、ftell 等)具有未指定行为、未定义行为和实现定义的行为。

例如:

  • gets 函数:

    char * gets ( char * buf );
    不会检查标准输入提供的字符数是否超出了缓冲区 buf 的大小。当输入超出缓冲区大小时,该函数可能会出现意外行为。

  • fopen 函数具有特定于实现的行为,该行为涉及在出错时是否设置 errno,或者是否接受标准模式设定符之后的附加字符。

Polyspace 实现

如果您使用了 cstdio 库中的函数,则 Polyspace® 会报告违反了此规则。Polyspace 会检测是否使用了以下 cstdio 函数:

  • remove 和 rename 等文件操作函数。

  • fclose、fflush 和 fopen 等文件访问函数。

  • fprintf、fscanf、printf 和 scanf 等格式化的输入/输出函数。

  • fgetc、fgets、fputc 和 getc 等字符输入输出函数。

  • fread 和 fwrite 等直接输入/输出函数。

  • fgetpos 和 fsetpos 等文件定位函数。

  • clearerr、ferror 和 perror 等错误处理函数。

故障排除

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

示例

全部展开

#include <cstdio>

void func()
{
    char array[10];
    fgets(array, sizeof array, stdin); //Noncompliant
}

使用 fgets() 违反了此规则。

检查信息

组:输入/输出库
类别:必需、自动
PQL 名称:std.autosar_cpp14.M27_0_1

版本历史记录

在 R2019a 中推出