[lkml]   [2009]   [Nov]   [17]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRe: [PATCH] Documentation: Fix NUL termination of strncpy
On Tue, 17 Nov 2009 12:25:03 GMT, Pádraig Brady said:

> You need to explicitly NUL terminate strncpy():

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... :)
[unhandled content-type:application/pgp-signature]
 \ /
  Last update: 2009-11-17 17:43    [W:0.044 / U:4.664 seconds]
©2003-2018 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site