From: Josh Durgin Date: Fri, 29 Mar 2013 23:48:02 +0000 (-0700) Subject: rbd: fail import-diff if we reach the end of the stream sooner than expected X-Git-Tag: v0.62~118^2~12 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=c0e5c22dfdea35b5325982dcef170c331570b709;p=ceph.git rbd: fail import-diff if we reach the end of the stream sooner than expected safe_read() just protects against EINTR, and may return less data than requested if it reaches the end of the file. Use safe_read_exact() to make sure we get the right amount of data. Signed-off-by: Josh Durgin --- diff --git a/src/rbd.cc b/src/rbd.cc index f3158396f2aa..9fa0b4fece73 100644 --- a/src/rbd.cc +++ b/src/rbd.cc @@ -1348,7 +1348,7 @@ static int read_string(int fd, unsigned max, string *out) { char buf[4]; - int r = safe_read(fd, buf, 4); + int r = safe_read_exact(fd, buf, 4); if (r < 0) return r; @@ -1361,7 +1361,7 @@ static int read_string(int fd, unsigned max, string *out) return -EINVAL; char sbuf[len]; - r = safe_read(fd, sbuf, len); + r = safe_read_exact(fd, sbuf, len); if (r < 0) return r; out->assign(sbuf, len); @@ -1394,7 +1394,7 @@ static int do_import_diff(librbd::Image &image, const char *path) } char buf[15]; - r = safe_read(fd, buf, strlen(RBD_DIFF_BANNER)); + r = safe_read_exact(fd, buf, strlen(RBD_DIFF_BANNER)); if (r < 0) goto done; buf[strlen(RBD_DIFF_BANNER)] = '\0'; @@ -1406,9 +1406,10 @@ static int do_import_diff(librbd::Image &image, const char *path) while (true) { __u8 tag; - r = safe_read(fd, &tag, 1); - if (r <= 0) + r = safe_read_exact(fd, &tag, 1); + if (r < 0) { goto done; + } if (tag == 'e') { dout(2) << " end diff" << dendl; @@ -1440,7 +1441,7 @@ static int do_import_diff(librbd::Image &image, const char *path) } else if (tag == 's') { uint64_t end_size; char buf[8]; - r = safe_read(fd, buf, 8); + r = safe_read_exact(fd, buf, 8); if (r < 0) goto done; bufferlist bl; @@ -1460,7 +1461,7 @@ static int do_import_diff(librbd::Image &image, const char *path) } else if (tag == 'w' || tag == 'z') { uint64_t len; char buf[16]; - r = safe_read(fd, buf, 16); + r = safe_read_exact(fd, buf, 16); if (r < 0) goto done; bufferlist bl; @@ -1471,7 +1472,7 @@ static int do_import_diff(librbd::Image &image, const char *path) if (tag == 'w') { bufferptr bp = buffer::create(len); - r = safe_read(fd, bp.c_str(), len); + r = safe_read_exact(fd, bp.c_str(), len); if (r < 0) goto done; bufferlist data;