Re: Fix some error handling for read() and errno

From: Kyotaro HORIGUCHI <horiguchi(dot)kyotaro(at)lab(dot)ntt(dot)co(dot)jp>
To: michael(at)paquier(dot)xyz
Cc: pgsql-hackers(at)postgresql(dot)org, tgl(at)sss(dot)pgh(dot)pa(dot)us, magnus(at)hagander(dot)net, hlinnaka(at)iki(dot)fi
Subject: Re: Fix some error handling for read() and errno
Date: 2018-05-22 07:51:00
Message-ID: 20180522.165100.182725143.horiguchi.kyotaro@lab.ntt.co.jp
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hello.

At Sun, 20 May 2018 09:05:22 +0900, Michael Paquier <michael(at)paquier(dot)xyz> wrote in <20180520000522(dot)GB1603(at)paquier(dot)xyz>
> Hi all,
>
> This is basically a new thread after what has been discussed for
> pg_controldata with its error handling for read():
> https://www.postgresql.org/message-id/CABUevEx8ZRV5Ut_FvP2etXiQppx3xVzm7oOaV3AcdHxX81Yt8Q%40mail.gmail.com
>
> While reviewing the core code, I have noticed similar weird error
> handling for read(). At the same time, some of those places may use an
> incorrect errno, as an error is invoked using an errno which may be
> overwritten by another system call. I found a funny one in slru.c,
> for which I have added a note in the patch. I don't think that this is
> worth addressing with more facility, thoughts are welcome.
>
> Attached is a patch addressing the issues I found.

I see the same issue in snapbuild.c(4 places).

| readBytes = read(fd, &ondisk, SnapBuildOnDiskConstantSize);
| pgstat_report_wait_end();
| if (readBytes != SnapBuildOnDiskConstantSize)
| {
| CloseTransientFile(fd);
| ereport(ERROR,
| (errcode_for_file_access(),
| errmsg("could not read file \"%s\", read %d of %d: %m",
| path, readBytes, (int) SnapBuildOnDiskConstantSize)));
| }

and walsender.c (2 places)

| if (nread <= 0)
| ereport(ERROR,
| (errcode_for_file_access(),
| errmsg("could not read file \"%s\": %m",
| path)));

and pg_receivewal.c

| if (read(fd, (char *) buf, sizeof(buf)) != sizeof(buf))
| {
| fprintf(stderr, _("%s: could not read compressed file \"%s\": %s\n"),
| progname, fullpath, strerror(errno));

pg_waldump.c

| if (readbytes <= 0)
...
| fatal_error("could not read from log file %s, offset %u, length %d: %s",
| fname, sendOff, segbytes, strerror(err));

A bit differnt issue, but in pg_waldump.c, search_directory can
check uninitialized errno when read returns a non-zero value.

regards.

--
Kyotaro Horiguchi
NTT Open Source Software Center

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Kyotaro HORIGUCHI 2018-05-22 08:11:44 Re: perl checking
Previous Message Craig Ringer 2018-05-22 07:50:00 Re: PostgreSQL and Homomorphic Encryption