[clang-tools-extra] [clang-tidy] New bugprone-unsafe-format-string check (PR #168691)

DonĂ¡t Nagy via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 10 12:46:24 PST 2026


================
@@ -0,0 +1,111 @@
+```{title} clang-tidy - bugprone-unsafe-format-string
+```
+
+# bugprone-unsafe-format-string
+
+Detects usage of vulnerable `printf` and `scanf`-like format string
+functions with unbounded `%s` specifiers that can cause buffer overflows.
+
+The check identifies calls to format string functions like `sprintf`, `scanf`,
+and their variants that use `%s` format specifiers without proper limits.
+This can lead to buffer overflow vulnerabilities
+when the input string is longer than the destination buffer.
+
+## Format Specifier Behavior
+
+The check distinguishes between different function families:
+
+**scanf family functions**: Field width limits input length
+- `%s` - unsafe (no limit)
+- `%99s` - safe (reads at most 99 characters)
+
+**sprintf family functions**: Precision limits output length
+- `%s` - unsafe (no limit)
+- `%99s` - unsafe (minimum width, no maximum)
+- `%.99s` - safe (outputs at most 99 characters)
+- `%10.99s` - safe (minimum 10 chars, maximum 99 chars)
+
+## Examples
+
+```c
+char buffer[100];
+const char* input = "user input";
+
+// Unsafe sprintf usage
+sprintf(buffer, "%s", input);      // No limit
+sprintf(buffer, "%99s", input);    // Field width is minimum, not maximum
+
+// Safe sprintf usage
+sprintf(buffer, "%.99s", input);   // Precision limits to 99 chars
+sprintf(buffer, "%10.99s", input); // Min 10, max 99 chars
+
+// Unsafe scanf usage
+scanf("%s", buffer);               // No limit
+
+// Safe scanf usage
+scanf("%99s", buffer);             // Field width limits to 99 chars
+
+// Safe alternative: use safer functions
+snprintf(buffer, sizeof(buffer), "%s", input);
+```
+
+## Checked Functions
+
+The check detects unsafe format strings in these functions:
+
+**sprintf family** (precision `.N` provides safety):
+- `sprintf`, `vsprintf`
+
+**scanf family** (field width `N` provides safety):
+- `scanf`, `fscanf`, `sscanf`
+- `vscanf`, `vfscanf`, `vsscanf`
+- `wscanf`, `fwscanf`, `swscanf`
+- `vwscanf`, `vfwscanf`, `vswscanf`
+
+## Options
+
+```{option} CustomPrintfFunctions
+The user can specify own printf-like functions with dangerous format string
+parameter. Default is an empty string.
+```
+
+```{option} CustomScanfFunctions
+The user can specify own scanf-like functions with dangerous format string
+parameter. Default is an empty string.
+```
+
+Both options have the following format:
+
+```
+bugprone-unsafe-format-string.CustomPrintfFunctions="
+  functionRegex1, format-string-position;
+  functionRegex2, format-string-position;
+  ...
----------------
NagyDonat wrote:

I prefer the current syntax (with commas).

The checks that use `=` use it because in their config it is natural to say that an abbreviation is _equal to_ the corresponding full name; but in this check we can't say that the function name regex is "equal to" the position of the format argument.

There is also some tradition behind using the equals sign in `name=value` mappings, but in those application the `name` is pretty much always an identifier-like plain string; while here we have regular expressions. If I would see the `function=position` syntax, I would be very surprised to see that the function name can be a regex.

However, this is just my personal intuition, if others think `=` is more natural, I can accept it. 

https://github.com/llvm/llvm-project/pull/168691


More information about the cfe-commits mailing list