Avoid trying to free NULL ptrs. Comment on malloc usages.
authorEric Andersen <andersen@codepoet.org>
Wed, 19 Jul 2000 17:37:57 +0000 (17:37 -0000)
committerEric Andersen <andersen@codepoet.org>
Wed, 19 Jul 2000 17:37:57 +0000 (17:37 -0000)
 -Erik

cmdedit.c
shell/cmdedit.c

index 0f064b4147d5ae03a9a002c698bdf0cfb0e496c6..0de18e81f6bf4695e2840ce898959ca1999fccb0 100644 (file)
--- a/cmdedit.c
+++ b/cmdedit.c
@@ -370,14 +370,16 @@ void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len)
 
 void get_previous_history(struct history **hp, char* command)
 {
-       free((*hp)->s);
+       if ((*hp)->s)
+               free((*hp)->s);
        (*hp)->s = strdup(command);
        *hp = (*hp)->p;
 }
 
 void get_next_history(struct history **hp, char* command)
 {
-       free((*hp)->s);
+       if ((*hp)->s)
+               free((*hp)->s);
        (*hp)->s = strdup(command);
        *hp = (*hp)->n;
 }
@@ -654,7 +656,7 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
                struct history *h = his_end;
 
                if (!h) {
-                       /* No previous history */
+                       /* No previous history -- this memory is never freed */
                        h = his_front = malloc(sizeof(struct history));
                        h->n = malloc(sizeof(struct history));
 
@@ -666,7 +668,7 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
                        his_end = h->n;
                        history_counter++;
                } else {
-                       /* Add a new history command */
+                       /* Add a new history command -- this memory is never freed */
                        h->n = malloc(sizeof(struct history));
 
                        h->n->p = h;
index 0f064b4147d5ae03a9a002c698bdf0cfb0e496c6..0de18e81f6bf4695e2840ce898959ca1999fccb0 100644 (file)
@@ -370,14 +370,16 @@ void input_tab(char* command, char* prompt, int outputFd, int *cursor, int *len)
 
 void get_previous_history(struct history **hp, char* command)
 {
-       free((*hp)->s);
+       if ((*hp)->s)
+               free((*hp)->s);
        (*hp)->s = strdup(command);
        *hp = (*hp)->p;
 }
 
 void get_next_history(struct history **hp, char* command)
 {
-       free((*hp)->s);
+       if ((*hp)->s)
+               free((*hp)->s);
        (*hp)->s = strdup(command);
        *hp = (*hp)->n;
 }
@@ -654,7 +656,7 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
                struct history *h = his_end;
 
                if (!h) {
-                       /* No previous history */
+                       /* No previous history -- this memory is never freed */
                        h = his_front = malloc(sizeof(struct history));
                        h->n = malloc(sizeof(struct history));
 
@@ -666,7 +668,7 @@ extern void cmdedit_read_input(char* prompt, char command[BUFSIZ])
                        his_end = h->n;
                        history_counter++;
                } else {
-                       /* Add a new history command */
+                       /* Add a new history command -- this memory is never freed */
                        h->n = malloc(sizeof(struct history));
 
                        h->n->p = h;