]> git.ipfire.org Git - thirdparty/coreutils.git/commit
head: fix overflows in elide_tail_bytes_pipe
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 11 Aug 2024 05:19:17 +0000 (22:19 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 11 Aug 2024 05:19:44 +0000 (22:19 -0700)
commit8fe800a06e50be3c905ab1694a2d1bfd6e70be42
treeb271b6d4b0369a970d725271d06ad1d0415ccbb8
parent91657204c129b5fa059052aa59d85bacdfaee3c0
head: fix overflows in elide_tail_bytes_pipe

Not clear that the overflows could be exploited,
but they made the code confusing.
* src/head.c (elide_tail_bytes_pipe): Don’t convert uintmax_t
to size_t first thing; wait until it’s known the value will fit,
and then use idx_t rather than size_t to prefer signed types.
Prefer idx_t in nearby code, too.
Rename locals n_elide_0 to n_elide (for consistency elsewhere)
and n_elide to in_elide.
Remove bogus (SIZE_MAX < n_elide + READ_BUFSIZE) test;
in the typical case where n_elide’s type was the same as
that of SIZE_MAX, the test never succeeded, and in the
less-common case where n_elide was wider than size_t,
the addition could silently overflow, causing the test
to fail when it should succeed.  The test is not needed anyway now.
Add static asserts to document code assumptions.
Redo the ! (n_elide <= HEAD_TAIL_PIPE_BYTECOUNT_THRESHOLD) case
so that it works with enormous values of n_elide even on
32-bit platforms; for example, n_bufs is now uintmax_t not size_t.
Simplify by using xpalloc instead of by-hand code.
Remove bogus ‘if (rem)’ test, as rem is always nonzero.
src/head.c