Secure Coding in C · advanced · ~10 min

Format string mistakes

Never pass user input as a format string.

Lesson

printf(user_input) is a classic vulnerability: if the user types %x%x%x%n, printf reads (and possibly writes) past the call's actual arguments.

Always pass user data as an argument to a fixed format: printf("%s", user_input). Compile with -Wformat -Wformat-security to catch the mistake.

Code examples

// BAD
printf(user_input);

// GOOD
printf("%s", user_input);