]> xenbits.xensource.com Git - xen.git/commitdiff
blktap2: Fix naked unchecked uses of read/write/chdir.
authorKeir Fraser <keir@xen.org>
Mon, 14 May 2012 16:02:16 +0000 (17:02 +0100)
committerKeir Fraser <keir@xen.org>
Mon, 14 May 2012 16:02:16 +0000 (17:02 +0100)
These cause warnings under warn_unused_result, and for read/write we
ought to deal with partial io results.

Signed-off-by: Keir Fraser <keir@xen.org>
xen-unstable changeset:   25299:01d64a3dea71
xen-unstable date:        Fri May 11 18:30:29 2012 +0100

blktap2: Fix another uninitialised value error

gcc  -O1 -fno-omit-frame-pointer -m32 -march=i686 -g
-fno-strict-aliasing -std=gnu99 -Wall -Wstrict-prototypes
-Wdeclaration-after-statement   -D__XEN_TOOLS__ -MMD -MF
.block-remus.o.d -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64
-D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -fno-optimize-sibling-calls
-mno-tls-direct-seg-refs -Werror -g -Wno-unused -fno-strict-aliasing
-I../include -I../drivers
-I/home/osstest/build.12828.build-i386/xen-unstable/tools/blktap2/drivers/../../../tools/libxc
-I/home/osstest/build.12828.build-i386/xen-unstable/tools/blktap2/drivers/../../../tools/include
-D_GNU_SOURCE -DUSE_NFS_LOCKS  -c -o block-remus.o block-remus.c

block-remus.c: In function 'ramdisk_flush':
block-remus.c:508: error: 'buf' may be used uninitialized in this
function
make[5]: *** [block-remus.o] Error 1

This is because gcc can see that merge_requests doesn't always set
*mergedbuf but gcc isn't able to prove that it always does so if
merge_requests returns 0 and that in that case the value of
ramdisk_flush::buf isn't used.

This is too useful a warning to disable, despite the occasional false
positive of this form.  The conventional approach is to suppress the
warning by explicitly initialising the variable to 0.

This has just come to light because 25275:27d63b9f111a reenabled
optimisation for this area of code, and gcc's data flow analysis
(which is required to trigger the uninitialised variable warning) only
occurs when optimisation is turned on.

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
xen-unstable changeset:   25281:60064411a8a9
xen-unstable date:        Thu May 10 14:26:14 2012 +0100

blktap2: Do not build with -O0

Signed-off-by: Keir Fraser <keir@xen.org>
xen-unstable changeset:   25275:27d63b9f111a
xen-unstable date:        Thu May 10 11:22:18 2012 +0100

blktap2: Fix uninitialised value error.

Signed-off-by: Keir Fraser <keir@xen.org>
xen-unstable changeset:   25274:cb82b5aa73bd
xen-unstable date:        Thu May 10 11:21:59 2012 +0100

tools/blktap2: fix out of bounds access in block-log.c

block-log.c: In function 'ctl_close_sock':
block-log.c:363:23: warning: array subscript is above array bounds
[-Warray-bounds]

Adjust loop condition in ctl_close_sock() to fix warning.
Adjust array acccess in ctl_close() to actually access the array
member.

Signed-off-by: Olaf Hering <olaf@aepfle.de>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Committed-by: Keir Fraser <keir@xen.org>
xen-unstable changeset:   25273:83a02f225bde
xen-unstable date:        Thu May 10 11:20:04 2012 +0100

tools/blktap2: fix build errors caused by Werror in
vhd_journal_write_entry

-O2 -Wall -Werror triggers these warnings:

libvhd-journal.c: In function 'vhd_journal_write_entry':
libvhd-journal.c:335: warning: statement with no effect

Really return the error from vhd_journal_write() to caller.

v2:
 - simplify the patch by just adding the missing return statement

Signed-off-by: Olaf Hering <olaf@aepfle.de>
Committed-by: Keir Fraser <keir@xen.org>
xen-unstable changeset:   25272:ca02580986d2
xen-unstable date:        Thu May 10 11:19:05 2012 +0100

12 files changed:
tools/blktap2/drivers/Makefile
tools/blktap2/drivers/block-log.c
tools/blktap2/drivers/block-qcow.c
tools/blktap2/drivers/block-remus.c
tools/blktap2/drivers/tapdisk-diff.c
tools/blktap2/drivers/tapdisk-log.c
tools/blktap2/drivers/tapdisk-queue.c
tools/blktap2/drivers/tapdisk-stream.c
tools/blktap2/drivers/tapdisk-utils.c
tools/blktap2/drivers/tapdisk-utils.h
tools/blktap2/drivers/tapdisk2.c
tools/blktap2/vhd/lib/libvhd-journal.c

