coreutils
[Top][All Lists]
Advanced

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

Re: [PATCH] ln: alloc pointer variables could be deleted initialized, i


From: Pádraig Brady
Subject: Re: [PATCH] ln: alloc pointer variables could be deleted initialized, it first allocates memory
Date: Fri, 5 Aug 2022 13:00:40 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:98.0) Gecko/20100101 Thunderbird/98.0

On 05/08/2022 08:00, Li kunyu wrote:
The alloc pointer variable does not need to be initialized and assigned
NULL, it has allocated memory before use, which seems to reduce a mov
instruction.

Signed-off-by: Li kunyu <kunyu@nfschina.com>
---
  src/ln.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/src/ln.c b/src/ln.c
index bb4695853..cf5d69a64 100644
--- a/src/ln.c
+++ b/src/ln.c
@@ -352,17 +352,17 @@ do_link (char const *source, int destdir_fd, char const 
*dest_base,
            if (backup_base)
              {
                char *backup = backup_base;
-              void *alloc = NULL;
+              void *alloc;
                ptrdiff_t destdirlen = dest_base - dest;
                if (0 < destdirlen)
                  {
                    alloc = xmalloc (destdirlen + strlen (backup_base) + 1);
                    backup = memcpy (alloc, dest, destdirlen);
                    strcpy (backup + destdirlen, backup_base);
+                 free (alloc);
                  }
                quoted_backup = quoteaf_n (2, backup);
                backup_sep = " ~ ";
-              free (alloc);
              }
            printf ("%s%s%s %c> %s\n", quoted_backup, backup_sep,
                    quoteaf_n (0, dest), symbolic_link ? '-' : '=',

This doesn't look correct.
The memcpy() resets "backup" to the allocated area,
which is then referenced in the quoteaf_n() call.
Also in general, it's easier to reason about allocations
when they're declared and free'd in the same scope.

thanks,
Pádraig



reply via email to

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