* src/base/ftsystem.c (ft_ansi_stream_io): Avoid undefined behaviour.

Also short-circuit on `offset` to avoid checking `count` a second time when
`ft_ansi_stream_io` is used for reading.

Per ISO/IEC 9899:

  If an argument to a function has an invalid value (such as a value outside
  the domain of the function, or a pointer outside the address space of the
  program, or a null pointer, or apointer to non-modifiable storage when the
  corresponding parameter is not const-qualified) or a type (after
  promotion) not expected by a function with variable number of arguments,
  the behavior is undefined.  If a function argument is described as being
  an array, the pointer actually passed to the function shall have a value
  such that all address computations and accesses to objects (that would be
  valid if the pointer did point to the first element of such an array) are
  in fact valid.

Per IEEE Std 1003.1:

  size_t fread(void *restrict ptr, size_t size, size_t nitems,
               FILE *restrict stream);

  The `fread` function shall read into the array pointed to by `ptr` up to
  `nitems` elements whose size is specified by `size` in bytes, from the
  stream pointed to by `stream`.

Since the first argument to `fread` is described as being an array, its
behavior is undefined when that argument is a null pointer.

Per the documentation on `ft_ansi_stream_io`:

  If `count' is zero (this is, the function is used for seeking), a non-zero
  return value indicates an error.

Thus the intent is clear, and the call to `fread` can be skipped, avoiding
undefined behaviour.
This commit is contained in:
Tamir Duberstein 2023-02-24 11:48:48 -05:00 committed by Werner Lemberg
parent 3f01161ff2
commit 3f2ac7d890
1 changed files with 6 additions and 1 deletions

View File

@ -219,7 +219,7 @@
FT_FILE* file;
if ( !count && offset > stream->size )
if ( offset > stream->size && !count )
return 1;
file = STREAM_FILE( stream );
@ -227,6 +227,11 @@
if ( stream->pos != offset )
ft_fseek( file, (long)offset, SEEK_SET );
/* Avoid calling `fread` with `buffer=NULL` and `count=0`, */
/* which is undefined behaviour. */
if ( !count )
return 0;
return (unsigned long)ft_fread( buffer, 1, count, file );
}