]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/fs/readlink/: readlinknul(): Use ssize_t to simplify
authorAlejandro Colomar <alx@kernel.org>
Sun, 9 Feb 2025 11:23:46 +0000 (12:23 +0100)
committerAlejandro Colomar <alx@kernel.org>
Mon, 3 Mar 2025 23:08:15 +0000 (00:08 +0100)
Consistently using a signed type allows us to avoid sign-mismatch
diagnostics, while keeping the code simple.  It feels weird to
accept a ssize_t instead of a size_t, but it's a matter of getting
used to it.

Another way to achieve this with a single 'len' variable and no casts
would be to compare against SIZE_MAX, but that's less readable than -1.
Or one could write a SIZE_C() macro a la UINT64_C(), and compare the
size_t against SIZE_C(-1), but that's still suboptimal (regarding
readability) compared to consistently using signed size types.

Fixes: b9d00b64a19f (2024-12-09; "lib/fs/readlink/readlinknul.h: readlinknul(): Silence warning")
Acked-by: Serge Hallyn <serge@hallyn.com>
Cc: Martin Uecker <uecker@tugraz.at>
Cc: "Robert C. Seacord" <rcseacord@gmail.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/fs/readlink/readlinknul.c
lib/fs/readlink/readlinknul.h

index b0fc2a786f1ed1934a924f786e5308752467b473..bb79dcaaf51d17547f36195219615b5f21427bf3 100644 (file)
@@ -1,4 +1,4 @@
-// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <alx@kernel.org>
 // SPDX-License-Identifier: BSD-3-Clause
 
 
@@ -6,9 +6,8 @@
 
 #include "fs/readlink/readlinknul.h"
 
-#include <stddef.h>
 #include <sys/types.h>
 
 
 extern inline ssize_t readlinknul(const char *restrict link, char *restrict buf,
-    size_t size);
+    ssize_t size);
index 606c2e112d9b37af13f680763e5d45ff8a2a96d6..ed2f34d135c4436af184f253479c20d2d987fd97 100644 (file)
@@ -1,4 +1,4 @@
-// SPDX-FileCopyrightText: 2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-FileCopyrightText: 2024-2025, Alejandro Colomar <alx@kernel.org>
 // SPDX-License-Identifier: BSD-3-Clause
 
 
@@ -9,7 +9,6 @@
 #include <config.h>
 
 #include <errno.h>
-#include <stddef.h>
 #include <string.h>
 #include <sys/types.h>
 #include <unistd.h>
 
 ATTR_STRING(1)
 inline ssize_t readlinknul(const char *restrict link, char *restrict buf,
-    size_t size);
+    ssize_t size);
 
 
 // Similar to readlink(2), but terminate the string.
 inline ssize_t
-readlinknul(const char *restrict link, char *restrict buf, size_t size)
+readlinknul(const char *restrict link, char *restrict buf, ssize_t size)
 {
-       size_t   ulen;
-       ssize_t  slen;
+       ssize_t  len;
 
-       slen = readlink(link, buf, size);
-       if (slen == -1)
+       len = readlink(link, buf, size);
+       if (len == -1)
                return -1;
 
-       ulen = slen;
-       if (ulen == size) {
+       if (len == size) {
                stpcpy(&buf[size-1], "");
                errno = E2BIG;
                return -1;
        }
 
-       stpcpy(&buf[ulen], "");
+       stpcpy(&buf[len], "");
 
-       return slen;
+       return len;
 }