patch(1): Don't try to mmap a zero length file.
authorSascha Wildner <saw@online.de>
Sun, 25 Sep 2011 00:14:58 +0000 (02:14 +0200)
committerSascha Wildner <saw@online.de>
Sun, 25 Sep 2011 00:19:33 +0000 (02:19 +0200)
Found-in: NetBSD

usr.bin/patch/inp.c

index 6ad5eb4..f61b317 100644 (file)
@@ -1,6 +1,5 @@
 /*
  * $OpenBSD: inp.c,v 1.34 2006/03/11 19:41:30 otto Exp $
- * $DragonFly: src/usr.bin/patch/inp.c,v 1.6 2007/09/29 23:11:10 swildner Exp $
  */
 
 /*
@@ -256,12 +255,16 @@ plan_a(const char *filename)
        if ((ifd = open(filename, O_RDONLY)) < 0)
                pfatal("can't open file %s", filename);
 
-       i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0);
-       if (i_womp == MAP_FAILED) {
-               perror("mmap failed");
+       if (i_size) {
+               i_womp = mmap(NULL, i_size, PROT_READ, MAP_PRIVATE, ifd, 0);
+               if (i_womp == MAP_FAILED) {
+                       perror("mmap failed");
+                       i_womp = NULL;
+                       close(ifd);
+                       return false;
+               }
+       } else {
                i_womp = NULL;
-               close(ifd);
-               return false;
        }
 
        close(ifd);