ftell: do not assume wraparound signed arithmetic
authorPaul Eggert <eggert@cs.ucla.edu>
Sun, 24 Jul 2011 16:42:14 +0000 (09:42 -0700)
committerPaul Eggert <eggert@cs.ucla.edu>
Sun, 24 Jul 2011 16:43:42 +0000 (09:43 -0700)
* lib/ftell.c: Include <limits.h>.
(ftell): Don't assume wraparound signed arithmetic.

ChangeLog
lib/ftell.c

index 7b6984cbc0f9c4f4b9c96242eda9b4e49487563d..63c0367648ae1c606fbdf80eeb2af3691c873965 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
 
 2011-07-24  Paul Eggert  <eggert@cs.ucla.edu>
 
+       ftell: do not assume wraparound signed arithmetic
+       * lib/ftell.c: Include <limits.h>.
+       (ftell): Don't assume wraparound signed arithmetic.
+
        * README: Modernize discussion of signed integers.
        Assuming overflow wraparound is no longer safe.
        Mention ones' complement and signed magnitude.
index 79083fb4965bcfd642a8ab36fa4d40c65e6236f3..babba353ba8208f5f77f5a01f536b55b7c947a29 100644 (file)
@@ -20,6 +20,7 @@
 #include <stdio.h>
 
 #include <errno.h>
+#include <limits.h>
 /* Get off_t.  */
 #include <unistd.h>
 
@@ -28,7 +29,7 @@ ftell (FILE *fp)
 {
   /* Use the replacement ftello function with all its workarounds.  */
   off_t offset = ftello (fp);
-  if (offset == (long)offset)
+  if (LONG_MIN <= offset && offset <= LONG_MAX)
     return (long)offset;
   else
     {