PR/27283: Onno van der Linden: incorrect check for mmap return value

While I am here, clean up the error path, KNF and use the proper stat
macro.
This commit is contained in:
christos 2004-10-17 22:21:43 +00:00
parent 4cec10fd22
commit 366176888a

View File

@ -1,4 +1,4 @@
/* $NetBSD: citrus_mmap.c,v 1.1 2003/06/25 09:51:37 tshiozak Exp $ */ /* $NetBSD: citrus_mmap.c,v 1.2 2004/10/17 22:21:43 christos Exp $ */
/*- /*-
* Copyright (c)2003 Citrus Project, * Copyright (c)2003 Citrus Project,
@ -28,7 +28,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#if defined(LIBC_SCCS) && !defined(lint) #if defined(LIBC_SCCS) && !defined(lint)
__RCSID("$NetBSD: citrus_mmap.c,v 1.1 2003/06/25 09:51:37 tshiozak Exp $"); __RCSID("$NetBSD: citrus_mmap.c,v 1.2 2004/10/17 22:21:43 christos Exp $");
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include "namespace.h" #include "namespace.h"
@ -48,38 +48,39 @@ __RCSID("$NetBSD: citrus_mmap.c,v 1.1 2003/06/25 09:51:37 tshiozak Exp $");
int int
_citrus_map_file(struct _citrus_region * __restrict r, _citrus_map_file(struct _citrus_region * __restrict r,
const char * __restrict path) const char * __restrict path)
{ {
int fd, ret; int fd, ret = 0;
struct stat st; struct stat st;
void *head; void *head;
_DIAGASSERT(r != NULL); _DIAGASSERT(r != NULL);
_region_init(r, NULL, 0); _region_init(r, NULL, 0);
fd = open(path, O_RDONLY);
if (fd<0) if ((fd = open(path, O_RDONLY)) == -1)
return (errno); return errno;
if (fstat(fd, &st)) {
if (fstat(fd, &st) == -1) {
ret = errno; ret = errno;
close(fd); goto error;
return ret;
} }
if ((st.st_mode & S_IFMT) != S_IFREG) { if (!S_ISREG(st.st_mode)) {
close(fd); ret = EOPNOTSUPP;
return EOPNOTSUPP; goto error;
} }
head = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_PRIVATE, head = mmap(NULL, (size_t)st.st_size, PROT_READ, MAP_FILE|MAP_PRIVATE,
fd, (off_t)0); fd, (off_t)0);
if (!head) { if (head == MAP_FAILED) {
ret = errno; ret = errno;
close(fd); goto error;
return ret;
} }
_region_init(r, head, (size_t)st.st_size); _region_init(r, head, (size_t)st.st_size);
close(fd);
return 0; error:
(void)close(fd);
return ret;
} }
void void
@ -89,7 +90,7 @@ _citrus_unmap_file(struct _citrus_region *r)
_DIAGASSERT(r != NULL); _DIAGASSERT(r != NULL);
if (_region_head(r) != NULL) { if (_region_head(r) != NULL) {
munmap(_region_head(r), _region_size(r)); (void)munmap(_region_head(r), _region_size(r));
_region_init(r, NULL, 0); _region_init(r, NULL, 0);
} }
} }