]> jfr.im git - irc/gunnarbeutner/shroudbnc.git/commitdiff
Fix potential memory leak.
authorGunnar Beutner <redacted>
Thu, 6 Feb 2014 19:24:15 +0000 (20:24 +0100)
committerGunnar Beutner <redacted>
Fri, 7 Feb 2014 10:19:07 +0000 (11:19 +0100)
src/Hashtable.h

index 4fdc763c9328528751091cc826d66937d46a3ecc..956871471e72654ec19cc6afbdcc4e4cdfda7c51 100644 (file)
@@ -431,16 +431,20 @@ public:
         * will eventually have to be passed to free().
         */
        char **GetSortedKeys(void) const {
-               char **Keys = NULL;
+               char **NewKeys, **Keys = NULL;
                int Count = 0;
 
                for (int i = 0; i < m_BucketCount; i++) {
-                       Keys = (char **)realloc(Keys, (Count + m_Buckets[i].Count) * sizeof(char *));
+                       NewKeys = (char **)realloc(Keys, (Count + m_Buckets[i].Count) * sizeof(char *));
+
+                       if (Count + m_Buckets[i].Count > 0 && NewKeys == NULL) {
+                               free(Keys);
 
-                       if (Count + m_Buckets[i].Count > 0 && Keys == NULL) {
                                return NULL;
                        }
 
+                       Keys = NewKeys;
+
                        for (int a = 0; a < m_Buckets[i].Count; a++) {
                                Keys[Count + a] = m_Buckets[i].Keys[a];
                        }