fstools: fix a couple of minor code problems
authorDaniel Golle <daniel@makrotopia.org>
Tue, 16 Nov 2021 20:49:23 +0000 (20:49 +0000)
committerDaniel Golle <daniel@makrotopia.org>
Tue, 16 Nov 2021 21:52:36 +0000 (21:52 +0000)
Improve error handling and fix a resource leak inside an error path.

Signed-off-by: Daniel Golle <daniel@makrotopia.org>
libblkid-tiny/libblkid-tiny.c
libfstools/common.c
libfstools/mount.c
libfstools/overlay.c
libfstools/partname.c
libfstools/snapshot.c

index 6e8cd81debd771fb1bca730189efba9b555cac61..8520f8a6b5b33934ec6e529c0a6f4786e601079a 100644 (file)
@@ -87,11 +87,15 @@ unsigned char *blkid_probe_get_buffer(blkid_probe pr,
        memset(bf, 0, sizeof(*bf));
        bf->data = ((unsigned char *)bf) + sizeof(*bf);
 
-       lseek(pr->fd, off, SEEK_SET);
+       if (lseek(pr->fd, off, SEEK_SET) < 0) {
+               fprintf(stderr, "failed to seek\n");
+               free(bf);
+               return NULL;
+       }
        ret = read(pr->fd, bf->data, len);
 
        if (ret != len) {
-               fprintf(stderr, "faile to read blkid\n");
+               fprintf(stderr, "failed to read blkid\n");
                free(bf);
                return NULL;
        }
@@ -212,10 +216,14 @@ int probe_block(char *block, struct blkid_struct_probe *pr)
                        int off = (mag->kboff * 1024) + mag->sboff;
                        char magic[32] = { 0 };
 
-                       lseek(pr->fd, off, SEEK_SET);
-                       if (read(pr->fd, magic, mag->len) < 0)
+                       if (lseek(pr->fd, off, SEEK_SET) < 0) {
+                               close(pr->fd);
                                return -1;
-
+                       }
+                       if (read(pr->fd, magic, mag->len) < 0) {
+                               close(pr->fd);
+                               return -1;
+                       }
                        DEBUG("magic: %s %s %d\n", mag->magic, magic, mag->len);
                        if (!memcmp(mag->magic, magic, mag->len))
                                break;
index 8dc109537a195c0476a5c02cc2eb4391157ff477..1b22d2cb19aefe1b928fad6045210b2656c1c16d 100644 (file)
@@ -59,13 +59,17 @@ int block_file_identify(FILE *f, uint64_t offset)
        uint32_t magic = 0;
        size_t n;
 
-       fseeko(f, offset, SEEK_SET);
+       if (fseeko(f, offset, SEEK_SET) < 0)
+               return -1;
+
        n = fread(&magic, sizeof(magic), 1, f);
        if (magic == cpu_to_le32(0x88b1f)) {
                return FS_TARGZ;
        }
 
-       fseeko(f, offset + 0x400, SEEK_SET);
+       if (fseeko(f, offset + 0x400, SEEK_SET) < 0)
+               return -1;
+
        n = fread(&magic, sizeof(magic), 1, f);
        if (n != 1)
                return -1;
@@ -74,7 +78,9 @@ int block_file_identify(FILE *f, uint64_t offset)
                return FS_F2FS;
 
        magic = 0;
-       fseeko(f, offset + 0x438, SEEK_SET);
+       if (fseeko(f, offset + 0x438, SEEK_SET) < 0)
+               return -1;
+
        n = fread(&magic, sizeof(magic), 1, f);
        if (n != 1)
                return -1;
index 1691ce747f50921423e1ca43709549c612b6d1be..3d4111fe6b58c7d46ba96038c7fc571005f308cd 100644 (file)
@@ -143,8 +143,11 @@ fopivot(char *rw_root, char *ro_root)
         * We can't really deal with these constraints without
         * creating two new subdirectories in /overlay.
         */
-       mkdir(upperdir, 0755);
-       mkdir(workdir, 0755);
+       if (mkdir(upperdir, 0755) == -1 && errno != EEXIST)
+               return -1;
+
+       if (mkdir(workdir, 0755) == -1 && errno != EEXIST)
+               return -1;
 
        if (stat(upgrade, &st) == 0)
                rename(upgrade, upgrade_dest);
index d7d8f48e1279589e5f55127fa6c6a068878d5892..5de12c52b683a2655068ec6d4fe25abbc07d7f80 100644 (file)
@@ -175,14 +175,20 @@ static int
 switch2jffs(struct volume *v)
 {
        struct stat s;
-       int ret;
+       int ret, fd;
 
        if (!stat(SWITCH_JFFS2, &s)) {
                ULOG_ERR("jffs2 switch already running\n");
                return -1;
        }
 
-       creat(SWITCH_JFFS2, 0600);
+       fd = creat(SWITCH_JFFS2, 0600);
+       if (fd == -1) {
+               ULOG_ERR("failed - cannot create jffs2 switch mark: %m\n");
+               return -1;
+       }
+       close(fd);
+
        ret = mount(v->blk, OVERLAYDIR, "jffs2", MS_NOATIME, NULL);
        unlink(SWITCH_JFFS2);
        if (ret) {
index 773df8cacad3089a61e019c328b020026e500570..f59c52eb8f3c6da0e8ee6c4080abd7cdac3db715 100644 (file)
@@ -152,6 +152,9 @@ static struct volume *partname_volume_find(char *name)
 
        devname = gl.gl_pathv[j];
        tmp = strrchr(devname, '/');
+       if (!tmp)
+               return NULL;
+
        *tmp = '\0';
        devname = strrchr(devname, '/') + 1;
 
index 4870cf7a1955b7fd91d4b31e8dbcfd3f0e9d47fd..5e7e847e91c9dd66174000cfb8b5947288f3c81e 100644 (file)
@@ -145,7 +145,7 @@ snapshot_write_file(struct volume *v, int block, char *file, uint32_t seq, uint3
        }
 
        in = open(file, O_RDONLY);
-       if (in < 1) {
+       if (in < 0) {
                ULOG_ERR("failed to open %s\n", file);
                goto out;
        }
@@ -161,7 +161,7 @@ snapshot_write_file(struct volume *v, int block, char *file, uint32_t seq, uint3
        ret = 0;
 
 out:
-       if (in > 0)
+       if (in >= 0)
                close(in);
 
        return ret;
@@ -190,7 +190,7 @@ snapshot_read_file(struct volume *v, int block, char *file, uint32_t type)
                return -1;
 
        out = open(file, O_WRONLY | O_CREAT, 0700);
-       if (!out) {
+       if (out < 0) {
                ULOG_ERR("failed to open %s\n", file);
                return -1;
        }
@@ -203,10 +203,14 @@ snapshot_read_file(struct volume *v, int block, char *file, uint32_t type)
                if (hdr.length < len)
                        len = hdr.length;
 
-               if (volume_read(v, buffer, offset, len))
+               if (volume_read(v, buffer, offset, len)) {
+                       close(out);
                        return -1;
-               if (write(out, buffer, len) != len)
+               }
+               if (write(out, buffer, len) != len) {
+                       close(out);
                        return -1;
+               }
                offset += len;
                hdr.length -= len;
        }
@@ -219,7 +223,7 @@ snapshot_read_file(struct volume *v, int block, char *file, uint32_t type)
                return 0;
        }
 
-        block += pad_file_size(v, hdr.length) / v->block_size;
+       block += pad_file_size(v, hdr.length) / v->block_size;
 
        return block;
 }
@@ -334,7 +338,10 @@ mount_snapshot(struct volume *v)
                return -1;
        }
        foreachdir("/overlay/", handle_whiteout);
-       mkdir("/volatile", 0700);
+       if (mkdir("/volatile", 0700) == -1 && errno != EEXIST) {
+               perror("mkdir");
+               return -1;
+       }
        _ramoverlay("/rom", "/volatile");
        mount_move("/rom/volatile", "/volatile", "");
        mount_move("/rom/rom", "/rom", "");