]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
lib/string/: vsnprintf_(), strtcpy(): Set errno = E2BIG on truncation
authorAlejandro Colomar <alx@kernel.org>
Sat, 8 Feb 2025 15:13:39 +0000 (16:13 +0100)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Fri, 15 May 2026 10:06:49 +0000 (12:06 +0200)
Signed-off-by: Alejandro Colomar <alx@kernel.org>
lib/string/sprintf/snprintf.h
lib/string/strcpy/strtcpy.h

index f6dee1245fc21b6fb4455d7fde750d93f647fe97..dc6009457508cd49c00bbb6257cf61a87fdafb1f 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
 
 
@@ -8,6 +8,7 @@
 
 #include "config.h"
 
+#include <errno.h>
 #include <stdarg.h>
 #include <stddef.h>
 #include <stdio.h>
@@ -53,8 +54,10 @@ vsnprintf_(char *restrict s, size_t size, const char *restrict fmt, va_list ap)
        len = vsnprintf(s, size, fmt, ap);
        if (len == -1)
                return -1;
-       if ((size_t) len >= size)
+       if ((size_t) len >= size) {
+               errno = E2BIG;
                return -1;
+       }
 
        return len;
 }
index a440bcdfb327adef390b1faa3678ee670163f507..4b9d34b7229ed65c1a7c4d2c8f84cfcf893fb408 100644 (file)
@@ -8,6 +8,7 @@
 
 #include "config.h"
 
+#include <errno.h>
 #include <stdbool.h>
 #include <stddef.h>
 #include <string.h>
@@ -42,8 +43,10 @@ strtcpy(char *restrict dst, const char *restrict src, size_t dsize)
 
        stpcpy(mempcpy(dst, src, dlen), "");
 
-       if (trunc)
+       if (trunc) {
+               errno = E2BIG;
                return -1;
+       }
 
        return slen;
 }