主要内容

MISRA C:2023 Rule 17.1

The standard header file <stdarg.h> shall not be used

自 R2024a 起

描述

规则定义

The standard header file <stdarg.h> shall not be used. 1

理由

该规则禁止使用 va_listva_argva_startva_endva_copy

对于使用这些功能的某些方式,标准中可能未定义其行为。例如:

  • 您在函数中调用了 va_start,但在函数模块结束之前没有调用对应的 va_end

  • 您在不同的函数中对 va_list 类型的同一个变量调用了 va_arg

  • va_arg 的语法为 type va_arg (va_list ap, type)

    您使用与从 ap 检索到的参量的实际类型不兼容的 type 调用 va_arg

故障排除

如果您预期会出现违规,但未看到该违规,请参阅诊断为何编码规范违规未按预期显示

示例

全部展开

#include<stdarg.h>                      /* Non-compliant */
void f2(int n, ...) {
    int i;
    double val;
    va_list vl;                         /* Non-compliant */

    va_start(vl, n);                    /* Non-compliant */

    for(i = 0; i < n; i++)
    {
        val = va_arg(vl, double);       /* Non-compliant */
    }

    va_end(vl);                         /* Non-compliant */
}

在此示例中,因为使用了 va_startva_listva_argva_end,所以违反了该规则。


#include <stdarg.h>                     /* Non-compliant */
void h(va_list ap) {                    /* Non-compliant */
    double y;

    y = va_arg(ap, double );            /* Non-compliant */
}

void g(unsigned short n, ...) {
    unsigned int x;
    va_list ap;                         /* Non-compliant */

    va_start(ap, n);                    /* Non-compliant */
    x = va_arg(ap, unsigned int);       /* Non-compliant */

    h(ap);

    /* Undefined - ap is indeterminate because va_arg used in h () */
    x = va_arg(ap, unsigned int);       /* Non-compliant */

}

void f(void) {
    /* undefined - uint32_t:double type mismatch when g uses va_arg () */
    g(1, 2.0, 3.0);
}

在此示例中,在函数 gh 中,对 va_list 类型的同一个变量 ap 使用了 va_arg。在 g 中,第二个参量的类型是 unsigned int,在 h 中,第二个参量的类型是 double。这种类型不匹配会导致未定义行为。

检查信息

组:函数
类别:必需
AGC 类别:必需

版本历史记录

在 R2024a 中推出


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.