Improve output variable handling of osa_adb_get_policy() in the db2
authorGreg Hudson <ghudson@mit.edu>
Fri, 9 Jul 2010 01:22:38 +0000 (01:22 +0000)
committerGreg Hudson <ghudson@mit.edu>
Fri, 9 Jul 2010 01:22:38 +0000 (01:22 +0000)
KDB module, and close some unlikely memory leaks.

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

src/plugins/kdb/db2/adb_policy.c

index 057f1826ffa0a06b922ae0073a2580508ca0b398..87fab3ac57c6759d5790a35c2c1dda781f429b98 100644 (file)
@@ -9,12 +9,10 @@
 static char *rcsid = "$Header$";
 #endif
 
-#include        <sys/file.h>
-#include        <fcntl.h>
-#include        "policy_db.h"
-#include        <stdlib.h>
-#include        <string.h>
-#include <errno.h>
+#include "k5-int.h"
+
+#include <sys/file.h>
+#include "policy_db.h"
 
 #define OPENLOCK(db, mode)                                              \
     {                                                                   \
@@ -184,13 +182,14 @@ error:
  */
 krb5_error_code
 osa_adb_get_policy(osa_adb_policy_t db, char *name,
-                   osa_policy_ent_t *entry)
+                   osa_policy_ent_t *entry_ptr)
 {
     DBT                 dbkey;
     DBT                 dbdata;
     XDR                 xdrs;
     int                 ret;
-    char                *aligned_data;
+    char                *aligned_data = NULL;
+    osa_policy_ent_t    entry = NULL;
 
     OPENLOCK(db, KRB5_DB_LOCKMODE_SHARED);
 
@@ -212,24 +211,26 @@ osa_adb_get_policy(osa_adb_policy_t db, char *name,
         ret = OSA_ADB_FAILURE;
         goto error;
     }
-    if (!(*(entry) = (osa_policy_ent_t)malloc(sizeof(osa_policy_ent_rec)))) {
-        ret = ENOMEM;
+    entry = k5alloc(sizeof(*entry), &ret);
+    if (entry == NULL)
         goto error;
-    }
-    if (!(aligned_data = (char *) malloc(dbdata.size))) {
-        ret = ENOMEM;
+    aligned_data = k5alloc(dbdata.size, &ret);
+    if (aligned_data == NULL)
         goto error;
-    }
     memcpy(aligned_data, dbdata.data, dbdata.size);
-    memset(*entry, 0, sizeof(osa_policy_ent_rec));
     xdrmem_create(&xdrs, aligned_data, dbdata.size, XDR_DECODE);
-    if (!xdr_osa_policy_ent_rec(&xdrs, *entry))
-        ret =  OSA_ADB_FAILURE;
-    else ret = OSA_ADB_OK;
+    if (!xdr_osa_policy_ent_rec(&xdrs, entry)) {
+        ret = OSA_ADB_FAILURE;
+        goto error;
+    }
+    ret = OSA_ADB_OK;
     xdr_destroy(&xdrs);
-    free(aligned_data);
+    *entry_ptr = entry;
+    entry = NULL;
 
 error:
+    free(aligned_data);
+    free(entry);
     CLOSELOCK(db);
     return ret;
 }