Secure Coding in C · intermediate · ~15 min
Classify a candidate string as 'has SQL metacharacters' or 'safe-looking' — the validate-don't-build-strings half of SQL injection defence.
Walk the input once, look for single-quote, --, /*, ;, or case-insensitive UNION SELECT. Return 1 if any present.
Parameterised queries are the proper fix. But a metacharacter detector on the input layer adds defence in depth and gives you signal in your logs.
The real defence against SQL injection is parameterised queries — never build SQL strings from user input. But on the way to that, defenders write input validators that flag suspicious metacharacters early, before the input even gets to the query layer.
This exercise teaches the metacharacter detector. The proper cure (prepared statements) is mentioned in the lesson but practiced separately.
') — the classic injection delimiter--, /*);)UNION SELECT, case-insensitive)Implement int has_sqli_markers(const char *input) returning:
0 — input contains no suspicious patterns1 — input contains at least one suspicious patternThe function is a flagger, not a sanitiser. Real defence is parameterised queries — this just tells the application "this input deserves a second look".
UNION SELECT. Use a tiny inline
case-folding helper.One-pass string walk. Flag, don't sanitise. The proper cure is prepared statements.