]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libblkid: use snprintf() rather than sprintf()
authorKarel Zak <kzak@redhat.com>
Tue, 27 Jul 2021 11:29:05 +0000 (13:29 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 27 Jul 2021 11:29:05 +0000 (13:29 +0200)
libblkid/src/devname.c
libblkid/src/save.c

index 5b445b011499bec65abd0b76e2220fd8c7c7c455..90a8245fc9cdb9c0430315c6cd6a69a168355bf4 100644 (file)
@@ -164,7 +164,7 @@ static int is_dm_leaf(const char *devname)
                    strncmp(de->d_name, "dm-", 3) != 0 ||
                    strlen(de->d_name) > sizeof(path)-32)
                        continue;
-               sprintf(path, "/sys/block/%s/slaves", de->d_name);
+               snprintf(path, sizeof(path), "/sys/block/%s/slaves", de->d_name);
                if ((d_dir = opendir(path)) == NULL)
                        continue;
                while ((d_de = readdir(d_dir)) != NULL) {
@@ -321,14 +321,16 @@ static void lvm_probe_all(blkid_cache cache, int only_if_new)
                char            *vdirname;
                char            *vg_name;
                struct dirent   *lv_iter;
+               size_t          len;
 
                vg_name = vg_iter->d_name;
                if (!strcmp(vg_name, ".") || !strcmp(vg_name, ".."))
                        continue;
-               vdirname = malloc(vg_len + strlen(vg_name) + 8);
+               len = vg_len + strlen(vg_name) + 8;
+               vdirname = malloc(len);
                if (!vdirname)
                        goto exit;
-               sprintf(vdirname, "%s/%s/LVs", VG_DIR, vg_name);
+               snprintf(vdirname, len, "%s/%s/LVs", VG_DIR, vg_name);
 
                lv_list = opendir(vdirname);
                free(vdirname);
@@ -342,16 +344,16 @@ static void lvm_probe_all(blkid_cache cache, int only_if_new)
                        if (!strcmp(lv_name, ".") || !strcmp(lv_name, ".."))
                                continue;
 
-                       lvm_device = malloc(vg_len + strlen(vg_name) +
-                                           strlen(lv_name) + 8);
+                       len = vg_len + strlen(vg_name) + strlen(lv_name) + 8;
+                       lvm_device = malloc(len);
                        if (!lvm_device) {
                                closedir(lv_list);
                                goto exit;
                        }
-                       sprintf(lvm_device, "%s/%s/LVs/%s", VG_DIR, vg_name,
+                       snprintf(lvm_device, len, "%s/%s/LVs/%s", VG_DIR, vg_name,
                                lv_name);
                        dev = lvm_get_devno(lvm_device);
-                       sprintf(lvm_device, "%s/%s", vg_name, lv_name);
+                       snprintf(lvm_device, len, "%s/%s", vg_name, lv_name);
                        DBG(DEVNAME, ul_debug("Probe LVM dev %s: devno 0x%04X",
                                                  lvm_device,
                                                  (unsigned int) dev));
index 9a342c69cfef1881af469fdb04fb15e6c51ae2c7..1a617c072e7dda179e38063c8db745b5930bda31 100644 (file)
@@ -128,9 +128,10 @@ int blkid_flush_cache(blkid_cache cache)
         * a temporary file then we open it directly.
         */
        if (ret == 0 && S_ISREG(st.st_mode)) {
-               tmp = malloc(strlen(filename) + 8);
+               size_t len = strlen(filename) + 8;
+               tmp = malloc(len);
                if (tmp) {
-                       sprintf(tmp, "%s-XXXXXX", filename);
+                       snprintf(tmp, len, "%s-XXXXXX", filename);
                        fd = mkstemp_cloexec(tmp);
                        if (fd >= 0) {
                                if (fchmod(fd, 0644) != 0)
@@ -178,10 +179,11 @@ int blkid_flush_cache(blkid_cache cache)
                        DBG(SAVE, ul_debug("unlinked temp cache %s", opened));
                } else {
                        char *backup;
+                       size_t len = strlen(filename) + 5;
 
-                       backup = malloc(strlen(filename) + 5);
+                       backup = malloc(len);
                        if (backup) {
-                               sprintf(backup, "%s.old", filename);
+                               snprintf(backup, len, "%s.old", filename);
                                unlink(backup);
                                if (link(filename, backup)) {
                                        DBG(SAVE, ul_debug("can't link %s to %s",