]> git.eshelyaron.com Git - emacs.git/commitdiff
Refactor negative file size checking
authorPaul Eggert <eggert@cs.ucla.edu>
Sat, 12 Jul 2025 19:13:32 +0000 (12:13 -0700)
committerEshel Yaron <me@eshelyaron.com>
Thu, 24 Jul 2025 08:48:35 +0000 (10:48 +0200)
* src/fileio.c (Finsert_file_contents): Check for negative file
sizes earlier, as it’s easy, avoids some unnecessary work, and
will simplify later changes.

(cherry picked from commit aa172be7d05f537509c37ef557774e550eecda7e)

src/fileio.c

index 24efff21e4c3749d2f422f5f61b8b4543a013fbd..ec9ec6bd3c12f597280b34b651deedb9ee876435 100644 (file)
@@ -4092,6 +4092,9 @@ by calling `format-decode', which see.  */)
   ptrdiff_t same_at_end_charpos = ZV;
   bool seekable = true;
 
+  /* A hint about the file size, or -1 if there is no hint.  */
+  off_t file_size_hint = -1;
+
   if (current_buffer->base_buffer && ! NILP (visit))
     error ("Cannot do file visiting in an indirect buffer");
 
@@ -4136,9 +4139,6 @@ by calling `format-decode', which see.  */)
 
   filename = ENCODE_FILE (filename);
 
-  /* A hint about the file size, or -1 if there is no hint.  */
-  off_t file_size_hint;
-
   fd = emacs_fd_open (SSDATA (filename), O_RDONLY, 0);
   if (!emacs_fd_valid_p (fd))
     {
@@ -4146,7 +4146,6 @@ by calling `format-decode', which see.  */)
       if (NILP (visit))
        report_file_error ("Opening input file", orig_filename);
       mtime = time_error_value (save_errno);
-      file_size_hint = -1;
       if (!NILP (Vcoding_system_for_read))
        {
          /* Don't let invalid values into buffer-file-coding-system.  */
@@ -4174,7 +4173,17 @@ by calling `format-decode', which see.  */)
       report_file_error ("Input file status", orig_filename);
     regular = S_ISREG (st.st_mode) != 0;
     bool memory_object = S_TYPEISSHM (&st) || S_TYPEISTMO (&st);
-    file_size_hint = regular | memory_object ? st.st_size : -1;
+
+    if (regular | memory_object)
+      {
+       file_size_hint = st.st_size;
+
+       /* A negative size can happen on a platform that allows file
+          sizes greater than the maximum off_t value.  */
+       if (file_size_hint < 0)
+         buffer_overflow ();
+      }
+
     mtime = (memory_object
             ? make_timespec (0, UNKNOWN_MODTIME_NSECS)
             : get_stat_mtime (&st));
@@ -4221,11 +4230,6 @@ by calling `format-decode', which see.  */)
        {
          end_offset = file_size_hint;
 
-         /* A negative size can happen on a platform that allows file
-            sizes greater than the maximum off_t value.  */
-         if (end_offset < 0)
-           buffer_overflow ();
-
          /* The file size returned from fstat may be zero, but data
             may be readable nonetheless, for example when this is a
             file in the /proc filesystem.  */