Fix memory overwrite when increasing display width in mid-editing.
authorsimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Thu, 26 Apr 2007 08:44:41 +0000 (08:44 +0000)
committersimon <simon@cda61777-01e9-0310-a592-d414129be87e>
Thu, 26 Apr 2007 08:44:41 +0000 (08:44 +0000)
(That operation increases the amount of data displayed on screen,
but failed to trigger a realloc of the buffer used to hold that data
during display.)

git-svn-id: svn://svn.tartarus.org/sgt/tweak@7478 cda61777-01e9-0310-a592-d414129be87e

main.c

diff --git a/main.c b/main.c
index 7394454..ae9e491 100644 (file)
--- a/main.c
+++ b/main.c
@@ -469,7 +469,7 @@ int backup_file (void) {
 }
 
 static unsigned char *scrbuf = NULL;
-static int scrbuflines = 0;
+static int scrbufsize = 0;
 
 /*
  * Draw the screen, for normal usage.
@@ -484,16 +484,15 @@ void draw_scr (void) {
     char *linebuf;
 
     scrlines = display_rows - 2;
-    if (scrlines > scrbuflines) {
-       scrbuf = (scrbuf ?
-                 realloc(scrbuf, scrlines*width) :
-                 malloc(scrlines*width));
+    scrsize = scrlines * width;
+    if (scrsize > scrbufsize) {
+       scrbuf = (scrbuf ? realloc(scrbuf, scrsize) : malloc(scrsize));
        if (!scrbuf) {
            done();
            fprintf(stderr, "%s: out of memory!\n", pname);
            exit (2);
        }
-       scrbuflines = scrlines;
+       scrbufsize = scrsize;
     }
 
     linebuf = malloc(width*4+20);
@@ -509,7 +508,8 @@ void draw_scr (void) {
        scroff = width - offset;
     else
        scroff = 0;
-    scrsize = scrlines * width - scroff;
+
+    scrsize -= scroff;
     if (scrsize > file_size - top_pos)
        scrsize = file_size - top_pos;