]> git.eshelyaron.com Git - emacs.git/commit
copy-file no longer trusts st_size
authorPaul Eggert <eggert@cs.ucla.edu>
Wed, 22 Jan 2025 19:06:06 +0000 (11:06 -0800)
committerEshel Yaron <me@eshelyaron.com>
Thu, 24 Jul 2025 08:47:21 +0000 (10:47 +0200)
commit65bd9993a796b29220c09746b7319379b018d895
tree88b4e6f48cc58034aafc9e5e7fc8817ae6a8c4ec
parent8e5ef348968f9cccef21f83b940c0ffad2ebe31d
copy-file no longer trusts st_size

In copy-file, do not trust st_size, since it might change as we run,
or we might be in a /proc system where it is unreliable anyway.
Also, fix some other unlikely copy-file bugs while we’re here.
* src/fileio.c (Fcopy_file): Use O_TRUNC when opening a
destination that already exists.  This saves us the trouble
of having to call ftruncate with a possibly-bogus st_size;
the old motivation for using ftruncate is no longer compelling.
Do not assume ptrdiff_t is as wide as ssize_t; although this is
true on all known platforms, it’s easy to not assume it.
Don’t trust st_size.  Prefer SSIZE_MAX to TYPE_MAXIMUM (ssize_t).
Always read+write, regardless of whether copy_file_range failed.

(cherry picked from commit ffd65be2277b9a30e77a00ad69c9ba21459f72c5)
src/fileio.c