]> granicus.if.org Git - libexpat/commitdiff
Fix for bug #1513566: filemap() in readfilemap.c doesn't handle zero length
authorKarl Waclawek <kwaclaw@users.sourceforge.net>
Wed, 28 Jun 2006 02:55:51 +0000 (02:55 +0000)
committerKarl Waclawek <kwaclaw@users.sourceforge.net>
Wed, 28 Jun 2006 02:55:51 +0000 (02:55 +0000)
files, and the same issue applies to filemap() in unixfilemap.c

expat/xmlwf/readfilemap.c
expat/xmlwf/unixfilemap.c

index 42b5e03824e7a9bbdeb443ad0956724dfc25ef2e..0a91f08bb25e65566791affcd0a98762f3eff27c 100755 (executable)
@@ -57,9 +57,17 @@ filemap(const char *name,
     return 0;
   }
   nbytes = sb.st_size;
+  /* malloc will return NULL with nbytes == 0, handle files with size 0 */
+  if (nbytes == 0) {
+    static const char c = '\0';
+    processor(&c, 0, name, arg);
+    close(fd);
+    return 1;
+  }
   p = malloc(nbytes);
   if (!p) {
     fprintf(stderr, "%s: out of memory\n", name);
+    close(fd);
     return 0;
   }
   n = read(fd, p, nbytes);
index 22048c82ed7691b39c6b4b2b7e7d8f9fb15b8433..93adce32e8268993014ba2413babcd7c922c0749 100755 (executable)
@@ -44,6 +44,13 @@ filemap(const char *name,
   }
 
   nbytes = sb.st_size;
+  /* mmap fails for zero length files */
+  if (nbytes == 0) {
+    static const char c = '\0';
+    processor(&c, 0, name, arg);
+    close(fd);
+    return 1;
+  }
   p = (void *)mmap((caddr_t)0, (size_t)nbytes, PROT_READ,
                    MAP_FILE|MAP_PRIVATE, fd, (off_t)0);
   if (p == (void *)-1) {