Fix crash on adding items near book end
authorH.G. Muller <h.g.muller@hccnet.nl>
Wed, 12 Oct 2011 15:43:33 +0000 (17:43 +0200)
committerH.G. Muller <h.g.muller@hccnet.nl>
Wed, 12 Oct 2011 15:56:40 +0000 (17:56 +0200)
When Edit Book would add so many entries near the end of an opening
book that the end of the added info would surpass the old end of the
file, the copying of the tail got stuck in an infinite loop, extending
the file without limit with repeating info.

book.c

diff --git a/book.c b/book.c
index e42b9c6..6b1198d 100644 (file)
--- a/book.c
+++ b/book.c
@@ -676,8 +676,10 @@ void SaveToBook(char *text)
     if(count != currentCount) {
        do {
            for(i=0; i<len1; i++) buf2[i] = buf1[i]; len2 = len1;
-           fseek(f, readpos, SEEK_SET);
-           readpos += len1 = fread(buf1, 1, 4096, f);
+           if(readpos > writepos) {
+               fseek(f, readpos, SEEK_SET);
+               readpos += len1 = fread(buf1, 1, 4096, f);
+           } else len1 = 0; // wrote already past old EOF
            fseek(f, writepos, SEEK_SET);
            fwrite(buf2, 1, len2, f);
            writepos += len2;