Printf Variable Interpolation High

The code directly includes variables in the printf format string, which can lead to unexpected behavior if the variable contains format specifiers or escape sequences. To fix this, use format specifiers like' %s' in the format string and pass variables as separate arguments to printf.

Detector ID
shell/printf-variable-interpolation@v1.0
Category
Common Weakness Enumeration (CWE) external icon
-
Tags
-

Noncompliant example

1
2# Noncompliant: Variables in format string can lead to unexpected behavior.
3name="Alice"
4printf "Welcome, $name!\n"

Compliant example

1
2# Compliant: Safely handles variables, even if they contain format specifiers.
3name="Alice"
4printf "Welcome, %s!\n" "$name"