[Asterisk-Dev] Is anyone thinking anymore?

Peter Svensson psvasterisk at psv.nu
Sun Jul 25 02:41:42 MST 2004


On Sun, 25 Jul 2004, Karl Brose wrote:

> In recent CVS updates we can observe code changes (strangely called 
> "source audits") such as those quoted below.
> The old code is perfectly legal, stable, safe, technically sound, and 
> readable.

Both of the changes below appear to be what one can call defensive 
programming. Used consistently they reduce the risk of a future change 
introducing an unintended behaviour such as a buffer overflow. For it to 
have the best effect the rules the programmers are to follow need to be 
available. 

It is a trade-off between execution speed and the future cost of 
maintanence. In C you as a programmer get somewhat less support for this 
from the compiler than in some other languages. 

> -       char ilimits[40];
> -       char olimits[40];
> +       char ilimits[40] = "";
> +       char olimits[40] = "";

[snip]

> -                       strcpy(ilimits, "N/A");
> +                       strncpy(ilimits, "N/A", sizeof(ilimits) - 1);
[snip]
> -                       strcpy(olimits, "N/A");
> +                       strncpy(olimits, "N/A", sizeof(olimits) - 1);

Peter
--
Peter Svensson      ! Pgp key available by finger, fingerprint:
<petersv at psv.nu>    ! 8A E9 20 98 C1 FF 43 E3  07 FD B9 0A 80 72 70 AF
------------------------------------------------------------------------
Remember, Luke, your source will be with you... always...





More information about the asterisk-dev mailing list