qemu-devel
[Top][All Lists]
Advanced

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

Re: [Qemu-devel] [PATCH 08/22] linux-user: remove two unchecked uses of


From: Jim Meyering
Subject: Re: [Qemu-devel] [PATCH 08/22] linux-user: remove two unchecked uses of strdup
Date: Wed, 09 May 2012 16:01:03 +0200

Peter Maydell wrote:
> On 9 May 2012 14:42, Jim Meyering <address@hidden> wrote:
>> From 5dce6a0222252cdc2a45ada3e3e96a8c3ef4e90f Mon Sep 17 00:00:00 2001
>> From: Jim Meyering <address@hidden>
>> Date: Mon, 7 May 2012 18:34:26 +0200
>> Subject: [PATCH] linux-user: remove two unchecked uses of strdup
>>
>> Remove two uses of strdup (use g_path_get_basename instead),
>> and add a comment that this strncpy use is ok.
>>
>> Signed-off-by: Jim Meyering <address@hidden>
>> ---
>>  linux-user/elfload.c | 11 +++++++----
>>  1 file changed, 7 insertions(+), 4 deletions(-)
>>
>> diff --git a/linux-user/elfload.c b/linux-user/elfload.c
>> index f3b1552..977283e 100644
>> --- a/linux-user/elfload.c
>> +++ b/linux-user/elfload.c
>> @@ -2338,13 +2338,16 @@ static int fill_psinfo(struct
>> target_elf_prpsinfo *psinfo, const TaskState *ts)
>>     psinfo->pr_uid = getuid();
>>     psinfo->pr_gid = getgid();
>>
>> -    filename = strdup(ts->bprm->filename);
>> -    base_filename = strdup(basename(filename));
>> +    filename = ts->bprm->filename;
>> +    base_filename = g_path_get_basename(filename);
>
> 'filename' is now pointless and you might as well
> just drop it and use
>  base_filename = g_path_get_basename(ts->bprm->filename);

I agree.

>> +    /*
>> +     * Using strncpy here is fine: at max-length,
>> +     * this field is not NUL-terminated.
>> +     */
>>     (void) strncpy(psinfo->pr_fname, base_filename,
>>                    sizeof(psinfo->pr_fname));
>> -    free(base_filename);
>> -    free(filename);
>>
>> +    free(base_filename);
>
> You need to g_free() this, not free() it, right?

Even if free would work, g_free looks better.
Thanks.

>From 402100deb7e27b1d7ac619ebac963f861fae91b0 Mon Sep 17 00:00:00 2001
From: Jim Meyering <address@hidden>
Date: Mon, 7 May 2012 18:34:26 +0200
Subject: [PATCH] linux-user: remove two unchecked uses of strdup

Remove two uses of strdup (use g_path_get_basename instead),
and add a comment that this strncpy use is ok.

Signed-off-by: Jim Meyering <address@hidden>
---
 linux-user/elfload.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/linux-user/elfload.c b/linux-user/elfload.c
index f3b1552..8807684 100644
--- a/linux-user/elfload.c
+++ b/linux-user/elfload.c
@@ -2316,7 +2316,7 @@ static void fill_prstatus(struct target_elf_prstatus 
*prstatus,

 static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
 {
-    char *filename, *base_filename;
+    char *base_filename;
     unsigned int i, len;

     (void) memset(psinfo, 0, sizeof (*psinfo));
@@ -2338,13 +2338,15 @@ static int fill_psinfo(struct target_elf_prpsinfo 
*psinfo, const TaskState *ts)
     psinfo->pr_uid = getuid();
     psinfo->pr_gid = getgid();

-    filename = strdup(ts->bprm->filename);
-    base_filename = strdup(basename(filename));
+    base_filename = g_path_get_basename(ts->bprm->filename);
+    /*
+     * Using strncpy here is fine: at max-length,
+     * this field is not NUL-terminated.
+     */
     (void) strncpy(psinfo->pr_fname, base_filename,
                    sizeof(psinfo->pr_fname));
-    free(base_filename);
-    free(filename);

+    g_free(base_filename);
     bswap_psinfo(psinfo);
     return (0);
 }
--
1.7.10.1.487.ga3935e6



reply via email to

[Prev in Thread] Current Thread [Next in Thread]