diff options
author | Guido van Rossum <guido@python.org> | 1996-07-21 02:32:44 +0000 |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1996-07-21 02:32:44 +0000 |
commit | 3be7140d31234860de5a2ea0764d97c4e29bec1f (patch) | |
tree | 23b487055e2f831d9589e0d0b878ace7af60342e /Modules/gdbmmodule.c | |
parent | 9e720e390a97796629a82c90e257b74cd1311b3f (diff) | |
download | cpython-git-3be7140d31234860de5a2ea0764d97c4e29bec1f.tar.gz |
Rewritten keys() and has_key() to avoid memory leaks.
Diffstat (limited to 'Modules/gdbmmodule.c')
-rw-r--r-- | Modules/gdbmmodule.c | 28 |
1 files changed, 17 insertions, 11 deletions
diff --git a/Modules/gdbmmodule.c b/Modules/gdbmmodule.c index ecfd31eee6..a48c756bcb 100644 --- a/Modules/gdbmmodule.c +++ b/Modules/gdbmmodule.c @@ -187,38 +187,44 @@ dbm_keys(dp, args) object *args; { register object *v, *item; - datum key, okey={ (char *)NULL, 0}; + datum key, nextkey; int err; if (dp == NULL || !is_dbmobject(dp)) { err_badcall(); return NULL; } + if (!getnoarg(args)) return NULL; + v = newlistobject(0); if (v == NULL) return NULL; - for (key = gdbm_firstkey(dp->di_dbm); key.dptr; - key = gdbm_nextkey(dp->di_dbm,okey) ) { + + key = gdbm_firstkey(dp->di_dbm); + while (key.dptr) { item = newsizedstringobject(key.dptr, key.dsize); - if (item == 0) { - DECREF(v); - return NULL; + if (item == NULL) { + free(key.dptr); + DECREF(v); + return NULL; } err = addlistitem(v, item); DECREF(item); - if(okey.dsize) free(okey.dptr); if (err != 0) { - DECREF(v); - return NULL; + free(key.dptr); + DECREF(v); + return NULL; } - okey=key; + nextkey = gdbm_nextkey(dp->di_dbm, key); + free(key.dptr); + key = nextkey; } + return v; } - static object * dbm_has_key(dp, args) register dbmobject *dp; |