主要内容

MISRA C:2023 Rule 17.8

A function parameter should not be modified

自 R2024a 起

描述

规则定义

A function parameter should not be modified 1 .

理由

当您修改某个参数时,与该参数对应的函数参量不会被修改。然而,您或其他不熟悉 C 的程序员可能会错误地以为,当修改参数时,参量也会被修改。

Polyspace 实现

当您在函数体中修改函数参数时,Polyspace® 会报告违反了此规则。当函数参数以地址的形式作为 const 传递给另一个函数时,Polyspace 不会报告违反了此规则。

故障排除

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

示例

全部展开

在此示例中,当参数 param1 被修改时会违反该规则。Polyspace 会报告违规。因为 param3 被作为 const 传递给 foo,所以它不会被修改。Polyspace 不会报告违规。

int input(void);
int foo(const int*);
void func(int param1, int* param2, int param3) {
    
    param1 = input();   /* Non-compliant */
    *param2 = input();  /* Compliant */
    foo(&param3); /*Compliant*/
}

当指针参数(例如 param2)被修改时,不会违反该规则。

检查信息

组:函数
类别:建议
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.