# VNX-562: Return of Stack Variable Address

A C or C++ function returns the address of a local array, local variable or by-value parameter, a local string's c_str(), a string_view of a local string.

| Field | Value |
|---|---|
| Rule ID | `VNX-562` |
| Severity | High |
| SARIF level | `error` |
| Kind | `sast` |
| Languages | C, C++ |
| CWE | [CWE-562](https://cwe.mitre.org/data/definitions/562.html) |
| CAPEC | [CAPEC-66](https://capec.mitre.org/data/definitions/66.html) |
| ATT&CK | [T1190](https://attack.mitre.org/techniques/T1190/) |
| CWSS vector | `CWSS:1.0/TI:H/AP:RU/AL:S/IC:N/FC:LT/RP:RU/RL:S/AV:L/AS:N/IN:A/SC:A/BI:H/DI:H/EX:H/EC:N/P:H` |
| Tags | `return-stack-address` `dangling-pointer` `cwe-562` |
| Source | [`internal/sast/rules/vnx-562.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-562.rego) |

## Overview

VNX-562 maps to [CWE-562: Return of Stack Variable Address](https://cwe.mitre.org/data/definitions/562.html). It reports functions that hand their caller a pointer or reference into their own stack frame.

## Why this matters

A function's locals are released when it returns. The returned pointer still points at that memory, which the next call overwrites. The program often appears to work until a different call sequence or compiler optimisation changes what lives there, and then reads garbage, leaks data from another frame or writes over a return address.

## What gets flagged

```c
char *format_name(const char *first, const char *last)
{
    char buf[128];
    snprintf(buf, sizeof(buf), "%s %s", first, last);
    return buf;                 /* FLAGGED: local array */
}

int *counter(int start) {
    int value = start + 1;
    return &value;              /* FLAGGED: address of a local */
}
```

```cpp
const char *display_name(int id) {
    std::string label = "user-" + std::to_string(id);
    return label.c_str();       // FLAGGED: buffer of a local string
}

std::string &cached_title() {
    std::string title = "home";
    return title;               // FLAGGED: reference to a local
}
```

Not flagged: `static`, `extern` and `thread_local` locals; heap pointers returned by value; globals and class members; pointer and array parameters; `&p->field` and `&p[i]` through a pointer; functions returning `std::string` or another value type (the result is copied); comments and string literals.

## Remediation

1. Return by value (`std::string`, a struct) and let the compiler elide the copy.
2. Take a caller-supplied buffer and its size, or allocate on the heap and document who frees it.
3. Use `static` storage only when the function is not reentrant or thread-shared.
4. Build with `-Wreturn-local-addr` / `-Wreturn-stack-address` and treat them as errors.

## References

- [CWE-562: Return of Stack Variable Address](https://cwe.mitre.org/data/definitions/562.html)
- [SEI CERT DCL30-C: Declare objects with appropriate storage durations](https://wiki.sei.cmu.edu/confluence/display/c/DCL30-C.+Declare+objects+with+appropriate+storage+durations)

## Suppressing this rule

Put a `nosec` comment on the line, or on line 1 to cover the whole file. Use the comment marker of the file's language (`#`, `//`, `--` or `;`):

```text
# nosec vnx-562
// nosec vnx-562
```

Or record an ignore rule: `vulnetix ignore add --rule vnx-562 --reason "..."`. See [Suppressing findings](/sast/guides/suppressing-findings.md).
