]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/sprintf/: snprintf_(): Use ssize_t instead of size_t in $2
authorAlejandro Colomar <alx@kernel.org>
Sat, 8 Feb 2025 15:19:02 +0000 (16:19 +0100)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Fri, 15 May 2026 10:06:49 +0000 (12:06 +0200)
vsnprintf(3) returns an int.  By using ssize_t, which is also signed, we
avoid the need for a cast.

Cc: Serge Hallyn <serge@hallyn.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/string/sprintf/snprintf.c
lib/string/sprintf/snprintf.h

index e3755da76bef12cc1ad969973b90c9276639b07f..f4ede2596ac79f844d68f0c9cddb7a416aea0295 100644 (file)
@@ -1,4 +1,4 @@
-// SPDX-FileCopyrightText: 2023-2024, Alejandro Colomar <alx@kernel.org>
+// SPDX-FileCopyrightText: 2023-2025, Alejandro Colomar <alx@kernel.org>
 // SPDX-License-Identifier: BSD-3-Clause
 
 
@@ -7,10 +7,10 @@
 #include "string/sprintf/snprintf.h"
 
 #include <stdarg.h>
-#include <stddef.h>
+#include <sys/types.h>
 
 
-extern inline int snprintf_(char *restrict s, size_t size,
+extern inline int snprintf_(char *restrict s, ssize_t size,
     const char *restrict fmt, ...);
-extern inline int vsnprintf_(char *restrict s, size_t size,
+extern inline int vsnprintf_(char *restrict s, ssize_t size,
     const char *restrict fmt, va_list ap);
index dc6009457508cd49c00bbb6257cf61a87fdafb1f..62be874922024260345e5288c9dbc820f7169ba2 100644 (file)
@@ -10,8 +10,8 @@
 
 #include <errno.h>
 #include <stdarg.h>
-#include <stddef.h>
 #include <stdio.h>
+#include <sys/types.h>
 
 #include "attr.h"
 #include "sizeof.h"
 
 
 format_attr(printf, 3, 4)
-inline int snprintf_(char *restrict s, size_t size, const char *restrict fmt,
-    ...);
+inline int snprintf_(char *restrict s, ssize_t size, const char *restrict fmt, ...);
 format_attr(printf, 3, 0)
-inline int vsnprintf_(char *restrict s, size_t size, const char *restrict fmt,
+inline int vsnprintf_(char *restrict s, ssize_t size, const char *restrict fmt,
     va_list ap);
 
 
 inline int
-snprintf_(char *restrict s, size_t size, const char *restrict fmt, ...)
+snprintf_(char *restrict s, ssize_t size, const char *restrict fmt, ...)
 {
        int      len;
        va_list  ap;
@@ -47,14 +46,14 @@ snprintf_(char *restrict s, size_t size, const char *restrict fmt, ...)
 
 
 inline int
-vsnprintf_(char *restrict s, size_t size, const char *restrict fmt, va_list ap)
+vsnprintf_(char *restrict s, ssize_t size, const char *restrict fmt, va_list ap)
 {
        int  len;
 
        len = vsnprintf(s, size, fmt, ap);
        if (len == -1)
                return -1;
-       if ((size_t) len >= size) {
+       if (len >= size) {
                errno = E2BIG;
                return -1;
        }