[Asterisk-Dev] Re: Is anyone thinking anymore?

Tony Mountifield tony at softins.clara.co.uk
Mon Jul 26 01:56:12 MST 2004

In article <002a01c472d7$7b0903e0$0500010a at mycomputer>,
Rob Gagnon <rob at networkip.net> wrote:
> Also caught, were many places, where strncpy() was already in place, but the
> dest parameter in the call was not previously nulled out.  IE:  if you use
> strncpy, you better make sure that last character is '\0' somehow.  Either
> memset() the thing, or initialize the string with char thing[40] ="";
> As you already know, the compiler will null out the whole string for you if
> you have the ="" in the definition.  If not, you get garbage, and need
> something like thing[sizeof(thing)-1] = '\0'; everywhere in the code, which
> is messy.

If efficiency becomes an issue (e.g. in a frequently-called function),
it's worth noting that thing[sizeof(thing)-1] = '\0'; is much faster
on a long array than initialising the whole array to nuls.

I agree ast_strncpy() would be a good idea and would enable the array
initialisations to be removed again.

Tony Mountifield
Work: tony at softins.co.uk - http://www.softins.co.uk
Play: tony at mountifield.org - http://tony.mountifield.org

More information about the asterisk-dev mailing list