主要内容

MISRA C++:2023 Rule 30.0.1

The C Library input/output functions shall not be used

自 R2024b 起

描述

The C Library input/output functions shall not be used. 1

理由

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

例如:

  • gets() 函数有以下用例:

    char * gets ( char * buf );
    该函数不会检查标准输入提供的字符数是否超出了缓冲区 buf 的大小。当输入超出缓冲区大小时,该函数可能会出现意外行为。请勿使用 <cwchar> 中的宽字符函数等效项。

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

Polyspace 实现

如果您使用了在 <cstdio> 中声明的函数或 <cwchar> 中的宽字符等效项,则 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.misra_cpp_2023.R30_0_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.