about summary refs log tree commit diff
diff options
context:
space:
mode:
authorSven Wischnowsky <wischnow@users.sourceforge.net>2001-05-31 09:43:59 +0000
committerSven Wischnowsky <wischnow@users.sourceforge.net>2001-05-31 09:43:59 +0000
commit1b2d095217d1560a648032aa192057b935ce9460 (patch)
tree4126aa0dc04cde27de4cc3fe83de835190f34e71
parentae5e34db253ffd89369dd67835999c23dd9e62c2 (diff)
downloadzsh-1b2d095217d1560a648032aa192057b935ce9460.tar.gz
zsh-1b2d095217d1560a648032aa192057b935ce9460.tar.xz
zsh-1b2d095217d1560a648032aa192057b935ce9460.zip
fix two memory leaks (at least I hope they are fixed) and some out-of-bound array accesses, plus some defensive programming for heap allocated param structs (14605)
-rw-r--r--ChangeLog6
-rw-r--r--Src/Modules/parameter.c1
-rw-r--r--Src/Zle/complete.c2
-rw-r--r--Src/Zle/computil.c5
-rw-r--r--Src/Zle/zleparameter.c64
-rw-r--r--Src/params.c4
6 files changed, 44 insertions, 38 deletions
diff --git a/ChangeLog b/ChangeLog
index 855a09554..199fd1fa0 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2001-05-31  Sven Wischnowsky  <wischnow@zsh.org>
 
+	* 14605: Src/params.c, Src/Modules/parameter.c,
+	Src/Zle/complete.c, Src/Zle/computil.c, Src/Zle/zleparameter.c:
+	fix two memory leaks (at least I hope they are fixed) and some
+	out-of-bound array accesses, plus some defensive programming
+	for heap allocated param structs
+
 	* 14602: Completion/Base/Utility/_values,
 	Functions/Misc/nslookup: better prompt matching in nslookup;
 	fix separator handling in _multi_parts
diff --git a/Src/Modules/parameter.c b/Src/Modules/parameter.c
index 2df362faa..db3534e69 100644
--- a/Src/Modules/parameter.c
+++ b/Src/Modules/parameter.c
@@ -60,6 +60,7 @@ createspecialhash(char *name, GetNodeFunc get, ScanTabFunc scan)
     pm->sets.hfn = hashsetfn;
     pm->unsetfn = stdunsetfn;
     pm->u.hash = ht = newhashtable(0, name, NULL);
+    pm->ct = 0;
 
     ht->hash        = hasher;
     ht->emptytable  = (TableFunc) shempty;
