Skip to content

Commit

Permalink
- check for MAP_FAILED before unmapping.
Browse files Browse the repository at this point in the history
- reorder setting of the flags, so that we act properly on error.
Might fix PR/494
  • Loading branch information
zoulasc committed Nov 9, 2015
1 parent f6b9697 commit 0087ada
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/apprentice.c
Expand Up @@ -32,7 +32,7 @@
#include "file.h"

#ifndef lint
FILE_RCSID("@(#)$File: apprentice.c,v 1.241 2015/09/16 22:52:54 christos Exp $")
FILE_RCSID("@(#)$File: apprentice.c,v 1.242 2015/10/31 15:37:17 christos Exp $")
#endif /* lint */

#include "magic.h"
Expand Down Expand Up @@ -548,7 +548,7 @@ apprentice_unmap(struct magic_map *map)
switch (map->type) {
#ifdef QUICK
case MAP_TYPE_MMAP:
if (map->p)
if (map->p && map->p != MAP_FAILED)
(void)munmap(map->p, map->len);
break;
#endif
Expand Down Expand Up @@ -2946,13 +2946,14 @@ apprentice_map(struct magic_set *ms, const char *fn)

map->len = (size_t)st.st_size;
#ifdef QUICK
map->type = MAP_TYPE_MMAP;
if ((map->p = mmap(0, (size_t)st.st_size, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FILE, fd, (off_t)0)) == MAP_FAILED) {
file_error(ms, errno, "cannot map `%s'", dbname);
goto error;
}
map->type = MAP_TYPE_MMAP;
#else
map->type = MAP_TYPE_MALLOC;
if ((map->p = CAST(void *, malloc(map->len))) == NULL) {
file_oomem(ms, map->len);
goto error;
Expand All @@ -2961,7 +2962,6 @@ apprentice_map(struct magic_set *ms, const char *fn)
file_badread(ms);
goto error;
}
map->type = MAP_TYPE_MALLOC;
#define RET 1
#endif
(void)close(fd);
Expand Down

2 comments on commit 0087ada

@osm0sis
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately still does not fix PR/494, please check the issue Notes again as I've found the breaking change in your commit history. 👍

@glensc
Copy link
Member

@glensc glensc commented on 0087ada Nov 12, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is read-only mirror of file CVS repository, updated every half hour. NOTE: do not make pull requests here, nor comment any commits, submit them usual way to bug tracker or to the mailing list. Maintainer(s) are not tracking this git mirror

Please sign in to comment.