index 959fa76bf2aa0fffb5ec350416579a78acb408b3..5a3cd9a2a9fa829be257c134f096f025117eea32 100644 (file)
@@ -9,7 +9,7 @@ QCOW_UTIL  = img2qcow qcow-create qcow2raw
 LOCK_UTIL  = lock-util
 INST_DIR   = $(SBINDIR)
 
-CFLAGS    += -Werror -g -O0
+CFLAGS    += -Werror -g
 CFLAGS    += -Wno-unused
 CFLAGS    += -fno-strict-aliasing
 CFLAGS    += -I../lib -I../../libxc
index c9612a4b9b41afb00c10fe331ceb2471953233a3..a03565b65e0b53c64385cbf4c3b4bdcc2ff369a2 100644 (file)
@@ -347,11 +347,11 @@ static int ctl_open(struct tdlog_state* s, const char* name)
 static int ctl_close(struct tdlog_state* s)
 {
   while (s->connected) {
+    s->connected--;
     tapdisk_server_unregister_event(s->connections[s->connected].id);
     close(s->connections[s->connected].fd);
     s->connections[s->connected].fd = -1;
     s->connections[s->connected].id = 0;
-    s->connected--;
   }
 
   if (s->ctl.fd >= 0) {
@@ -382,7 +382,7 @@ static int ctl_close_sock(struct tdlog_state* s, int fd)
 {
   int i;
 
-  for (i = 0; i <= s->connected; i++) {
+  for (i = 0; i < s->connected; i++) {
     if (s->connections[i].fd == fd) {
       tapdisk_server_unregister_event(s->connections[i].id);
       close(s->connections[i].fd);
index 13d6c324ea1948a3486a7db229b9083d76bb6f94..a0e827a4e4959f188a0490bd3033ed1412d52b14 100644 (file)
@@ -1457,7 +1457,7 @@ int tdqcow_get_parent_id(td_driver_t *driver, td_disk_id_t *id)
 {
        off_t off;
        char *buf, *filename;
-       int len, secs, type, err = -EINVAL;
+       int len, secs, type = 0, err = -EINVAL;
        struct tdqcow_state *child  = (struct tdqcow_state *)driver->data;
 
        if (!child->backing_file_offset)
index f6da984e274f5a0f0589410aef4d1f696411d62a..58ca16049d212450b9bea0b5bff09cf6945fcd8d 100644 (file)
@@ -481,7 +481,7 @@ static char* merge_requests(struct ramdisk* ramdisk, uint64_t start,
 static int ramdisk_flush(td_driver_t *driver, struct tdremus_state* s)
 {
        uint64_t* sectors;
-       char* buf;
+       char* buf = NULL;
        uint64_t base, batchlen;
        int i, j, count = 0;
 
index 0a36c430d472f500433434a66ac6fff1da3df098..5734810ee9f246831fb861d2865016941102899e 100644 (file)
@@ -169,7 +169,7 @@ tapdisk_stream_poll_clear(struct tapdisk_stream_poll *p)
 {
        int dummy;
 
-       read(p->pipe[POLL_READ], &dummy, sizeof(dummy));
+       read_exact(p->pipe[POLL_READ], &dummy, sizeof(dummy));
        p->set = 0;
 }
 
@@ -179,7 +179,7 @@ tapdisk_stream_poll_set(struct tapdisk_stream_poll *p)
        int dummy = 0;
 
        if (!p->set) {
-               write(p->pipe[POLL_WRITE], &dummy, sizeof(dummy));
+               write_exact(p->pipe[POLL_WRITE], &dummy, sizeof(dummy));
                p->set = 1;
        }
 }
index 1220bf927e917e3a7a71b534ae5caa3f4005f4a0..d14da3241570621ccccf7d5a265801fd0b063760 100644 (file)
@@ -37,6 +37,7 @@
 #include <sys/time.h>
 
 #include "tapdisk-log.h"
+#include "tapdisk-utils.h"
 
 #define MAX_ENTRY_LEN      512
 #define MAX_ERROR_MESSAGES 16
@@ -247,7 +248,7 @@ tlog_flush(void)
        wsize = ((size + 511) & (~511));
 
        memset(tapdisk_log.buf + size, '\n', wsize - size);
-       write(fd, tapdisk_log.buf, wsize);
+       write_exact(fd, tapdisk_log.buf, wsize);
 
        tapdisk_log.p = tapdisk_log.buf;
 
index b6394bfdad0253d18d1bb27966cc423c05c0237a..1a9403828b4f61e1348c29ea3433caf550a81ac8 100644 (file)
@@ -435,7 +435,7 @@ tapdisk_lio_ack_event(struct tqueue *queue)
        uint64_t val;
 
        if (lio->flags & LIO_FLAG_EVENTFD)
-               read(lio->event_fd, &val, sizeof(val));
+               read_exact(lio->event_fd, &val, sizeof(val));
 }
 
 static void
index 8fa9d9e0bf7d5883334717e8a53bdf92a66b39d6..9d04be6adc9b337e544d2bd9d1838b3f82c60081 100644 (file)
@@ -144,7 +144,7 @@ tapdisk_stream_poll_clear(struct tapdisk_stream_poll *p)
 {
        int dummy;
 
-       read(p->pipe[POLL_READ], &dummy, sizeof(dummy));
+       read_exact(p->pipe[POLL_READ], &dummy, sizeof(dummy));
        p->set = 0;
 }
 
@@ -154,7 +154,7 @@ tapdisk_stream_poll_set(struct tapdisk_stream_poll *p)
        int dummy = 0;
 
        if (!p->set) {
-               write(p->pipe[POLL_WRITE], &dummy, sizeof(dummy));
+               write_exact(p->pipe[POLL_WRITE], &dummy, sizeof(dummy));
                p->set = 1;
        }
 }
@@ -202,7 +202,7 @@ tapdisk_stream_print_request(struct tapdisk_stream *s,
 {
        unsigned long idx = (unsigned long)tapdisk_stream_request_idx(s, sreq);
        char *buf = (char *)MMAP_VADDR(s->vbd->ring.vstart, idx, 0);
-       write(s->out_fd, buf, sreq->secs << SECTOR_SHIFT);
+       write_exact(s->out_fd, buf, sreq->secs << SECTOR_SHIFT);
 }
 
 static void
index 757c0bdc775e82418eb3ab96dcaf59d4f5b30bb1..124fce8aaba6e66181a69bde53fb69fc6e8d8e27 100644 (file)
@@ -215,3 +215,40 @@ int tapdisk_linux_version(void)
 }
 
 #endif
+int read_exact(int fd, void *data, size_t size)
+{
+    size_t offset = 0;
+    ssize_t len;
+
+    while ( offset < size )
+    {
+        len = read(fd, (char *)data + offset, size - offset);
+        if ( (len == -1) && (errno == EINTR) )
+            continue;
+        if ( len == 0 )
+            errno = 0;
+        if ( len <= 0 )
+            return -1;
+        offset += len;
+    }
+
+    return 0;
+}
+
+int write_exact(int fd, const void *data, size_t size)
+{
+    size_t offset = 0;
+    ssize_t len;
+
+    while ( offset < size )
+    {
+        len = write(fd, (const char *)data + offset, size - offset);
+        if ( (len == -1) && (errno == EINTR) )
+            continue;
+        if ( len <= 0 )
+            return -1;
+        offset += len;
+    }
+
+    return 0;
+}
index 5e08aa83265c7ad626ceabcdced787f4a1e94b68..018051634670dd0bb35bdfd62f470e93a181c2ba 100644 (file)
@@ -40,4 +40,7 @@ int tapdisk_parse_disk_type(const char *, char **, int *);
 int tapdisk_get_image_size(int, uint64_t *, uint32_t *);
 int tapdisk_linux_version(void);
 
+int read_exact(int fd, void *data, size_t size); /* EOF => -1, errno=0 */
+int write_exact(int fd, const void *data, size_t size);
+
 #endif
index 961bad6c2fb748dcd92f6fcb5323bfa959dec6b8..5804f842216193aacfb8463778990f1d1c2bd0bb 100644 (file)
@@ -330,7 +330,12 @@ tapdisk2_create_device(const char *params)
        char *path;
        int err, type;
 
-       chdir("/");
+       if (chdir("/")) {
+               DPRINTF("failed to chdir(/): %d\n", errno);
+               err = 1;
+               goto out;
+       }
+
        tapdisk_start_logging("tapdisk2");
 
        err = tapdisk2_set_child_fds();
index 24383ff62b299cbca0fb3016e73f37bf82b02f72..2cd0c9a6afc35ab115ce4bb45ac92564fe9e533a 100644 (file)
@@ -332,7 +332,7 @@ vhd_journal_write_entry(vhd_journal_t *j, vhd_journal_entry_t *entry)
 
        err = vhd_journal_write(j, &e, sizeof(vhd_journal_entry_t));
        if (err)
-               err;
+               return err;
 
        return 0;
 }