]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: use snprintf() instead of sprintf() stable/v2.41
authorKarel Zak <kzak@redhat.com>
Mon, 6 Oct 2025 13:04:24 +0000 (15:04 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 9 Oct 2025 08:18:28 +0000 (10:18 +0200)
Replace sprintf() calls with snprintf() to ensure proper bounds
checking when formatting strings.

In encode.c, the check now validates snprintf() return value instead
of pre-checking buffer size, providing more robust error handling.

In probe.c, snprintf() is used with proper size calculation based on
remaining buffer space.

Signed-off-by: Karel Zak <kzak@redhat.com>
(cherry picked from commit 041380f4ca7244df624bf7efdb5e27fdd3144175)

libblkid/src/encode.c
libblkid/src/probe.c

index 8213873eeae5e0ff5cc60759abab7d5e67b8d458..4b83f66909dfaf35b61fac36df3245b22d54165a 100644 (file)
@@ -191,9 +191,11 @@ int blkid_encode_string(const char *str, char *str_enc, size_t len)
                        j += seqlen;
                        i += (seqlen-1);
                } else if (str[i] == '\\' || !is_whitelisted(str[i], NULL)) {
-                       if (len-j < 4)
+                       int rc;
+
+                       rc = snprintf(&str_enc[j], len-j, "\\x%02x", (unsigned char) str[i]);
+                       if (rc != 4)
                                goto err;
-                       sprintf(&str_enc[j], "\\x%02x", (unsigned char) str[i]);
                        j += 4;
                } else {
                        if (len-j < 1)
index 61b93021c7c9f1c9b561e94f1035a634796638a3..a38f5990ebe89b432bfbe2a3932d3597661eca2e 100644 (file)
@@ -2012,8 +2012,8 @@ static void blkid_probe_log_csum_mismatch(blkid_probe pr, size_t n, const void *
        int hex_size = min(sizeof(csum_hex), n * 2);
 
        for (int i = 0; i < hex_size; i+=2) {
-               sprintf(&csum_hex[i], "%02X", ((const unsigned char *) csum)[i / 2]);
-               sprintf(&expected_hex[i], "%02X", ((const unsigned char *) expected)[i / 2]);
+               snprintf(&csum_hex[i], sizeof(csum_hex) - i, "%02X", ((const unsigned char *) csum)[i / 2]);
+               snprintf(&expected_hex[i], sizeof(expected_hex) - i, "%02X", ((const unsigned char *) expected)[i / 2]);
        }
 
        DBG(LOWPROBE, ul_debug(