diff -x '*.o' -ur ../.orig-emacs-24.2.92/src/ChangeLog src/ChangeLog --- ../.orig-emacs-24.2.92/src/ChangeLog 2013-01-05 23:06:21.000000000 +0400 +++ src/ChangeLog 2013-01-21 12:48:58.244932711 +0400 @@ -1,3 +1,13 @@ +2013-01-21 Dmitry Antipov + + Fix crash when inserting data from non-regular files. See + http://lists.gnu.org/archive/html/emacs-devel/2013-01/msg00406.html + for the error description produced by valgrind. + * fileio.c (Finsert_file_contents): Adjust gap_size correctly. + Do not signal an I/O error too early and so do not leave not yet + decoded characters in a buffer, which was the reason of redisplay + crash. Adjust comment. + 2013-01-05 Eli Zaretskii * xdisp.c (dump_glyph): Align glyph data better. Use "pD" instead diff -x '*.o' -ur ../.orig-emacs-24.2.92/src/fileio.c src/fileio.c --- ../.orig-emacs-24.2.92/src/fileio.c 2013-01-02 00:37:17.000000000 +0400 +++ src/fileio.c 2013-01-21 12:31:51.405273880 +0400 @@ -4098,8 +4098,8 @@ /* Maybe make more room. */ if (gap_size < trytry) { - make_gap (total - gap_size); - gap_size = GAP_SIZE; + make_gap (trytry - gap_size); + gap_size = GAP_SIZE - inserted; } /* Read from the file, capturing `quit'. When an @@ -4152,8 +4152,9 @@ } } - /* Now we have read all the file data into the gap. - If it was empty, undo marking the buffer modified. */ + /* Now we have either read all the file data into the gap, + or stop reading on I/O error or quit. If nothing was + read, undo marking the buffer modified. */ if (inserted == 0) { @@ -4166,6 +4167,15 @@ else Vdeactivate_mark = Qt; + emacs_close (fd); + + /* Discard the unwind protect for closing the file. */ + specpdl_ptr--; + + if (how_much < 0) + error ("IO error reading %s: %s", + SDATA (orig_filename), emacs_strerror (errno)); + /* Make the text read part of the buffer. */ GAP_SIZE -= inserted; GPT += inserted; @@ -4179,15 +4189,6 @@ /* Put an anchor to ensure multi-byte form ends at gap. */ *GPT_ADDR = 0; - emacs_close (fd); - - /* Discard the unwind protect for closing the file. */ - specpdl_ptr--; - - if (how_much < 0) - error ("IO error reading %s: %s", - SDATA (orig_filename), emacs_strerror (errno)); - notfound: if (NILP (coding_system))