]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/: Use readlinknul() instead of its pattern
authorAlejandro Colomar <alx@kernel.org>
Wed, 3 Jul 2024 01:20:34 +0000 (03:20 +0200)
committerSerge Hallyn <serge@hallyn.com>
Sat, 2 Nov 2024 02:25:50 +0000 (21:25 -0500)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/copydir.c

index 0d855b7166c94f92aa538744959a9c8a1d2baa65..be122ea584a3707ba017402167d9df44df38176b 100644 (file)
@@ -22,6 +22,7 @@
 #include "alloc/malloc.h"
 #include "alloc/x/xmalloc.h"
 #include "attr.h"
+#include "fs/readlink/readlinknul.h"
 #include "prototypes.h"
 #include "defines.h"
 #ifdef WITH_SELINUX
@@ -549,27 +550,20 @@ static /*@null@*/char *readlink_malloc (const char *filename)
        size_t size = 1024;
 
        while (true) {
-               ssize_t nchars;
+               int  len;
                char *buffer = MALLOC(size, char);
                if (NULL == buffer) {
                        return NULL;
                }
 
-               nchars = readlink (filename, buffer, size);
+               len = readlinknul(filename, buffer, size);
+               if (len != -1)
+                       return buffer;
 
-               if (nchars < 0) {
-                       free(buffer);
+               free(buffer);
+               if (errno != E2BIG)
                        return NULL;
-               }
-
-               if ((size_t) nchars < size) { /* The buffer was large enough */
-                       /* readlink does not nul-terminate */
-                       stpcpy(&buffer[nchars], "");
-                       return buffer;
-               }
 
-               /* Try again with a bigger buffer */
-               free (buffer);
                size *= 2;
        }
 }