All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Daniel P. Berrangé" <berrange@redhat.com>
To: "Philippe Mathieu-Daudé" <philmd@redhat.com>
Cc: "Marc-André Lureau" <marcandre.lureau@redhat.com>,
	"Riku Voipio" <riku.voipio@iki.fi>,
	qemu-devel@nongnu.org, "Laurent Vivier" <laurent@vivier.eu>
Subject: Re: [RFC PATCH] linux-user/syscall: Use g_file_open_tmp()
Date: Thu, 27 Feb 2020 10:31:18 +0000	[thread overview]
Message-ID: <20200227103118.GE1645630@redhat.com> (raw)
In-Reply-To: <20200227100621.21844-1-philmd@redhat.com>

On Thu, Feb 27, 2020 at 11:06:21AM +0100, Philippe Mathieu-Daudé wrote:
> Use GLib g_file_open_tmp() instead of getenv + snprintf + mkstemp.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@redhat.com>
> ---
> RFC because I'm not sure g_autoptr(GError) works this way.

It does work. Any struct that's defined in GLib has support for
g_autoptr(). If you aren't suyre though, just check for a
G_DEFINE_AUTOPTR_CLEANUP_FUNC() macro usage that refers to the
struct in question

$ grep -r 'G_DEFINE_AUTOPTR_CLEANUP_FUNC(GError' /usr/include/glib-2.0
/usr/include/glib-2.0/glib/glib-autocleanups.h:G_DEFINE_AUTOPTR_CLEANUP_FUNC(GError, g_error_free)

>  linux-user/syscall.c | 11 ++++-------
>  1 file changed, 4 insertions(+), 7 deletions(-)
> 
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index 8d27d10807..0e44969e16 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -7282,17 +7282,14 @@ static int do_openat(void *cpu_env, int dirfd, const char *pathname, int flags,
>      }
>  
>      if (fake_open->filename) {
> -        const char *tmpdir;
> -        char filename[PATH_MAX];
> +        g_autoptr(GError) gerr = NULL;
> +        g_autofree gchar *filename = NULL;
>          int fd, r;
>  
>          /* create temporary file to map stat to */
> -        tmpdir = getenv("TMPDIR");
> -        if (!tmpdir)
> -            tmpdir = "/tmp";
> -        snprintf(filename, sizeof(filename), "%s/qemu-open.XXXXXX", tmpdir);
> -        fd = mkstemp(filename);
> +        fd = g_file_open_tmp("qemu-open.XXXXXX", &filename, &gerr);

g_file_open_tmp, calls g_get_tmp_name, which calls
g_get_tmp_dir, which defaults to $TMPDIR, falling back
to /tmp. So we're using the same dir as before.

>          if (fd < 0) {
> +            fprintf(stderr, "Error opening %s: %s\n", filename, gerr->message);

This is wrong - the returned "filename" is only valid when
g_file_open_tmp succeeds. So the use of "filename" here
is likely a NULL. Given that the only place you use "filename"
is in the error path, and that's not valid, we can simply
eliminate it entirely, and pass NULL into g_file_open_tmp

>              return fd;
>          }
>          unlink(filename);

Regards,
Daniel
-- 
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|



  parent reply	other threads:[~2020-02-27 10:36 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-27 10:06 [RFC PATCH] linux-user/syscall: Use g_file_open_tmp() Philippe Mathieu-Daudé
2020-02-27 10:25 ` Marc-André Lureau
2020-02-27 10:31 ` Daniel P. Berrangé [this message]
2020-02-27 10:56   ` Ján Tomko

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=20200227103118.GE1645630@redhat.com \
    --to=berrange@redhat.com \
    --cc=laurent@vivier.eu \
    --cc=marcandre.lureau@redhat.com \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=riku.voipio@iki.fi \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.