Let's try to free allocated lines when resizing / reloading.
When using a big HISTSIZE and multiple concurrent shells, resource usage
climb up rather quickly.

The initial version didn't bother with resetting the lines pointers to
NULL, but better safe(r) than sorry, I guess.

ok?

NB: after "histptr = history - 1;", histptr is technically out of
bounds.  I'll try to address this in a subsequent diff.


Index: history.c
===================================================================
RCS file: /d/cvs/src/bin/ksh/history.c,v
retrieving revision 1.65
diff -u -p -p -u -r1.65 history.c
--- history.c   26 Aug 2017 12:34:32 -0000      1.65
+++ history.c   26 Aug 2017 14:00:03 -0000
@@ -434,6 +434,20 @@ histbackup(void)
        }
 }
 
+static void
+histreset(void)
+{
+       char **hp;
+
+       for (hp = history; hp <= histptr; hp++) {
+               afree(*hp, APERM);
+               *hp = NULL;
+       }
+
+       histptr = history - 1;
+       hist_source->line = 0;
+}
+
 /*
  * Return the current position.
  */
@@ -513,6 +527,11 @@ sethistsize(int n)
 
                /* save most recent history */
                if (offset > n - 1) {
+                       int i;
+                       for (i = 0; i < n; i++) {
+                               afree(history[i], APERM);
+                               history[i] = NULL;
+                       }
                        offset = n - 1;
                        memmove(history, histptr - offset, n * sizeof(char *));
                }
@@ -544,9 +563,7 @@ sethistfile(const char *name)
        if (hname) {
                afree(hname, APERM);
                hname = NULL;
-               /* let's reset the history */
-               histptr = history - 1;
-               hist_source->line = 0;
+               histreset();
        }
 
        history_close();
@@ -603,9 +620,7 @@ histsave(int lno, const char *cmd, int d
                        if (timespeccmp(&sb.st_mtim, &last_sb.st_mtim, ==))
                                ; /* file is unchanged */
                        else {
-                               /* reset history */
-                               histptr = history - 1;
-                               hist_source->line = 0;
+                               histreset();
                                history_load(hist_source);
                        }
                }


-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE

Reply via email to