Improve error handling and fix a resource leak inside an error path.
Signed-off-by: Daniel Golle <daniel@makrotopia.org>
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;
}
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;
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;
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;
* 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);
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) {
devname = gl.gl_pathv[j];
tmp = strrchr(devname, '/');
+ if (!tmp)
+ return NULL;
+
*tmp = '\0';
devname = strrchr(devname, '/') + 1;
}
in = open(file, O_RDONLY);
- if (in < 1) {
+ if (in < 0) {
ULOG_ERR("failed to open %s\n", file);
goto out;
}
ret = 0;
out:
- if (in > 0)
+ if (in >= 0)
close(in);
return ret;
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;
}
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;
}
return 0;
}
- block += pad_file_size(v, hdr.length) / v->block_size;
+ block += pad_file_size(v, hdr.length) / v->block_size;
return block;
}
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", "");