Defensive programming: Difference between revisions

Content deleted Content added
Added the part about how gets() is OK if you check strlen()
+copyedit
Line 8:
<!-- Please expand this article. These random notes should be changed to a more coherent article. -->
 
* One of the most common problems is unchecked use of constant-size structures and functions for dynamic-size data (the [[buffer overflow]] problem). This is especially common for [[string]] data in [[C programming language|C]]. C library functions like <tt>gets</tt> should never be used since the maximum size of the input buffer is not passed as an argument. (Actually, <tt>gets()</tt> can be made safe, inprovided certainthe situations,programmer checks to make sure suchthat ashe whereknows the size of the source string wasby verified bycalling <tt>strlen()</tt> or a similar function before the call tocalling <tt>gets</tt>.) C library functions like <tt>scanf</tt> can be used safely, but require the programmer to take care with the selection of safe format strings, by sanitising it before using it.
* Never make code more complex than necessary. Complexity breeds bugs, including security problems.
* Either leave the code available to everyone on the Net (see [[Free software]] or [[Open Source Definition]]) or hire someone who will perform a software [[security audit]].