From e69dafade3ccd5c2997a3f6fd6c7bea29e75ad8e Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 14 Jun 2011 18:07:35 -0700 Subject: [PATCH] * fileio.c: Don't assume EMACS_INT fits in off_t. (emacs_lseek): New static function. (Finsert_file_contents, Fwrite_region): Use it. Use SEEK_SET, SEEK_CUR, SEEK_END as appropriate. --- src/ChangeLog | 7 +++++++ src/fileio.c | 29 +++++++++++++++++++++-------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index 35dab796be7..e477162b32b 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,10 @@ +2011-06-15 Paul Eggert + + * fileio.c: Don't assume EMACS_INT fits in off_t. + (emacs_lseek): New static function. + (Finsert_file_contents, Fwrite_region): Use it. + Use SEEK_SET, SEEK_CUR, SEEK_END as appropriate. + 2011-06-14 Paul Eggert * fns.c (Fload_average): Don't assume 100 * load average fits in int. diff --git a/src/fileio.c b/src/fileio.c index 9a08e11e27e..2e0b3831e8d 100644 --- a/src/fileio.c +++ b/src/fileio.c @@ -3109,6 +3109,19 @@ read_non_regular_quit (Lisp_Object ignore) return Qnil; } +/* Reposition FD to OFFSET, based on WHENCE. This acts like lseek + except that it also tests for OFFSET being out of lseek's range. */ +static off_t +emacs_lseek (int fd, EMACS_INT offset, int whence) +{ + if (! (TYPE_MINIMUM (off_t) <= offset && offset <= TYPE_MAXIMUM (off_t))) + { + errno = EINVAL; + return -1; + } + return lseek (fd, offset, whence); +} + DEFUN ("insert-file-contents", Finsert_file_contents, Sinsert_file_contents, 1, 5, 0, @@ -3317,7 +3330,7 @@ variable `last-coding-system-used' to the coding system actually used. */) nread = emacs_read (fd, read_buf, 1024); if (nread >= 0) { - if (lseek (fd, st.st_size - (1024 * 3), 0) < 0) + if (lseek (fd, st.st_size - (1024 * 3), SEEK_SET) < 0) report_file_error ("Setting file position", Fcons (orig_filename, Qnil)); nread += emacs_read (fd, read_buf + nread, 1024 * 3); @@ -3361,7 +3374,7 @@ variable `last-coding-system-used' to the coding system actually used. */) specpdl_ptr--; /* Rewind the file for the actual read done later. */ - if (lseek (fd, 0, 0) < 0) + if (lseek (fd, 0, SEEK_SET) < 0) report_file_error ("Setting file position", Fcons (orig_filename, Qnil)); } @@ -3428,7 +3441,7 @@ variable `last-coding-system-used' to the coding system actually used. */) if (XINT (beg) != 0) { - if (lseek (fd, XINT (beg), 0) < 0) + if (emacs_lseek (fd, XINT (beg), SEEK_SET) < 0) report_file_error ("Setting file position", Fcons (orig_filename, Qnil)); } @@ -3500,7 +3513,7 @@ variable `last-coding-system-used' to the coding system actually used. */) break; /* How much can we scan in the next step? */ trial = min (curpos, sizeof buffer); - if (lseek (fd, curpos - trial, 0) < 0) + if (emacs_lseek (fd, curpos - trial, SEEK_SET) < 0) report_file_error ("Setting file position", Fcons (orig_filename, Qnil)); @@ -3618,7 +3631,7 @@ variable `last-coding-system-used' to the coding system actually used. */) /* First read the whole file, performing code conversion into CONVERSION_BUFFER. */ - if (lseek (fd, XINT (beg), 0) < 0) + if (emacs_lseek (fd, XINT (beg), SEEK_SET) < 0) report_file_error ("Setting file position", Fcons (orig_filename, Qnil)); @@ -3817,7 +3830,7 @@ variable `last-coding-system-used' to the coding system actually used. */) if (XINT (beg) != 0 || !NILP (replace)) { - if (lseek (fd, XINT (beg), 0) < 0) + if (emacs_lseek (fd, XINT (beg), SEEK_SET) < 0) report_file_error ("Setting file position", Fcons (orig_filename, Qnil)); } @@ -4549,9 +4562,9 @@ This calls `write-region-annotate-functions' at the start, and long ret; if (NUMBERP (append)) - ret = lseek (desc, XINT (append), 1); + ret = emacs_lseek (desc, XINT (append), SEEK_CUR); else - ret = lseek (desc, 0, 2); + ret = lseek (desc, 0, SEEK_END); if (ret < 0) { #ifdef CLASH_DETECTION -- 2.39.2