Re: [RFC PATCH v4 03/26] pkt-line: Make packet_read_line easier to debug
- From
David Brown <davidb@quicinc.com>
- Date
- Oct 30, 2009, 19:31 UTC
- Message-ID
- <20091030193108.GA30397@huya.quicinc.com>
- In-Reply-To
- <20091030191239.GF10505@spearce.org>
On Fri, Oct 30, 2009 at 12:12:40PM -0700, Shawn O. Pearce wrote:
Show 8 quoted lines
> > If you really just want to initialize to zero, using
> >
> > char linelen[5] = { 0 };
>
> Bleh, I find that has hard to grok as what we have now. Perhaps my
> understanding of the relevant standards is incomplete, but I'd read
> that as linelen[0] = 0, but the other 4 positions are undefined
> and may be not be initialized.C isn't that into undefined things. In this particular case, all remaining values will be initialized to zero.
Show 6 quoted lines
> I actually considered this one, but again I wasn't clear what would > happen in the standard C library when we fed a string that wasn't > actually NUL terminated. Is the library permitted to call strlen() > before formatting? If so strlen() could SIGSEGV if we are unlucky > and no NUL is present between our string and the end of the assigned > memory region.
The linux manpage says "If a precision is given, no null byte need be present". This text is copied verbatim out of the Posix specification, so a C library that failed to handle this would be non-compliant.
I think the %.4s is clearest, and is, in fact, somewhat idiomatic.
David