diff --git a/Src/Zle/complete.c b/Src/Zle/complete.c
index 5c166291e..574e638ac 100644
--- a/Src/Zle/complete.c
+++ b/Src/Zle/complete.c
@@ -403,7 +403,7 @@ parse_class(Cpattern p, unsigned char *s, unsigned char e)
 
     n = !n;
     while (*s && (k || *s != e)) {
-	if (s[1] == '-' && s[2] != e) {
+	if (s[1] == '-' && s[2] && s[2] != e) {
 	    /* a run of characters */
 	    for (j = (int) *s; j <= (int) s[2]; j++)
 		p->tab[j] = (eq ? i++ : n);
diff --git a/Src/Zle/computil.c b/Src/Zle/computil.c
index d7d44f999..2c0e119f9 100644
--- a/Src/Zle/computil.c
+++ b/Src/Zle/computil.c
@@ -819,8 +819,9 @@ parse_cadef(char *nam, char **args)
 	    if (!multi) {
 		if (!xor) {
 		    xor = (char **) zalloc(2 * sizeof(char *));
-		    xor[1] = NULL;
+		    xor[0] = xor[1] = NULL;
 		}
+                zsfree(xor[xnum]);
 		xor[xnum] = ztrdup(rembslashcolon(name));
 	    }
 	    if (c == ':') {
@@ -1029,7 +1030,7 @@ get_cadef(char *nam, char **args)
     Cadef *p, *min, new;
     int i, na = arrlen(args);
 
-    for (i = MAX_CACACHE, p = cadef_cache, min = NULL; *p && i; p++, i--)
+    for (i = MAX_CACACHE, p = cadef_cache, min = NULL; i && *p; p++, i--)
 	if (*p && na == (*p)->ndefs && arrcmp(args, (*p)->defs)) {
 	    (*p)->lastt = time(0);
 
diff --git a/Src/Zle/zleparameter.c b/Src/Zle/zleparameter.c
index 8a5bc0bc2..cce421897 100644
--- a/Src/Zle/zleparameter.c
+++ b/Src/Zle/zleparameter.c
@@ -54,7 +54,8 @@ createspecialhash(char *name, GetNodeFunc get, ScanTabFunc scan)
     pm->gets.hfn = hashgetfn;
     pm->sets.hfn = hashsetfn;
     pm->unsetfn = stdunsetfn;
-    pm->u.hash = ht = newhashtable(7, name, NULL);
+    pm->u.hash = ht = newhashtable(0, name, NULL);
+    pm->ct = 0;
 
     ht->hash        = hasher;
     ht->emptytable  = (TableFunc) shempty;
@@ -100,27 +101,24 @@ getpmwidgets(HashTable ht, char *name)
     Param pm = NULL;
     Thingy th;
 
-    HEAPALLOC {
-	pm = (Param) zhalloc(sizeof(struct param));
-	pm->nam = dupstring(name);
-	pm->flags = PM_SCALAR | PM_READONLY;
-	pm->sets.cfn = NULL;
-	pm->gets.cfn = strgetfn;
-	pm->unsetfn = NULL;
-	pm->ct = 0;
-	pm->env = NULL;
-	pm->ename = NULL;
-	pm->old = NULL;
-	pm->level = 0;
-	if ((th = (Thingy) thingytab->getnode(thingytab, name)) &&
-	    !(th->flags & DISABLED))
-	    pm->u.str = widgetstr(th->widget);
-	else {
-	    pm->u.str = dupstring("");
-	    pm->flags |= PM_UNSET;
-	}
-    } LASTALLOC;
-
+    pm = (Param) zhalloc(sizeof(struct param));
+    pm->nam = dupstring(name);
+    pm->flags = PM_SCALAR | PM_READONLY;
+    pm->sets.cfn = NULL;
+    pm->gets.cfn = strgetfn;
+    pm->unsetfn = NULL;
+    pm->ct = 0;
+    pm->env = NULL;
+    pm->ename = NULL;
+    pm->old = NULL;
+    pm->level = 0;
+    if ((th = (Thingy) thingytab->getnode(thingytab, name)) &&
+	!(th->flags & DISABLED))
+	pm->u.str = widgetstr(th->widget);
+    else {
+	pm->u.str = dupstring("");
+	pm->flags |= PM_UNSET;
+    }
     return (HashNode) pm;
 }
 
@@ -145,7 +143,9 @@ scanpmwidgets(HashTable ht, ScanFunc func, int flags)
     for (i = 0; i < thingytab->hsize; i++)
 	for (hn = thingytab->nodes[i]; hn; hn = hn->next) {
 	    pm.nam = hn->nam;
-	    if (func != scancountparams)
+	    if (func != scancountparams &&
+		((flags & (SCANPM_WANTVALS|SCANPM_MATCHVAL)) ||
+		 !(flags & SCANPM_WANTKEYS)))
 		pm.u.str = widgetstr(((Thingy) hn)->widget);
 	    func((HashNode) &pm, flags);
 	}
@@ -185,10 +185,10 @@ struct pardef {
 };
 
 static struct pardef partab[] = {
-    { "zlewidgets", PM_READONLY,
+    { "widgets", PM_READONLY,
       getpmwidgets, scanpmwidgets, hashsetfn,
       NULL, NULL, stdunsetfn, NULL },
-    { "zlekeymaps", PM_ARRAY|PM_HIDE|PM_SPECIAL|PM_READONLY,
+    { "keymaps", PM_ARRAY|PM_SPECIAL|PM_READONLY,
       NULL, NULL, NULL,
       arrsetfn, keymapsgetfn, stdunsetfn, NULL },
     { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL }
@@ -196,14 +196,14 @@ static struct pardef partab[] = {
 
 /**/
 int
-setup_zleparameter(Module m)
+setup_(Module m)
 {
     return 0;
 }
 
 /**/
 int
-boot_zleparameter(Module m)
+boot_(Module m)
 {
     struct pardef *def;
 
@@ -218,7 +218,7 @@ boot_zleparameter(Module m)
 	    if (def->hsetfn)
 		def->pm->sets.hfn = def->hsetfn;
 	} else {
-	    if (!(def->pm = createparam(def->name, def->flags)))
+	    if (!(def->pm = createparam(def->name, def->flags | PM_HIDE)))
 		return 1;
 	    def->pm->sets.afn = def->setfn;
 	    def->pm->gets.afn = def->getfn;
@@ -228,11 +228,9 @@ boot_zleparameter(Module m)
     return 0;
 }
 
-#ifdef MODULE
-
 /**/
 int
-cleanup_zleparameter(Module m)
+cleanup_(Module m)
 {
     Param pm;
     struct pardef *def;
@@ -249,9 +247,7 @@ cleanup_zleparameter(Module m)
 
 /**/
 int
-finish_zleparameter(Module m)
+finish_(Module m)
 {
     return 0;
 }
-
-#endif
diff --git a/Src/params.c b/Src/params.c
index 6c07396b2..f4575b56e 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -704,7 +704,7 @@ createparam(char *name, int flags)
 	if (isset(ALLEXPORT) && !(flags & PM_HASHELEM))
 	    flags |= PM_EXPORTED;
     } else {
-	pm = (Param) zhalloc(sizeof *pm);
+	pm = (Param) hcalloc(sizeof *pm);
 	pm->nam = nulstring;
     }
     pm->flags = flags & ~PM_LOCAL;
@@ -727,6 +727,7 @@ copyparam(Param tpm, Param pm, int toplevel)
      * with sets.?fn() usage).
      */
     tpm->flags = pm->flags;
+    tpm->ct = pm->ct;
     if (!toplevel)
 	tpm->flags &= ~PM_SPECIAL;
     switch (PM_TYPE(pm->flags)) {
@@ -2551,6 +2552,7 @@ usernamesetfn(Param pm, char *x)
 	}
     }
 #endif /* HAVE_SETUID && HAVE_GETPWNAM */
+    zsfree(x);
 }
 
 /* Function to get value for special parameter `UID' */