MISRA C:2012 Rule 18.6
The address of an object with automatic or thread-local storage shall not be copied to another object that persists after the first object has ceased to exist
描述
规则定义
The address of an object with automatic or thread-local storage shall not be copied to another object that persists after the first object has ceased to exist 1 .
理由
当对象的生命周期到期时,该对象的地址将变得不确定。使用不确定的地址会导致未定义行为。
Polyspace 实现
当指向局部变量的指针离开该变量的作用域时,Polyspace® 会报告违反此规则。例如:
函数返回一个指向局部变量的指针。
函数执行赋值
globPtr = &locVar
。globPtr
是全局指针变量,locVar
是局部变量。函数执行赋值
*paramPtr = &locVar
。paramPtr
是函数参数,例如int**
指针,locVar
是局部变量int
。
Polyspace 将此规则应用于使用 alloca
函数分配的内存,并忽略静态局部变量。Polyspace 假定函数定义中的局部对象位于同一范围内。
故障排除
如果您预期会出现违规,但未看到该违规,请参阅诊断为何编码规范违规未按预期显示。
示例
检查信息
组:指针和数组 |
类别:必需 |
AGC 类别:必需 |
版本历史记录
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.