# C/C++ Secure Coding

| Language | Insecure Function               | Vulnerability                                   | Scenario                                                                           | Remediation/Secure Function                                |
| -------- | ------------------------------- | ----------------------------------------------- | ---------------------------------------------------------------------------------- | ---------------------------------------------------------- |
| C        | `strcpy`                        | Buffer Overflow                                 | A software application takes user input and stores it in a fixed-size buffer.      | `strncpy` with bounds checking and null termination        |
| C        | `gets`                          | Stack Buffer Overflow                           | A program reads user input into a stack buffer without bounds checking.            | `fgets` with bounds checking                               |
| C        | `malloc` with unchecked bounds  | Heap Buffer Overflow                            | A program allocates memory for user data but does not check the size of the input. | Ensure loop limits are correct and check allocation size   |
| C        | `strcpy`                        | Stack Buffer Overflow leading to Stack Smashing | A web server takes input from a request and copies it into a local stack buffer.   | `strncpy` with bounds checking and null termination        |
| C        | `system`                        | Command Injection                               | An application runs system commands based on user input.                           | Validate input, use `snprintf` and validate input securely |
| C        | Unchecked arithmetic operations | Integer Overflow                                | A program performs arithmetic operations without checking for overflow.            | Check arithmetic bounds and validate inputs                |
| C        | `printf`                        | Format String Vulnerability                     | A program prints user input directly using `printf`.                               | Use format specifiers, e.g., `printf("%s", user_input)`    |

#### String Handling Vulnerabilities

| Language | Insecure Function                  | Vulnerability   | Scenario                                                                      | Remediation/Secure Function                         |
| -------- | ---------------------------------- | --------------- | ----------------------------------------------------------------------------- | --------------------------------------------------- |
| C        | `strcpy`                           | Buffer Overflow | A software application takes user input and stores it in a fixed-size buffer. | `strncpy` with bounds checking and null termination |
| C        | `strncpy` without null termination | Buffer Overflow | A program uses `strncpy` but does not ensure null termination.                | Ensure null termination after `strncpy`             |

#### Buffer Overflow

| Language | Insecure Function        | Vulnerability         | Scenario                                                                 | Remediation/Secure Function                           |
| -------- | ------------------------ | --------------------- | ------------------------------------------------------------------------ | ----------------------------------------------------- |
| C        | `gets`                   | Stack Buffer Overflow | A program reads user input into a stack buffer without bounds checking.  | `fgets` with bounds checking                          |
| C        | Manual memory management | Heap Buffer Overflow  | A program does not properly manage dynamic memory allocation and bounds. | Use safe memory allocation functions and check bounds |

#### Integer Security

| Language | Insecure Function               | Vulnerability    | Scenario                                                                | Remediation/Secure Function                 |
| -------- | ------------------------------- | ---------------- | ----------------------------------------------------------------------- | ------------------------------------------- |
| C        | Unchecked arithmetic operations | Integer Overflow | A program performs arithmetic operations without checking for overflow. | Check arithmetic bounds and validate inputs |

#### Code Injection

| Language | Insecure Function | Vulnerability     | Scenario                                                 | Remediation/Secure Function                                |
| -------- | ----------------- | ----------------- | -------------------------------------------------------- | ---------------------------------------------------------- |
| C        | `system`          | Command Injection | An application runs system commands based on user input. | Validate input, use `snprintf` and validate input securely |

#### Exploiting Formatted Output Functions

| Language | Insecure Function | Vulnerability               | Scenario                                             | Remediation/Secure Function                             |
| -------- | ----------------- | --------------------------- | ---------------------------------------------------- | ------------------------------------------------------- |
| C        | `printf`          | Format String Vulnerability | A program prints user input directly using `printf`. | Use format specifiers, e.g., `printf("%s", user_input)` |


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://playbook.sidthoviti.com/devsecops/secure-coding/c-c++-secure-coding.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
