From e54ab6ec24442eb5414fef414f0ee815efb9a9bb Mon Sep 17 00:00:00 2001
From: Kamil Dudka <kdudka@redhat.com>
Date: Tue, 19 Mar 2024 17:07:52 +0800
Subject: [PATCH 1/3] ln: fix memory leaks in do_link

* src/ln.c (do_link): Free memory allocated by convert_abs_rel
on all code paths (Bug#47373).

Upstream-commit: 6e98f67758260579d7d44ea5f2df4c82d28c9f58
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
---
 src/ln.c | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/src/ln.c b/src/ln.c
index ffa278e..9b52602 100644
--- a/src/ln.c
+++ b/src/ln.c
@@ -229,14 +229,14 @@ do_link (char const *source, int destdir_fd, char const *dest_base,
               if (errno != ENOENT)
                 {
                   error (0, errno, _("failed to access %s"), quoteaf (dest));
-                  return false;
+                  goto fail;
                 }
               force = false;
             }
           else if (S_ISDIR (dest_stats.st_mode))
             {
               error (0, 0, _("%s: cannot overwrite directory"), quotef (dest));
-              return false;
+              goto fail;
             }
           else if (seen_file (dest_set, dest, &dest_stats))
             {
@@ -245,7 +245,7 @@ do_link (char const *source, int destdir_fd, char const *dest_base,
               error (0, 0,
                      _("will not overwrite just-created %s with %s"),
                      quoteaf_n (0, dest), quoteaf_n (1, source));
-              return false;
+              goto fail;
             }
           else
             {
@@ -274,7 +274,7 @@ do_link (char const *source, int destdir_fd, char const *dest_base,
                     {
                       error (0, 0, _("%s and %s are the same file"),
                              quoteaf_n (0, source), quoteaf_n (1, dest));
-                      return false;
+                      goto fail;
                     }
                 }
 
@@ -285,7 +285,10 @@ do_link (char const *source, int destdir_fd, char const *dest_base,
                       fprintf (stderr, _("%s: replace %s? "),
                                program_name, quoteaf (dest));
                       if (!yesno ())
-                        return true;
+                        {
+                          free(rel_source);
+                          return true;
+                        }
                     }
 
                   if (backup_type != no_backups)
@@ -304,7 +307,7 @@ do_link (char const *source, int destdir_fd, char const *dest_base,
                            {
                               error (0, rename_errno, _("cannot backup %s"),
                                      quoteaf (dest));
-                              return false;
+                              goto fail;
                             }
                           force = false;
                         }
@@ -397,6 +400,10 @@ do_link (char const *source, int destdir_fd, char const *dest_base,
   free (backup_base);
   free (rel_source);
   return link_errno <= 0;
+
+fail:
+  free (rel_source);
+  return false;
 }
 
 void
-- 
2.27.0


From e1422d781d0d9da3a92f28d7294a9c8a0e45f25a Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Tue, 19 Mar 2024 17:22:26 +0800
Subject: [PATCH 2/3] hostname: use puts

* src/hostname.c (main): Prefer puts to printf "%s\n".

Upstream-commit: c7a588ac3632aae21642d4d568497177950d36bf
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
---
 src/hostname.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/hostname.c b/src/hostname.c
index 0b5c0cf..62cc98c 100644
--- a/src/hostname.c
+++ b/src/hostname.c
@@ -103,7 +103,7 @@ main (int argc, char **argv)
       hostname = xgethostname ();
       if (hostname == NULL)
         die (EXIT_FAILURE, errno, _("cannot determine hostname"));
-      printf ("%s\n", hostname);
+      puts (hostname);
     }
 
   if (optind + 1 < argc)
-- 
2.27.0


From 76b29ef9909cc881b99de3dd5c34e90b03385a70 Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Tue, 19 Mar 2024 17:23:22 +0800
Subject: [PATCH 3/3] hostname: pacify valgrind

* src/hostname.c (main) [IF_LINT]: Free hostname (Bug#47384).

Upstream-commit: 4698e284f37844bc9b9f63f00eb556ccaaed5030
Signed-off-by: Kamil Dudka <kdudka@redhat.com>
---
 src/hostname.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/hostname.c b/src/hostname.c
index 62cc98c..7210248 100644
--- a/src/hostname.c
+++ b/src/hostname.c
@@ -104,6 +104,7 @@ main (int argc, char **argv)
       if (hostname == NULL)
         die (EXIT_FAILURE, errno, _("cannot determine hostname"));
       puts (hostname);
+      IF_LINT (free (hostname));
     }
 
   if (optind + 1 < argc)
-- 
2.27.0
