Re: [PATCH] Documentation: Fix NUL termination of strncpy

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, 17 Nov 2009 12:25:03 GMT, Pádraig Brady said:

> You need to explicitly NUL terminate strncpy():
> http://www.pixelbeat.org/programming/gcc/string_buffers.html

Umm. Actually, no.  It's *nice* if you explicitly do it.  However, it's
not strictly *required*. Consider this code:

struct a {
	int b, c;
	char d[20];
}

bzero(a,sizeof(a)); /* voila! a->d[19] is now a \0 */

Perfectly valid and we do it all the time. Your referenced web page comments
that using memset() or bzero() is inefficient - I wonder if they actually
*tested* the difference between using one memset() or bzero() on a large
structure compared to lots and lots of explicit initialization statements
(i-cache issues probably most important there). The downside is that there's a
greater chance of some programmer refactoring code and bollixing it up. (The
astute reader will remember a short thread about this not too long ago.. :)

And if you're truly careful and always use the strn- variants of the string
functions, it's actually possible to *not* null-terminate the strings.  But
it results in very brittle code and makes Baby Andrew cry, mostly because when
somebody adds a printf() to debug the brittle code, it suddenly becomes even
more brittle... :) 

Attachment: pgpzl8n9zBHCm.pgp
Description: PGP signature


[Index of Archives]     [Kernel Newbies]     [Security]     [Netfilter]     [Bugtraq]     [Linux FS]     [Yosemite Forum]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Samba]     [Video 4 Linux]     [Device Mapper]     [Linux Resources]

  Powered by Linux