From: Christophe Fergeau Date: Mon, 14 Feb 2011 05:36:06 +0000 (+0800) Subject: fix OOM handling in hash routines X-Git-Tag: v0.8.8~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9190f0b0a0e649a5a480f47691dfd44751eb78c9;p=thirdparty%2Flibvirt.git fix OOM handling in hash routines * src/util/hash.c: virHashAddEntry and virHashUpdateEntry were missing NULL checks on strdup * AUTHORS: add Christophe Fergeau --- diff --git a/AUTHORS b/AUTHORS index 38ea4bb51e..6ff7c14c20 100644 --- a/AUTHORS +++ b/AUTHORS @@ -154,6 +154,7 @@ Patches have also been contributed by: Zdenek Styblik Gui Jianfeng Michal Novotny + Christophe Fergeau [....send patches to get your name here....] diff --git a/src/util/hash.c b/src/util/hash.c index 5c56dae029..754a87687c 100644 --- a/src/util/hash.c +++ b/src/util/hash.c @@ -253,6 +253,7 @@ virHashAddEntry(virHashTablePtr table, const char *name, void *userdata) unsigned long key, len = 0; virHashEntryPtr entry; virHashEntryPtr insert; + char *new_name; if ((table == NULL) || (name == NULL)) return (-1); @@ -281,12 +282,17 @@ virHashAddEntry(virHashTablePtr table, const char *name, void *userdata) return (-1); } - entry->name = strdup(name); + new_name = strdup(name); + if (new_name == NULL) { + if (insert != NULL) + VIR_FREE(entry); + return (-1); + } + entry->name = new_name; entry->payload = userdata; entry->next = NULL; entry->valid = 1; - if (insert != NULL) insert->next = entry; @@ -318,6 +324,7 @@ virHashUpdateEntry(virHashTablePtr table, const char *name, unsigned long key; virHashEntryPtr entry; virHashEntryPtr insert; + char *new_name; if ((table == NULL) || name == NULL) return (-1); @@ -353,7 +360,13 @@ virHashUpdateEntry(virHashTablePtr table, const char *name, return (-1); } - entry->name = strdup(name); + new_name= strdup(name); + if (new_name == NULL) { + if (insert != NULL) + VIR_FREE(entry); + return (-1); + } + entry->name = new_name; entry->payload = userdata; entry->next = NULL; entry->valid = 1;