This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Cannot truncate to negative size.
authorJarkko Hietaniemi <jhi@iki.fi>
Fri, 26 Jun 2015 00:03:38 +0000 (20:03 -0400)
committerJarkko Hietaniemi <jhi@iki.fi>
Sat, 27 Jun 2015 03:09:48 +0000 (23:09 -0400)
Coverity CID 104848

pp_sys.c

index 85aa1b6..78d9584 100644 (file)
--- a/pp_sys.c
+++ b/pp_sys.c
@@ -2290,13 +2290,18 @@ PP(pp_truncate)
                         SETERRNO(EBADF,RMS_IFI);
                         result = 0;
                     } else {
-                        PerlIO_flush(fp);
+                        if (len < 0) {
+                            SETERRNO(EINVAL, LIB_INVARG);
+                            result = 0;
+                        } else {
+                           PerlIO_flush(fp);
 #ifdef HAS_TRUNCATE
-                        if (ftruncate(fd, len) < 0)
+                           if (ftruncate(fd, len) < 0)
 #else
-                        if (my_chsize(fd, len) < 0)
+                           if (my_chsize(fd, len) < 0)
 #endif
-                            result = 0;
+                               result = 0;
+                        }
                     }
                }
            }