[pacman-dev] [PATCH 1/3] lib/libalpm/signing.c: Fix memory leak in decode_signature() in case of error.
Signed-off-by: Diogo Sousa <diogogsousa@gmail.com> --- lib/libalpm/signing.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-) diff --git a/lib/libalpm/signing.c b/lib/libalpm/signing.c index 8db4a7b..67f7adf 100644 --- a/lib/libalpm/signing.c +++ b/lib/libalpm/signing.c @@ -175,6 +175,7 @@ static int decode_signature(const char *base64_data, size_t destlen = len * 3 / 4; MALLOC(*data, destlen, goto error); if(base64_decode(*data, &destlen, usline, len)) { + free(*data); goto error; } *data_len = destlen; -- 1.7.6.1
Signed-off-by: Diogo Sousa <diogogsousa@gmail.com> --- lib/libalpm/deps.c | 11 +++++++++-- 1 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/libalpm/deps.c b/lib/libalpm/deps.c index 47b7637..4c05191 100644 --- a/lib/libalpm/deps.c +++ b/lib/libalpm/deps.c @@ -455,12 +455,19 @@ alpm_depend_t *_alpm_dep_dup(const alpm_depend_t *dep) alpm_depend_t *newdep; CALLOC(newdep, 1, sizeof(alpm_depend_t), return NULL); - STRDUP(newdep->name, dep->name, return NULL); + STRDUP(newdep->name, dep->name, goto error1); newdep->name_hash = dep->name_hash; - STRDUP(newdep->version, dep->version, return NULL); + STRDUP(newdep->version, dep->version, goto error2); newdep->mod = dep->mod; return newdep; + +error2: + free(newdep->name); +error1: + free(newdep); + + return NULL; } /* These parameters are messy. We check if this package, given a list of -- 1.7.6.1
On Sat, Aug 27, 2011 at 4:03 PM, Diogo Sousa <diogogsousa@gmail.com> wrote:
Signed-off-by: Diogo Sousa <diogogsousa@gmail.com> I am not a fan of labels like 'error1', 'error2', this will result in spaghetti disasters down the road.
Given that free(NULL) is a no-op by standard, you can use one error: label here and just unwind in order- no need for multiple labels and we can afford the no-ops given this is an extreme error case (and we are likely about to eat it and die anyway). Same thing applies for the other, similar patch.
--- lib/libalpm/deps.c | 11 +++++++++-- 1 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/lib/libalpm/deps.c b/lib/libalpm/deps.c index 47b7637..4c05191 100644 --- a/lib/libalpm/deps.c +++ b/lib/libalpm/deps.c @@ -455,12 +455,19 @@ alpm_depend_t *_alpm_dep_dup(const alpm_depend_t *dep) alpm_depend_t *newdep; CALLOC(newdep, 1, sizeof(alpm_depend_t), return NULL);
- STRDUP(newdep->name, dep->name, return NULL); + STRDUP(newdep->name, dep->name, goto error1); newdep->name_hash = dep->name_hash; - STRDUP(newdep->version, dep->version, return NULL); + STRDUP(newdep->version, dep->version, goto error2); newdep->mod = dep->mod;
return newdep; + +error2: + free(newdep->name); +error1: + free(newdep); + + return NULL; }
/* These parameters are messy. We check if this package, given a list of -- 1.7.6.1
Signed-off-by: Diogo Sousa <diogogsousa@gmail.com> --- lib/libalpm/deps.c | 11 +++++++++-- 1 files changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/libalpm/deps.c b/lib/libalpm/deps.c index 4c05191..1e24dbc 100644 --- a/lib/libalpm/deps.c +++ b/lib/libalpm/deps.c @@ -441,13 +441,20 @@ alpm_depend_t *_alpm_splitdep(const char *depstring) } /* copy the right parts to the right places */ - STRNDUP(depend->name, depstring, ptr - depstring, return NULL); + STRNDUP(depend->name, depstring, ptr - depstring, goto error1); depend->name_hash = _alpm_hash_sdbm(depend->name); if(version) { - STRDUP(depend->version, version, return NULL); + STRDUP(depend->version, version, goto error2); } return depend; + +error2: + free(depend->name); +error1: + free(depend); + + return NULL; } alpm_depend_t *_alpm_dep_dup(const alpm_depend_t *dep) -- 1.7.6.1
On Sat, Aug 27, 2011 at 4:03 PM, Diogo Sousa <diogogsousa@gmail.com> wrote:
Signed-off-by: Diogo Sousa <diogogsousa@gmail.com> Thanks, applied. --- lib/libalpm/signing.c | 1 + 1 files changed, 1 insertions(+), 0 deletions(-)
diff --git a/lib/libalpm/signing.c b/lib/libalpm/signing.c index 8db4a7b..67f7adf 100644 --- a/lib/libalpm/signing.c +++ b/lib/libalpm/signing.c @@ -175,6 +175,7 @@ static int decode_signature(const char *base64_data, size_t destlen = len * 3 / 4; MALLOC(*data, destlen, goto error); if(base64_decode(*data, &destlen, usline, len)) { + free(*data); goto error; } *data_len = destlen; -- 1.7.6.1
participants (2)
-
Dan McGee
-
Diogo Sousa