From: Paul Eggert <eggert@cs.ucla.edu>
To: Adhemerval Zanella <adhemerval.zanella@linaro.org>,
Andreas Schwab <schwab@suse.de>
Cc: libc-alpha@sourceware.org
Subject: Re: [PATCH v2] libio: Flush stream at freopen (BZ#21037)
Date: Thu, 14 Jun 2018 16:43:00 -0000 [thread overview]
Message-ID: <2df65bd4-8dfc-187a-c917-87da4510fd15@cs.ucla.edu> (raw)
In-Reply-To: <25a001ee-301f-1af5-20ed-27b883500f04@linaro.org>
On 06/14/2018 08:01 AM, Adhemerval Zanella wrote:
> + char fdfilename[30];
The magic number 30 should be turned into a named constant defined in
fd_to_filename.h, to help prevent future mistakes. Once that is done,
you can change the signature of fd_to_filename to not pass the size, and
to require the caller to pass an array of at least size 30, so that
fd_to_filename need not check for buffer overflow (see below for more on
this).
> + const char *gfilename;
> + if (filename == NULL && fd >= 0)
> + gfilename = fd_to_filename (fd, fdfilename, sizeof fdfilename)
> + ? fdfilename : NULL;
> + else
> + gfilename = filename;
Cleaner would be:
const char *gfilename
= filename != NULL ? filename : fd_to_filename (fd, fdfilename);
That is, let fd_to_filename worry about what to do with negative fd, and
have it return fdfilename or NULL, and don't pass the size (which should
be that magic number regardless).
> -static inline const char *
> -fd_to_filename (int fd)
> +static inline bool
> +fd_to_filename (int fd, char *buf, size_t len)
> {
> - char *ret = malloc (30);
> + __snprintf (buf, len, "/proc/self/fd/%d", fd);
>
> - if (ret != NULL)
> - {
> - struct stat64 st;
> -
> - *_fitoa_word (fd, __stpcpy (ret, "/proc/self/fd/"), 10, 0) = '\0';
> -
> - /* We must make sure the file exists. */
> - if (__lxstat64 (_STAT_VER, ret, &st) < 0)
> - {
> - /* /proc is not mounted or something else happened. Don't
> - return the file name. */
> - free (ret);
> - ret = NULL;
> - }
> - }
> - return ret;
> + /* We must make sure the file exists. */
> + if (__lxstat64 (_STAT_VER, buf, & (struct stat64) {}) < 0)
> + /* /proc is not mounted or something else happened. */
> + return false;
> + return true;
> }
The __snprintf would be quite wrong if the string did not fit. Again, I
suggest simply requiring the buffer to be long enough and not checking
its length, and sticking with stpcpy + _fitoa_word which should be more
efficient than __snprintf anyway (or if you prefer simplicity to speed,
just use sprintf).
The '& (struct stat64) {}' construct looks pretty but is less efficient
as it makes the compiler zero out the structure unnecessarily, so the
code should keep doing that struct the old-fashioned way.
next prev parent reply other threads:[~2018-06-14 16:43 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-06-13 21:33 Adhemerval Zanella
2018-06-14 13:20 ` Andreas Schwab
2018-06-14 15:02 ` Adhemerval Zanella
2018-06-14 16:43 ` Paul Eggert [this message]
2018-06-14 18:28 ` Adhemerval Zanella
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=2df65bd4-8dfc-187a-c917-87da4510fd15@cs.ucla.edu \
--to=eggert@cs.ucla.edu \
--cc=adhemerval.zanella@linaro.org \
--cc=libc-alpha@sourceware.org \
--cc=schwab@suse.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).