CWE Rule 562
Description
Rule Description
A function returns the address of a stack variable, which will cause unintended program behavior, typically in the form of a crash.
Polyspace Implementation
The rule checker checks for Pointer or reference to stack variable leaving scope.
Examples
This issue occurs when a pointer or reference to a local variable leaves the scope of the variable. For instance:
A function returns a pointer to a local variable.
A function performs the assignment
globPtr = &locVar.globPtris a global pointer variable andlocVaris a local variable.A function performs the assignment
*paramPtr = &locVar.paramPtris a function parameter that is, for instance, anint**pointer andlocVaris a localintvariable.A C++ method performs the assignment
memPtr = &locVar.memPtris a pointer data member of the class the method belongs to.locVaris a variable local to the method.(C++11 and later) A function returns a lambda expression object that captures local variables of the function by reference.
The defect also applies to memory allocated using the
alloca function. The defect does not apply to static, local
variables. Polyspace® assumes that the local objects within a function definition are in the
same scope.
Local variables are allocated an address on the stack. Once the scope of a local variable ends, this address is available for reuse. Using this address to access the local variable value outside the variable scope can cause unexpected behavior.
If a pointer to a local variable leaves the scope of the variable, Polyspace Bug Finder™ highlights the defect. The defect appears even if you do not use the address stored in the pointer. For maintainable code, it is a good practice to not allow the pointer to leave the variable scope. Even if you do not use the address in the pointer now, someone else using your function can use the address, causing undefined behavior.
Do not allow a pointer or reference to a local variable to leave the variable scope.
void func2(int *ptr) {
*ptr = 0;
}
int* func1(void) {
int ret = 0; //Noncompliant
return &ret ;
}
void main(void) {
int* ptr = func1() ;
func2(ptr) ;
}In this example, func1 returns a pointer
to local variable ret.
In main, ptr points to
the address of the local variable. When ptr is
accessed in func2, the access is illegal because
the scope of ret is limited to func1,
auto createAdder(int amountToAdd) {
int addThis = amountToAdd; //Noncompliant
auto adder = [&] (int initialAmount) {
return (initialAmount + addThis);
};
return adder;
}
void func() {
auto AddByTwo = createAdder(2);
int res = AddByTwo(10);
}In this example, the createAdder function defines a lambda expression
adder that captures the local variable addThis by
reference. The scope of addThis is limited to the
createAdder function. When the object returned by
createAdder is called, a reference to the variable
addThis is accessed outside its scope. When accessed in this way, the value
of addThis is undefined.
If a function returns a lambda expression object, avoid capturing local variables by reference in the lambda object. Capture the variables by copy instead.
Variables captured by copy have the same lifetime as the lambda object, but variables captured by reference often have a smaller lifetime than the lambda object itself. When the lambda object is used, these variables accessed outside scope have undefined values.
auto createAdder(int amountToAdd) {
int addThis = amountToAdd;
auto adder = [=] (int initialAmount) {
return (initialAmount + addThis);
};
return adder;
}
void func() {
auto AddByTwo = createAdder(2);
int res = AddByTwo(10);
}Check Information
| Category: Bad Coding Practices |
Version History
Introduced in R2023a
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)