From: Karl Waclawek Date: Wed, 28 Jun 2006 02:55:51 +0000 (+0000) Subject: Fix for bug #1513566: filemap() in readfilemap.c doesn't handle zero length X-Git-Tag: R_2_0_1~55 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f18b068f383c80752d9ff7a371ea81b5a4febb66;p=libexpat Fix for bug #1513566: filemap() in readfilemap.c doesn't handle zero length files, and the same issue applies to filemap() in unixfilemap.c --- diff --git a/expat/xmlwf/readfilemap.c b/expat/xmlwf/readfilemap.c index 42b5e038..0a91f08b 100755 --- a/expat/xmlwf/readfilemap.c +++ b/expat/xmlwf/readfilemap.c @@ -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); diff --git a/expat/xmlwf/unixfilemap.c b/expat/xmlwf/unixfilemap.c index 22048c82..93adce32 100755 --- a/expat/xmlwf/unixfilemap.c +++ b/expat/xmlwf/unixfilemap.c @@ -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) {