提交 a64e3b3e 编写于 作者: J Jiri Denemark

Fix safezero()

Various safezero() implementations used either -1, errno or -errno
return values. This patch fixes them all to return -1 and set errno
appropriately.

There was also a bug in size parameter passed to safewrite() which could
result in an attempt to write gigabytes out of a megabyte buffer.
Signed-off-by: NJiri Denemark <jdenemar@redhat.com>
上级 cf104533
...@@ -316,7 +316,7 @@ static int createRawFileOpHook(int fd, void *data) { ...@@ -316,7 +316,7 @@ static int createRawFileOpHook(int fd, void *data) {
if ((r = safezero(fd, 0, hdata->vol->allocation - remain, if ((r = safezero(fd, 0, hdata->vol->allocation - remain,
bytes)) != 0) { bytes)) != 0) {
ret = errno; ret = errno;
virReportSystemError(r, _("cannot fill file '%s'"), virReportSystemError(errno, _("cannot fill file '%s'"),
hdata->vol->target.path); hdata->vol->target.path);
goto cleanup; goto cleanup;
} }
...@@ -327,7 +327,7 @@ static int createRawFileOpHook(int fd, void *data) { ...@@ -327,7 +327,7 @@ static int createRawFileOpHook(int fd, void *data) {
if ((r = safezero(fd, 0, 0, remain)) != 0) { if ((r = safezero(fd, 0, 0, remain)) != 0) {
ret = errno; ret = errno;
virReportSystemError(r, _("cannot fill file '%s'"), virReportSystemError(errno, _("cannot fill file '%s'"),
hdata->vol->target.path); hdata->vol->target.path);
goto cleanup; goto cleanup;
} }
......
...@@ -146,11 +146,11 @@ int safezero(int fd, int flags ATTRIBUTE_UNUSED, off_t offset, off_t len) ...@@ -146,11 +146,11 @@ int safezero(int fd, int flags ATTRIBUTE_UNUSED, off_t offset, off_t len)
*/ */
r = ftruncate(fd, offset + len); r = ftruncate(fd, offset + len);
if (r < 0) if (r < 0)
return -errno; return -1;
buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset); buf = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
if (buf == MAP_FAILED) if (buf == MAP_FAILED)
return -errno; return -1;
memset(buf, 0, len); memset(buf, 0, len);
munmap(buf, len); munmap(buf, len);
...@@ -167,24 +167,26 @@ int safezero(int fd, int flags ATTRIBUTE_UNUSED, off_t offset, off_t len) ...@@ -167,24 +167,26 @@ int safezero(int fd, int flags ATTRIBUTE_UNUSED, off_t offset, off_t len)
unsigned long long remain, bytes; unsigned long long remain, bytes;
if (lseek(fd, offset, SEEK_SET) < 0) if (lseek(fd, offset, SEEK_SET) < 0)
return errno; return -1;
/* Split up the write in small chunks so as not to allocate lots of RAM */ /* Split up the write in small chunks so as not to allocate lots of RAM */
remain = len; remain = len;
bytes = 1024 * 1024; bytes = 1024 * 1024;
r = VIR_ALLOC_N(buf, bytes); r = VIR_ALLOC_N(buf, bytes);
if (r < 0) if (r < 0) {
return -ENOMEM; errno = ENOMEM;
return -1;
}
while (remain) { while (remain) {
if (bytes > remain) if (bytes > remain)
bytes = remain; bytes = remain;
r = safewrite(fd, buf, len); r = safewrite(fd, buf, bytes);
if (r < 0) { if (r < 0) {
VIR_FREE(buf); VIR_FREE(buf);
return r; return -1;
} }
/* safewrite() guarantees all data will be written */ /* safewrite() guarantees all data will be written */
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册