From: Valdis.Kletnieks@vt.edu
To: "Pádraig Brady" <P@draigBrady.com>
Cc: Roel Kluin <roel.kluin@gmail.com>,
Randy Dunlap <rdunlap@xenotime.net>,
linux-doc@vger.kernel.org,
Andrew Morton <akpm@linux-foundation.org>,
LKML <linux-kernel@vger.kernel.org>,
David Wagner <daw@cs.berkeley.edu>
Subject: Re: [PATCH] Documentation: Fix NUL termination of strncpy
Date: Tue, 17 Nov 2009 11:41:05 -0500 [thread overview]
Message-ID: <6806.1258476065@turing-police.cc.vt.edu> (raw)
In-Reply-To: Your message of "Tue, 17 Nov 2009 12:25:03 GMT." <4B02961F.8020108@draigBrady.com>
[-- Attachment #1: Type: text/plain, Size: 1267 bytes --]
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 #2: Type: application/pgp-signature, Size: 227 bytes --]
prev parent reply other threads:[~2009-11-17 16:41 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-11-16 16:55 Roel Kluin
2009-11-16 18:52 ` Roel Kluin
2009-11-16 21:24 ` Roel Kluin
2009-11-17 12:25 ` Pádraig Brady
2009-11-17 16:41 ` Valdis.Kletnieks [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6806.1258476065@turing-police.cc.vt.edu \
--to=valdis.kletnieks@vt.edu \
--cc=P@draigBrady.com \
--cc=akpm@linux-foundation.org \
--cc=daw@cs.berkeley.edu \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rdunlap@xenotime.net \
--cc=roel.kluin@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®