lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <20260105092729.386bac33@pumpkin>
Date: Mon, 5 Jan 2026 09:27:29 +0000
From: David Laight <david.laight.linux@...il.com>
To: Daniel Palmer <daniel@...ngy.jp>
Cc: w@....eu, linux@...ssschuh.net, linux-kernel@...r.kernel.org
Subject: Re: [PATCH 1/2] tools/nolibc: Add fread() to stdio.h

On Mon, 5 Jan 2026 09:54:10 +0900
Daniel Palmer <daniel@...ngy.jp> wrote:

> Hi David,
> 
> On Mon, 5 Jan 2026 at 03:34, David Laight <david.laight.linux@...il.com> wrote:
> > > +static __attribute__((unused))
> > > +int _fread(void *buf, size_t size, FILE *stream)
> > > +{
> > > +     ssize_t ret;
> > > +     int fd = fileno(stream);
> > > +
> > > +     while (size) {
> > > +             ret = read(fd, buf, size);
> > > +             if (ret <= 0)
> > > +                     return EOF;  
> >
> > You need to return a partial length if some data was read before EOF.  
> 
> According to the man page:
> 
>   On success, fread() and fwrite() return the number of items read
>   or written.  This number equals the number of bytes transferred
>   only when size is 1.  If an error occurs, or the end of the file
>   is reached, the return value is a short item count (or zero).
> 
> So I think the current logic is correct? If you have a file that has a
> length that is not a multiple of the item size and try to read more
> items than possible (i.e. you have 3 bytes in the file, you try to
> read 2 2 byte items) we read and count the items that are possible,
> the partial data is read but the read loop returns EOF, the partial
> item isn't counted and fread() returns the number of fully read items.

But you've deleted the partial bytes from the input.
I'm sure that isn't right.
Normally a FILE is buffered and the bytes are saved for the next read.
Remember you can be reading from a pipe that is being written using
'block buffering' - so it is valid for only a partial 'item' be read.
(I'm sure non-blocking IO is also valid...)

> _fread() could return the amount that was partially read but fread()
> is only checking for non-zero so there wouldn't be any difference.
> 
> Maybe I'm missing something?

What you are doing is equivalent to a long read and then dividing
the number of bytes read by the item size.
So you might as well do a single read system call.

	David

> 
> Cheers,
> 
> Daniel


Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