+2024-06-20 Paul Eggert <eggert@re>
+
+ sigsegv: avoid unlikely undefined behavior
+ Problem found by Coverity for diffutils and reported by Wasser Mai in:
+ https://bugs.gnu.org/71535
+ * lib/stackvma.c (rof_open) [__linux__ || __FreeBSD__ || etc]:
+ Don’t assume result of ‘read’ fits in int.
+ Avoid undefined behavior if ‘n + MIN_LEFTOVER’ would overflow.
+ Also, move a test to be after an (n == 0) test, to help the compiler.
+
2024-06-19 Bruno Haible <bruno@clisp.org>
vasnprintf, u*-asnprintf tests: Add test of huge %ls arguments.
/* Attempt to read the contents in a single system call. */
if (size > MIN_LEFTOVER)
{
- int n = read (fd, rof->buffer, size);
+ ssize_t n = read (fd, rof->buffer, size);
if (n < 0 && errno == EINTR)
goto retry;
# if defined __DragonFly__
if (n <= 0)
/* Empty file. */
goto fail1;
- if (n + MIN_LEFTOVER <= size)
+ if (MIN_LEFTOVER <= size - n)
{
/* The buffer was sufficiently large. */
rof->filled = n;
if (n < 0)
/* Some error. */
goto fail1;
- if (n + MIN_LEFTOVER > size - rof->filled)
- /* Allocate a larger buffer. */
- break;
if (n == 0)
{
/* Reached the end of file. */
close (fd);
return 0;
}
+ if (size - rof->filled - n < MIN_LEFTOVER)
+ /* Allocate a larger buffer. */
+ break;
rof->filled += n;
}
# else