Use MAP_FAILED instead of double cast
PATCH 1/1 Use MAP_FAILED instead of double cast
Hello!
I have found some really ugly code to used to check the result of mmap():
if (-1 == (int)(long)map)
return;
Double cast almost always indicates that something fishy is going on. Indeed, mmap() could (in theory) return 0xffffffff on a 64-bit platform, and that would be misinterpreted as a failure.
There is a preprocessor symbol MAP_FAILED that should be used instead. It's used already in some places, so let's use it consistently.
Signed-off-by Pavel Roskin <[email protected]>
diffcore-order.c +1 -1
@@ -28,7 +28,7 @@ static void prepare_order(const char *or } map =mmap (NULL , st.st_size,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 );close (fd); -if (-1 == (int )(long )map) +if (map ==MAP_FAILED )return ; endp = map + st.st_size;for (pass =0 ; pass <2 ; pass++) {
local-pull.c +1 -1
@@ -54,7 +54,7 @@ int fetch(unsigned char *sha1) } map =mmap (NULL , st.st_size,PROT_READ ,MAP_PRIVATE , ifd,0 );close (ifd); -if (-1 == (int )(long )map) { +if (map ==MAP_FAILED ) {fprintf (stderr,"cannot mmap %s\n" , filename);return -1 ; }
read-cache.c +1 -1
@@ -376,7 +376,7 @@ int read_cache(void) map =mmap (NULL , size,PROT_READ |PROT_WRITE ,MAP_PRIVATE , fd,0 ); }close (fd); -if (-1 == (int )(long )map) +if (map ==MAP_FAILED )return error ("mmap failed" ); hdr = map;
sha1_file.c +2 -2
@@ -507,7 +507,7 @@ static void *map_sha1_file_internal(cons } map =mmap (NULL , st.st_size,PROT_READ ,MAP_PRIVATE , fd,0 );close (fd); -if (-1 == (int )(long )map) +if (map ==MAP_FAILED )return NULL ; *size = st.st_size;return map;
@@ -1293,7 +1293,7 @@ int index_fd(unsigned char *sha1, int fdif (size) buf =mmap (NULL , size,PROT_READ ,MAP_PRIVATE , fd,0 );close (fd); -if ((int )(long )buf ==-1 ) +if (buf ==MAP_FAILED )return -1 ; ret =write_sha1_file (buf, size,"blob" , sha1);
Signature · 2 lines
Regards, Pavel Roskin