Rewrite set_results() in prof_get.c
authorGreg Hudson <ghudson@mit.edu>
Mon, 25 Jul 2011 15:54:33 +0000 (15:54 +0000)
committerGreg Hudson <ghudson@mit.edu>
Mon, 25 Jul 2011 15:54:33 +0000 (15:54 +0000)
The new implementation should be more friendly to static analyzers.
Coverity was getting confused into thinking that profile_iterator()
had the effect of returning a freed name pointer.

git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@25045 dc483132-0cff-0310-8789-dd5450dbe970

src/util/profile/prof_get.c

index 844d226899477b3b391ef1dacbdffdaf7d8f4f13..b2ea40ce1a2de6fc87c0a30f50a0dfdd820bbf4a 100644 (file)
@@ -544,28 +544,31 @@ static errcode_t
 set_results(const char *name, const char *value, char **ret_name,
             char **ret_value)
 {
-    if (ret_name) {
-        if (name) {
-            *ret_name = strdup(name);
-            if (!*ret_name)
-                return ENOMEM;
-        } else
-            *ret_name = NULL;
+    char *name_copy = NULL, *value_copy = NULL;
+
+    if (ret_name && name) {
+        name_copy = strdup(name);
+        if (name_copy == NULL)
+            goto oom;
     }
-    if (ret_value) {
-        if (value) {
-            *ret_value = strdup(value);
-            if (!*ret_value) {
-                if (ret_name) {
-                    free(*ret_name);
-                    *ret_name = NULL;
-                }
-                return ENOMEM;
-            }
-        } else
-            *ret_value = NULL;
+    if (ret_value && value) {
+        value_copy = strdup(value);
+        if (value_copy == NULL)
+            goto oom;
     }
+    if (ret_name)
+        *ret_name = name_copy;
+    if (ret_value)
+        *ret_value = value_copy;
     return 0;
+oom:
+    free(name_copy);
+    free(value_copy);
+    if (ret_name)
+        *ret_name = NULL;
+    if (ret_value)
+        *ret_value = NULL;
+    return ENOMEM;
 }
 
 errcode_t KRB5_CALLCONV