From 61851f65266eaafd4ff8775c097dafd7c2d3a2c3 Mon Sep 17 00:00:00 2001 From: Chih-Mih Chao Date: Mon, 11 May 2009 02:57:05 +0800 Subject: [PATCH] add error handling to suppress 'warn_unused_result' warning --- block-bochs.c | 3 ++- block-cow.c | 11 +++++++++-- block-qcow.c | 17 ++++++++++++++--- block-qcow2.c | 38 ++++++++++++++++++++++++++++++-------- block-raw-posix.c | 5 ++++- block-vmdk.c | 26 +++++++++++++++++++------- block-vvfat.c | 4 +++- block.c | 3 ++- qemu-nbd.c | 3 ++- usb-linux.c | 4 +++- vl.c | 18 ++++++++++++++---- 11 files changed, 102 insertions(+), 30 deletions(-) diff --git a/block-bochs.c b/block-bochs.c index 7a75412..97ec528 100644 --- a/block-bochs.c +++ b/block-bochs.c @@ -198,7 +198,8 @@ static inline int seek_to_sector(BlockDriverState *bs, int64_t sector_num) // read in bitmap for current extent lseek(s->fd, bitmap_offset + (extent_offset / 8), SEEK_SET); - read(s->fd, &bitmap_entry, 1); + if (read(s->fd, &bitmap_entry, 1) == -1) + return -1; if (!((bitmap_entry >> (extent_offset % 8)) & 1)) { diff --git a/block-cow.c b/block-cow.c index 17e3292..c0e3911 100644 --- a/block-cow.c +++ b/block-cow.c @@ -239,11 +239,18 @@ static int cow_create(const char *filename, int64_t image_sectors, } cow_header.sectorsize = cpu_to_be32(512); cow_header.size = cpu_to_be64(image_sectors * 512); - write(cow_fd, &cow_header, sizeof(cow_header)); + if (write(cow_fd, &cow_header, sizeof(cow_header)) == -1) + goto fail; /* resize to include at least all the bitmap */ - ftruncate(cow_fd, sizeof(cow_header) + ((image_sectors + 7) >> 3)); + if (ftruncate(cow_fd, sizeof(cow_header) + ((image_sectors + 7) >> 3)) == -1) + goto fail; + close(cow_fd); return 0; + +fail: + close(cow_fd); + return -1; } static void cow_flush(BlockDriverState *bs) diff --git a/block-qcow.c b/block-qcow.c index fc6b809..16138f3 100644 --- a/block-qcow.c +++ b/block-qcow.c @@ -811,17 +811,28 @@ static int qcow_create(const char *filename, int64_t total_size, } /* write all the data */ - write(fd, &header, sizeof(header)); + if (write(fd, &header, sizeof(header)) == -1) { + goto fail; + } + if (backing_file) { - write(fd, backing_file, backing_filename_len); + if (write(fd, backing_file, backing_filename_len) == -1) { + goto fail; + } } lseek(fd, header_size, SEEK_SET); tmp = 0; for(i = 0;i < l1_size; i++) { - write(fd, &tmp, sizeof(tmp)); + if (write(fd, &tmp, sizeof(tmp)) == -1) { + goto fail; + } } close(fd); return 0; + +fail: + close(fd); + return -1; } static int qcow_make_empty(BlockDriverState *bs) diff --git a/block-qcow2.c b/block-qcow2.c index 9a49777..1306f42 100644 --- a/block-qcow2.c +++ b/block-qcow2.c @@ -1662,7 +1662,9 @@ static int qcow_create2(const char *filename, int64_t total_size, create_refcount_update(s, s->refcount_block_offset, ref_clusters * s->cluster_size); /* write all the data */ - write(fd, &header, sizeof(header)); + if (write(fd, &header, sizeof(header)) == -1) { + goto fail; + } if (backing_file) { if (backing_format_len) { char zero[16]; @@ -1671,29 +1673,49 @@ static int qcow_create2(const char *filename, int64_t total_size, memset(zero, 0, sizeof(zero)); cpu_to_be32s(&ext_bf.magic); cpu_to_be32s(&ext_bf.len); - write(fd, &ext_bf, sizeof(ext_bf)); - write(fd, backing_format, backing_format_len); + if (write(fd, &ext_bf, sizeof(ext_bf)) == -1) { + goto fail; + } + if (write(fd, backing_format, backing_format_len) == -1) { + goto fail; + } if (d>0) { - write(fd, zero, d); + if (write(fd, zero, d) == -1) { + goto fail; + } } } - write(fd, backing_file, backing_filename_len); + if (write(fd, backing_file, backing_filename_len) == -1) { + goto fail; + } } lseek(fd, s->l1_table_offset, SEEK_SET); tmp = 0; for(i = 0;i < l1_size; i++) { - write(fd, &tmp, sizeof(tmp)); + if (write(fd, &tmp, sizeof(tmp)) == -1) { + goto fail; + } } lseek(fd, s->refcount_table_offset, SEEK_SET); - write(fd, s->refcount_table, s->cluster_size); + if (write(fd, s->refcount_table, s->cluster_size) == -1) { + goto fail; + } lseek(fd, s->refcount_block_offset, SEEK_SET); - write(fd, s->refcount_block, ref_clusters * s->cluster_size); + if (write(fd, s->refcount_block, ref_clusters * s->cluster_size) == -1) { + goto fail; + } qemu_free(s->refcount_table); qemu_free(s->refcount_block); close(fd); return 0; + +fail: + qemu_free(s->refcount_table); + qemu_free(s->refcount_block); + close(fd); + return -1; } static int qcow_create(const char *filename, int64_t total_size, diff --git a/block-raw-posix.c b/block-raw-posix.c index 0663c06..3ace450 100644 --- a/block-raw-posix.c +++ b/block-raw-posix.c @@ -835,7 +835,10 @@ static int raw_create(const char *filename, int64_t total_size, 0644); if (fd < 0) return -EIO; - ftruncate(fd, total_size * 512); + if (ftruncate(fd, total_size * 512) < 0) { + close(fd); + return -EIO; + } close(fd); return 0; } diff --git a/block-vmdk.c b/block-vmdk.c index d47d483..b880377 100644 --- a/block-vmdk.c +++ b/block-vmdk.c @@ -232,7 +232,8 @@ static int vmdk_snapshot_create(const char *filename, const char *backing_file) memset(&header, 0, sizeof(header)); memcpy(&header,&hdr[4], sizeof(header)); // skip the VMDK4_MAGIC - ftruncate(snp_fd, header.grain_offset << 9); + if (ftruncate(snp_fd, header.grain_offset << 9) == -1) + goto fail; /* the descriptor offset = 0x200 */ if (lseek(p_fd, 0x200, SEEK_SET) == -1) goto fail; @@ -756,22 +757,28 @@ static int vmdk_create(const char *filename, int64_t total_size, header.check_bytes[3] = 0xa; /* write all the data */ - write(fd, &magic, sizeof(magic)); - write(fd, &header, sizeof(header)); + if (write(fd, &magic, sizeof(magic)) == -1) + goto fail; + if (write(fd, &header, sizeof(header)) == -1) + goto fail; - ftruncate(fd, header.grain_offset << 9); + if (ftruncate(fd, header.grain_offset << 9) == -1) + goto fail; /* write grain directory */ lseek(fd, le64_to_cpu(header.rgd_offset) << 9, SEEK_SET); for (i = 0, tmp = header.rgd_offset + gd_size; i < gt_count; i++, tmp += gt_size) - write(fd, &tmp, sizeof(tmp)); + if (write(fd, &tmp, sizeof(tmp)) == -1) + goto fail; /* write backup grain directory */ lseek(fd, le64_to_cpu(header.gd_offset) << 9, SEEK_SET); for (i = 0, tmp = header.gd_offset + gd_size; i < gt_count; i++, tmp += gt_size) - write(fd, &tmp, sizeof(tmp)); + if (write(fd, &tmp, sizeof(tmp)) == -1) + goto fail; + /* compose the descriptor */ real_filename = filename; @@ -788,10 +795,15 @@ static int vmdk_create(const char *filename, int64_t total_size, /* write the descriptor */ lseek(fd, le64_to_cpu(header.desc_offset) << 9, SEEK_SET); - write(fd, desc, strlen(desc)); + if (write(fd, desc, strlen(desc)) == -1) + goto fail; close(fd); return 0; + + fail: + close(fd); + return -1; } static void vmdk_close(BlockDriverState *bs) diff --git a/block-vvfat.c b/block-vvfat.c index 7905931..4de09ff 100644 --- a/block-vvfat.c +++ b/block-vvfat.c @@ -2256,7 +2256,9 @@ static int commit_one_file(BDRVVVFATState* s, c = c1; } - ftruncate(fd, size); + if (ftruncate(fd, size) < 0) + return -4; + close(fd); return commit_mappings(s, first_cluster, dir_index); diff --git a/block.c b/block.c index acb8976..55c64c6 100644 --- a/block.c +++ b/block.c @@ -394,7 +394,8 @@ int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, snprintf(backing_filename, sizeof(backing_filename), "%s", filename); else - realpath(filename, backing_filename); + if (realpath(filename, backing_filename) == NULL) + return -1; ret = bdrv_create2(&bdrv_qcow2, tmp_filename, total_size, backing_filename, diff --git a/qemu-nbd.c b/qemu-nbd.c index 0af97ca..b60602d 100644 --- a/qemu-nbd.c +++ b/qemu-nbd.c @@ -346,7 +346,8 @@ int main(int argc, char **argv) int sock; if (!verbose) - daemon(0, 0); /* detach client and server */ + if (daemon(0, 0) == -1) /* detach client and server */ + errx(errno, "Could not run in background"); if (socket == NULL) { sprintf(sockpath, SOCKET_PATH, basename(device)); diff --git a/usb-linux.c b/usb-linux.c index 70d7a1c..98e55b1 100644 --- a/usb-linux.c +++ b/usb-linux.c @@ -1160,7 +1160,9 @@ static int usb_host_read_file(char *line, size_t line_size, const char *device_f device_file); f = fopen(filename, "r"); if (f) { - fgets(line, line_size, f); + if (fgets(line, line_size, f) == NULL) + monitor_printf(mon, "husb: could not read %s\n", filename); + fclose(f); ret = 1; } else { diff --git a/vl.c b/vl.c index 8e4fba6..7e88ee1 100644 --- a/vl.c +++ b/vl.c @@ -3696,7 +3696,8 @@ static void qemu_event_increment(void) if (io_thread_fd == -1) return; - write(io_thread_fd, &byte, sizeof(byte)); + if (write(io_thread_fd, &byte, sizeof(byte)) == -1) + perror("Failed write io_thread"); } static void qemu_event_read(void *opaque) @@ -5629,7 +5630,8 @@ int main(int argc, char **argv, char **envp) if (pid_file && qemu_create_pidfile(pid_file) != 0) { if (daemonize) { uint8_t status = 1; - write(fds[1], &status, 1); + if (write(fds[1], &status, 1) == -1) + fprintf(stderr, "Could not write status\n"); } else fprintf(stderr, "Could not acquire pid file\n"); exit(1); @@ -6047,7 +6049,11 @@ int main(int argc, char **argv, char **envp) if (len != 1) exit(1); - chdir("/"); + if (chdir("/") == -1) { + fprintf(stderr, "change directory to '/' failed"); + exit(1); + } + TFR(fd = open("/dev/null", O_RDWR)); if (fd == -1) exit(1); @@ -6066,7 +6072,11 @@ int main(int argc, char **argv, char **envp) fprintf(stderr, "chroot failed\n"); exit(1); } - chdir("/"); + + if (chdir("/") == -1) { + fprintf(stderr, "change directory to '/' failed"); + exit(1); + } } if (run_as) { -- 1.6.0.4