Re: [PATCH 03/29] modpost: add read_text_file() and get_line() helpers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Tue, May 19, 2020 at 7:21 PM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote:
>
> On Sun, May 17, 2020 at 06:48:33PM +0900, Masahiro Yamada wrote:
>
> > +char *read_text_file(const char *filename)
> > +{
> > +     struct stat st;
> > +     int fd;
> > +     char *buf;
> > +
> > +     fd = open(filename, O_RDONLY);
> > +     if (fd < 0)
> > +             return NULL;
> > +
> > +     if (fstat(fd, &st) < 0)
> > +             return NULL;
> > +
> > +     buf = NOFAIL(malloc(st.st_size + 1));
> > +
> > +     if (read(fd, buf, st.st_size) != st.st_size) {
>
> Is this sensible coding ? I've always been taught read() can return
> early/short for a number of reasons and we must not assume this is an
> error.
>
> The 'normal' way to read a file is something like:
>
>         for (;;) {
>                 ssize_t ret = read(fd, buf + size, st.st_size - size);
>                 if (ret < 0) {
>                         free(buf);
>                         buf = NULL;
>                         goto close;
>                 }
>                 if (!ret)
>                         break;
>
>                 size += ret;
>         }
>
> > +             free(buf);
> > +             buf = NULL;
> > +             goto close;
> > +     }
> > +     buf[st.st_size] = '\0';
> > +close:
> > +     close(fd);
> > +
> > +     return buf;
> > +}


In theory, I think yes.

But, is it necessary when we know
it is reading a regular file?



The specification [1] says this:

"The value returned may be less than nbyte if the number of bytes
left in the file is less than nbyte, if the read() request was
interrupted by a signal, or if the file is a pipe or FIFO or
special file and has fewer than nbyte bytes immediately available
for reading."


This case does not meet any of 'if ...' parts.

[1] https://pubs.opengroup.org/onlinepubs/000095399/functions/read.html


-- 
Best Regards
Masahiro Yamada



[Index of Archives]     [Linux&nblp;USB Development]     [Linux Media]     [Video for Linux]     [Linux Audio Users]     [Yosemite Secrets]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux