Make sure memory is freed on errors
authorTheodore Tso <tytso@mit.edu>
Fri, 24 Dec 1993 22:47:18 +0000 (22:47 +0000)
committerTheodore Tso <tytso@mit.edu>
Fri, 24 Dec 1993 22:47:18 +0000 (22:47 +0000)
git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@3279 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/kdb/fetch_mkey.c
src/lib/kdb/kdb_dbm.c

index 98dcac7a4164b3798b892a287db9a78662363bb0..8c5ed8ccb4bdd38020cc3ca157bcd614b8a4fbcb 100644 (file)
@@ -147,9 +147,12 @@ OLDDECLARG(krb5_keyblock *,key)
            goto errout;
        }
        if (fread((krb5_pointer) key->contents,
-                 sizeof(key->contents[0]), key->length, kf) != key->length)
+                 sizeof(key->contents[0]), key->length, kf) != key->length) {
            retval = KRB5_KDB_CANTREAD_STORED;
-       else
+           memset(key->contents, 0, key->length);
+           free(key->contents);
+           key->contents = 0;
+       } else
            retval = 0;
     errout:
        (void) fclose(kf);
index 43aa3e4d797137de883915123e1473da256d705b..5a6712f1d23f1eafd8423ecc528a1b01b2e2f1fd 100644 (file)
@@ -179,36 +179,42 @@ char *sfx;
 krb5_error_code
 krb5_dbm_db_init()
 {
-    if (!inited) {
-       char *filename = gen_dbsuffix (current_db_name, ".ok");
-       if (!filename)
-           return ENOMEM;
+    char *filename = 0;
+
+    if (inited)
+       return 0;
+
+    filename = gen_dbsuffix (current_db_name, ".ok");
+    if (!filename)
+       return ENOMEM;
 #ifdef POSIX_FILE_LOCKS
-       /* needs be open read/write so that write locking can work with
-          POSIX systems */
-       if ((dblfd = open(filename, O_RDWR, 0)) == -1) {
-           if (errno == EACCES) {
-               if ((dblfd = open(filename, O_RDONLY, 0)) == -1)
-                   return errno;
-           } else
-               return errno;
-       }
+    /*
+     * needs be open read/write so that write locking can work with
+     * POSIX systems
+     */
+    if ((dblfd = open(filename, O_RDWR, 0)) == -1) {
+       if (errno == EACCES) {
+           if ((dblfd = open(filename, O_RDONLY, 0)) == -1)
+               goto err_out;
+       } else
+           goto err_out;
+    }
 #else
-       if ((dblfd = open(filename, 0, 0)) == -1) {
-           return errno;
-       }
+    if ((dblfd = open(filename, 0, 0)) == -1)
+       goto err_out;
 #endif
-       free(filename);
-       inited++;
-    }
-    return (0);
+    inited++;
+    errno = 0;
+    
+err_out:
+    free(filename);
+    return (errno);
 }
+
 /*
  * gracefully shut down database--must be called by ANY program that does
  * a krb5_dbm_db_init 
  */
-
-
 krb5_error_code
 krb5_dbm_db_fini()
 {
@@ -997,11 +1003,11 @@ krb5_dbm_db_rename(from, to)
     char *from;
     char *to;
 {
-    char *fromdir;
-    char *todir;
-    char *frompag;
-    char *topag;
-    char *fromok;
+    char *fromdir = 0;
+    char *todir = 0;
+    char *frompag = 0;
+    char *topag = 0;
+    char *fromok = 0;
     time_t trans;
     krb5_error_code retval;
 
@@ -1011,26 +1017,26 @@ krb5_dbm_db_rename(from, to)
     todir = gen_dbsuffix (to, ".dir");
     if (!todir) {
        retval = ENOMEM;
-       goto freefromdir;
+       goto errout;
     }
     frompag = gen_dbsuffix (from, ".pag");
     if (!frompag) {
        retval = ENOMEM;
-       goto freetodir;
+       goto errout;
     }
     topag = gen_dbsuffix (to, ".pag");
     if (!topag) {
        retval = ENOMEM;
-       goto freefrompag;
+       goto errout;
     }
     fromok = gen_dbsuffix(from, ".ok");
     if (!fromok) {
        retval = ENOMEM;
-       goto freetopag;
+       goto errout;
     }
 
     if (retval = krb5_dbm_db_start_update(to, &trans))
-       return(retval);
+       goto errout;
     
     if ((rename (fromdir, todir) == 0)
        && (rename (frompag, topag) == 0)) {
@@ -1039,15 +1045,17 @@ krb5_dbm_db_rename(from, to)
     } else
        retval = errno;
     
-    free_dbsuffix (fromok);
- freetopag:
-    free_dbsuffix (topag);
- freefrompag:
-    free_dbsuffix (frompag);
- freetodir:
-    free_dbsuffix (todir);
- freefromdir:
-    free_dbsuffix (fromdir);
+errout:
+    if (fromok)
+       free_dbsuffix (fromok);
+    if (topag)
+       free_dbsuffix (topag);
+    if (frompag)
+       free_dbsuffix (frompag);
+    if (todir)
+       free_dbsuffix (todir);
+    if (fromdir)
+       free_dbsuffix (fromdir);
 
     if (retval == 0)
        return krb5_dbm_db_end_update(to, trans);