[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] [xen stable-4.2] tools/libxc: Improve xc_dom_malloc_filemap() error handling



commit 30b447521114411374a42340475ee31e22a60ad1
Author:     Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
AuthorDate: Mon Nov 25 11:05:49 2013 +0000
Commit:     Ian Jackson <Ian.Jackson@xxxxxxxxxxxxx>
CommitDate: Thu Jan 9 12:38:47 2014 +0000

    tools/libxc: Improve xc_dom_malloc_filemap() error handling
    
    Coverity ID 1055563
    
    In the original function, mmap() could be called with a length of -1 if the
    second lseek failed and the caller had not provided max_size.
    
    While fixing up this error, improve the logging of other error paths.  I 
know
    from personal experience that debugging failures function is rather 
difficult
    given only "xc_dom_malloc_filemap: failed (on file <somefile>)" in the logs.
    
    Signed-off-by: Andrew Cooper <andrew.cooper3@xxxxxxxxxx>
    CC: Ian Campbell <Ian.Campbell@xxxxxxxxxx>
    Acked-by: Ian Jackson <Ian.Jackson@xxxxxxxxxxxxx>
    (cherry picked from commit c635c1ef7833e7505423f6567bf99bd355101587)
    (cherry picked from commit a5febe4aeff4ab80ce0411f63f336c25951098cf)
---
 tools/libxc/xc_dom_core.c |   35 ++++++++++++++++++++++++++++-------
 1 files changed, 28 insertions(+), 7 deletions(-)

diff --git a/tools/libxc/xc_dom_core.c b/tools/libxc/xc_dom_core.c
index e79e38d..73ad976 100644
--- a/tools/libxc/xc_dom_core.c
+++ b/tools/libxc/xc_dom_core.c
@@ -176,13 +176,25 @@ void *xc_dom_malloc_filemap(struct xc_dom_image *dom,
 {
     struct xc_dom_mem *block = NULL;
     int fd = -1;
+    off_t offset;
 
     fd = open(filename, O_RDONLY);
-    if ( fd == -1 )
+    if ( fd == -1 ) {
+        xc_dom_panic(dom->xch, XC_INTERNAL_ERROR,
+                     "failed to open file: %s",
+                     strerror(errno));
+        goto err;
+    }
+
+    if ( (lseek(fd, 0, SEEK_SET) == -1) ||
+         ((offset = lseek(fd, 0, SEEK_END)) == -1) ) {
+        xc_dom_panic(dom->xch, XC_INTERNAL_ERROR,
+                     "failed to seek on file: %s",
+                     strerror(errno));
         goto err;
+    }
 
-    lseek(fd, 0, SEEK_SET);
-    *size = lseek(fd, 0, SEEK_END);
+    *size = offset;
 
     if ( max_size && *size > max_size )
     {
@@ -192,14 +204,24 @@ void *xc_dom_malloc_filemap(struct xc_dom_image *dom,
     }
 
     block = malloc(sizeof(*block));
-    if ( block == NULL )
+    if ( block == NULL ) {
+        xc_dom_panic(dom->xch, XC_OUT_OF_MEMORY,
+                     "failed to allocate block (%zu bytes)",
+                     sizeof(*block));
         goto err;
+    }
+
     memset(block, 0, sizeof(*block));
     block->mmap_len = *size;
     block->mmap_ptr = mmap(NULL, block->mmap_len, PROT_READ,
                            MAP_SHARED, fd, 0);
-    if ( block->mmap_ptr == MAP_FAILED )
+    if ( block->mmap_ptr == MAP_FAILED ) {
+        xc_dom_panic(dom->xch, XC_INTERNAL_ERROR,
+                     "failed to mmap file: %s",
+                     strerror(errno));
         goto err;
+    }
+
     block->next = dom->memblocks;
     dom->memblocks = block;
     dom->alloc_malloc += sizeof(*block);
@@ -212,8 +234,7 @@ void *xc_dom_malloc_filemap(struct xc_dom_image *dom,
  err:
     if ( fd != -1 )
         close(fd);
-    if ( block != NULL )
-        free(block);
+    free(block);
     DOMPRINTF("%s: failed (on file `%s')", __FUNCTION__, filename);
     return NULL;
 }
--
generated by git-patchbot for /home/xen/git/xen.git#stable-4.2

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.