# VNX-242: Use of Inherently Dangerous Function

C/C++ calls to gets(), std::gets(), _getws(), getws(), _getts() or getwd(), and C++ operator>> from a stream into a char array with no std::setw().

| Field | Value |
|---|---|
| Rule ID | `VNX-242` |
| Severity | High |
| SARIF level | `error` |
| Kind | `sast` |
| Languages | C, C++ |
| CWE | [CWE-242](https://cwe.mitre.org/data/definitions/242.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 | `dangerous-function` `cwe-242` `buffer-overflow` |
| Source | [`internal/sast/rules/vnx-242.rego`](https://github.com/Vulnesis/cli/blob/main/internal/sast/rules/vnx-242.rego) |

## Overview

VNX-242 maps to [CWE-242: Use of Inherently Dangerous Function](https://cwe.mitre.org/data/definitions/242.html). It reports functions that cannot be called safely, because they write input of any length into a buffer they are given no size for:

- `gets()`, `std::gets()`, `_getws()`, `getws()`, `_getts()` and `getwd()`;
- C++ extraction with `operator>>` from `cin`, `wcin` or a variable declared as an `istream`, `ifstream`, `istringstream`, `stringstream` or `fstream` into a `char` or `wchar_t` array with no `std::setw()` before it (unbounded before C++20).

## Why this matters

There is no input for which these calls are safe: the caller cannot tell them how big the buffer is, so a long line overflows it and overwrites the stack. `gets()` was removed from C11 and C++14 for this reason.

Not reported: the bounded replacements (`fgets`, `gets_s`, `fgetws`, `getcwd`), method calls with the same name (`conn.gets()`, `p->gets()`), prototypes, `#define`/`#undef` lines, extraction into `std::string` or numbers, shift expressions, comments and string literals. `atoi`/`atol`/`atof` are not inherently dangerous (they are unsafe only on unvalidated input, CWE-676) and are not reported here.

## What gets flagged

```c
char name[64];
gets(name);
getwd(dir);
```

```cpp
char user[32];
std::cin >> user;
```

## Remediation

```c
if (fgets(name, sizeof name, stdin) == NULL)
    return 1;
getcwd(dir, sizeof dir);
```

```cpp
std::string user;
std::cin >> user;                    // or: std::cin >> std::setw(sizeof user) >> user;
```

## References

- [CWE-242: Use of Inherently Dangerous Function](https://cwe.mitre.org/data/definitions/242.html)
- [CWE-120: Buffer Copy without Checking Size of Input](https://cwe.mitre.org/data/definitions/120.html)

## 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-242
// nosec vnx-242
```

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