Whamcloud - gitweb
resize2fs: Add sanity check for off_t overflow before truncating
authorTheodore Ts'o <tytso@mit.edu>
Mon, 21 Jan 2008 18:43:18 +0000 (13:43 -0500)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 21 Jan 2008 18:43:18 +0000 (13:43 -0500)
If we can't use ftruncate64(), and have to use ftruncate() instead,
make sure that we don't accidentally truncate the size when we chop it
down to an off_t before calling ftruncate(), lest we severely damage a
filesystem image file.

Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
resize/main.c

index 7db4ebc..b3f80f7 100644 (file)
@@ -416,7 +416,9 @@ int main (int argc, char ** argv)
 #ifdef HAVE_FSTAT64
                ftruncate64(fd, new_file_size);
 #else
-               ftruncate(fd, (off_t) new_file_size);
+               /* Only truncate if new_file_size doesn't overflow off_t */
+               if (((off_t) new_file_size) == new_file_size)
+                       ftruncate(fd, (off_t) new_file_size);
 #endif
        }
        if (fd > 0)