# warning: format string is not a string literal (potentially insecure) [-Wformat-security]

**Code:** `Wformat-security` · **Category:** C/C++ · **Seen on:** C/C++, Clang, GCC

A `printf`-style function was given a variable as its format string.

## Meaning

If the string comes from user input it can contain `%n` or `%s` and read or write memory. The fix is to pass a literal format and the variable as an argument.

## Common causes

- `printf(userString)`
- `syslog(LOG_ERR, msg)` with a variable format
- Logging wrappers forwarding a runtime format

## Quick fix

1. Use `printf("%s", s)`
2. Use `puts(s)` or `std::cout << s`
3. Mark wrapper functions with `__attribute__((format(printf, 1, 2)))`

## Detailed fix by platform

### GCC/Clang

1. Keep `-Wformat -Wformat-security` enabled

## Code examples

### Format-string fix

```c
printf(user_input);            // warning: format string is not a string literal
printf("%s", user_input);      // safe
```

## How to diagnose

1. **Source** — Where does the string come from?
2. **Literal** — Can it be a constant?
3. **Wrapper** — Format attribute?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/clang-warning-shadow-and-format-security)
