Hi

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

G_DEFINE_AUTOPTR_CLEANUP_FUNC(GError, g_error_free)
it will call g_error_free() at the end of the scope if the variable is != NULL.

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

I am not sure if it's a good idea to printf() here, this may be
confused with the program output being run.

However, having a good errno value is probably necessary. And here,
glib doesn't guarantee that for this function, since it relies on
GFileError. So you would need something like g_file_error_to_errno()
which doesn't exist...



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


Reply via email to