This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
Re: Implement C11 annex K?
- From: Russ Allbery <eagle at eyrie dot org>
- To: <libc-alpha at sourceware dot org>
- Date: Wed, 13 Aug 2014 18:01:23 -0700
- Subject: Re: Implement C11 annex K?
- Authentication-results: sourceware.org; auth=none
- References: <E1XHe8v-0004Ur-Hp at rmm6prod02 dot runbox dot com> <Pine dot LNX dot 4 dot 64 dot 1408132054090 dot 16622 at digraph dot polyomino dot org dot uk> <53EBD7D9 dot 1040008 at cs dot ucla dot edu> <20140813213520 dot GQ12888 at brightrain dot aerifal dot cx> <53EBEACD dot 3070000 at googlemail dot com> <87k36cc559 dot fsf at windlord dot stanford dot edu> <Pine dot LNX dot 4 dot 64 dot 1408140047410 dot 16622 at digraph dot polyomino dot org dot uk>
"Joseph S. Myers" <joseph@codesourcery.com> writes:
> On Wed, 13 Aug 2014, Russ Allbery wrote:
>> If the answer is "use strcpy and strcat because this code is provably
>> correct with them," I guess I understand your position, but if I somehow
> stpcpy is better than strcpy and strcat here; repeated strcat, or strlcat
> as in your code, has quadratic overhead repeatedly looking for the end of
> the string being built up, whereas stpcpy returns the end pointer for use
> in the next stpcpy call.
Arguably, yes, but the code is not performance-critical, and I find the
stpcpy code (slightly) harder to read, possibly just because it's less
familiar. No one is ever going to notice that quadratic factor here.
Either way, though, I would prefer the belt and suspenders of the
additional check on the length calculation logic.
>> messed up the length calculation logic, I would prefer to truncate the
> It's not obvious what a safer version of the stpcpy code would look
> like, and the length calculation logic fails to allow for the sum of the
> lengths exceeding SIZE_MAX (e.g. from a long separator, or from several
> pointers to the same long string in the vector) (so resulting in buffer
> overflows from any version that doesn't check each write fits in the
> remaining space). (This size calculation as written could feasibly
> overflow even on a 64-bit system; many such overflows are only realistic
> on 32-bit systems.)
Yes, thank you, that's a good point. I need to fix that. But I think
that's orthogonal to the strlcpy vs. strcpy/stpcpy debate here. One has
to do the size calculation in roughly the same way with either approach,
and the overflow check applies to either approach, I believe.
--
Russ Allbery (eagle@eyrie.org) <http://www.eyrie.org/~eagle/>