Parse, Don’t Validate AKA Some C Safety Tips
“A good programmer is someone who looks both ways before crossing a one-way street.” – Doug Linder
Posted by Lelanthran
2025-03-27
If you’ve read the original post on “Parse, Don’t Validate” you may have noticed that it focuses primarily on conceptual correctness. Here, I’ll build on that by showing how this technique can be used outside of niche academic languages by demonstrating it in a language that is as practical as it is dangerous - C.
In this blog post you will see three techniques of reducing the risk of exploitable errors in C.
The basic idea is this:
Data Comes Into Your System. Your System Processes It.
Your first instinct, when your system receives as input an email address (for example), is to perform validateEmail(untrustedInput) and then pass the validated string further into the depths of the system for usage.
The problem is that other code deep within the rest of the system is going to also do some sort of validation on the string they just got. Every single function deep within the bowels of the system will still need to validate the input before processing it.
... continue reading