Clang warning: ... stack memory associated with ... B returned [-Wreturn-stack-address] (warn_ret_stack_addr_ref)

From emmtrix Wiki
Jump to navigation Jump to search
Text
error:
address of
reference to
stack memory associated with
local variable
parameter
B returned

(since 7.0)

error:
address of
reference to
stack memory associated with local variable B returned

(until 6.0)

Type Warning
Category Semantic Issue
Internal Id warn_ret_stack_addr_ref
Active by Default Yes
Flags -Wno-dangling (12 elements)
-Wno-return-local-addr (3 elements)
-Wno-return-stack-address (3 elements)
Internal Message %select{address of|reference to}0 stack memory associated with %select{local variable|parameter}2 %1 returned (since 7.0)
%select{address of|reference to}0 stack memory associated with local variable %1 returned (until 6.0)
Regular Expression (?:warning|error|fatal error)\: (?:address of|reference to) stack memory associated with (?:local variable|parameter) (.*?) returned \[(?:\-Werror,)?\-Wreturn\-stack\-address[^\]]*\]
First Commit 2015-11-17 da7b27ff0b98 [Sema] Combine similar diagnostics using %select. NFC

Description

The warning is issued by the Clang compiler when a function returns by address or reference a portion of the stack memory associated either with a local variable or a parameter. This behavior is identified by the compiler as potentially unsafe because stack memory is deallocated once the function call that owns the local variable or receives the parameter ends. Thus, the returned address or reference may refer to a memory location that is no longer valid for use. The warning aims to highlight places in the code where this unsafe memory usage may occur, alerting the developer to the risk of undefined behavior should the returned memory location be accessed.

-Wreturn-stack-address is the specific warning flag related to this issue and is active by default, given its importance in maintaining the integrity and safety of memory usage within an application. Adjusting or disregarding this warning involves careful consideration and, usually, a more appropriate memory handling strategy to ensure the longevity and validity of the returned data.

 
AI Generated

Example

In the following example, a function named f is defined, returning a reference to an integer. Within this function, an integer variable a is declared and initialized with the value 10. However, instead of returning the variable's value, the function attempts to return a reference to a. As a is a local variable with automatic storage duration, its lifetime ends when the function f returns. This implies that the reference to a becomes invalid once the function exits. Consequently, when f is called in the main function, and its return value is assigned to b, the reference b points to a memory location that is no longer valid. Accessing b results in undefined behavior as it refers to stack memory that was associated with a local variable which has already been deallocated. The compiler warning warning: reference to stack memory associated with local variable 'a' returned [-Wreturn-stack-address] is generated to signal this potentially unsafe operation.  
AI Generated


Flags -xc++

[Try out in Compiler Explorer]

Source
#include<iostream>
using namespace std;

int& f() {
    int a = 10; // Local variable
    return a; // Return reference to local variable
}

int main() {
    int& b = f();
    cout << b << endl;
    return 0;
}
Compiler Output
<source>:6:12: warning: reference to stack memory associated with local variable 'a' returned [-Wreturn-stack-address]


Clang Internals (17.0.6)

Git Commit Message

[Sema] Combine similar diagnostics using %select. NFC

llvm-svn: 253315

Used in Clang Sources

This section lists all occurrences of the diagnostic within the Clang's codebase. For each occurrence, an auto-extracted snipped from the source code is listed including key elements like control structures, functions, or classes. It should illustrate the conditions under which the diagnostic is activated.

clang/lib/Sema/SemaInit.cpp (line 8257)

void Sema::checkInitializerLifetime(const InitializedEntity &Entity, Expr *Init) {
  // ...
  auto TemporaryVisitor = [&](IndirectLocalPath &Path, Local L, ReferenceKind RK) -> bool {
    // ...
    case LK_Return:
    case LK_StmtExprResult:
      if (auto *DRE = dyn_cast<DeclRefExpr>(L)) {
        // ...
        Diag(DiagLoc, diag::warn_ret_stack_addr_ref) << Entity.getType()->isReferenceType() << DRE->getDecl() << isa<ParmVarDecl>(DRE->getDecl()) << DiagRange;

Triggered in Clang Tests

This section lists all internal Clang test cases that trigger the diagnostic.

clang/test/CodeGen/pointer-to-int.c

  • clang/test/CodeGen/pointer-to-int.c:11:10: warning: address of stack memory associated with local variable 'x' returned [-Wreturn-stack-address]