From 8b6549519bb4a49f7afb55b6f5dd11a5692498dd Mon Sep 17 00:00:00 2001 From: Markus Armbruster Date: Mon, 24 Oct 2005 20:36:17 +0000 Subject: [PATCH] (ef_open): Portability fix: malloc(0) may return null, which is not a failure. fillcache() chokes on null cache, so don't call it for empty files. --- src/lib/common/file.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/lib/common/file.c b/src/lib/common/file.c index 34a8d27b..f34c7e72 100644 --- a/src/lib/common/file.c +++ b/src/lib/common/file.c @@ -95,17 +95,21 @@ ef_open(int type, int how) else ep->csize = max(1, blksize(fd) / ep->size); size = ep->csize * ep->size; - ep->cache = malloc(size); - if (ep->cache == NULL) { - logerror("Can't open %s: out of memory", ep->file); - close(fd); - return 0; + if (size) { + ep->cache = malloc(size); + if (ep->cache == NULL) { + logerror("Can't open %s: out of memory", ep->file); + close(fd); + return 0; + } + } else { + ep->cache = NULL; } ep->baseid = 0; ep->cids = 0; ep->flags = (ep->flags & ~EFF_OPEN) | (how ^ ~EFF_CREATE); ep->fd = fd; - if (how & EFF_MEM) { + if ((how & EFF_MEM) && ep->fids) { if (fillcache(ep, 0) != ep->fids) { ep->cids = 0; /* prevent cache flush */ ep->flags &= ~EFF_OPEN; /* maintain invariant */