Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date: Fri, 24 Feb 2023 09:07:41 -0500
From: Rich Felker <>
To: NRK <>
Cc:, Tamir Duberstein <>
Subject: Re: undefined behavior in fread.c

On Fri, Feb 24, 2023 at 07:55:11PM +0600, NRK wrote:
> On Fri, Feb 24, 2023 at 08:34:14AM -0500, Rich Felker wrote:
> > Is there any indication that passing NULL as the first argument to
> > fread is not itself undefined?
> C99 says the following:
> | If size or nmemb is zero, fread returns zero and the contents of the
> | array and the state of the stream remain unchanged.
> It doesn't explicitly mention weather stream can be NULL or not in case
> of 0 size/nmemb - but regardless, is there any actual reason for not
> returning early?
> The following should be OK as far as I see:
> -	if (!size) nmemb = 0;
> +	if (!size || !nmemb) return 0;
> or `if (!len) return 0;` could also work if multiplication overflow is
> not a concern.

stdio functions are required (by POSIX) to behave as if they take a
mutex on the FILE. If fread with a length of zero makes forward
progress when another thread holds the lock, this is non-conforming.


Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.