krb5 commit: Add cleanup label in ms2mit

Tom Yu tlyu at mit.edu
Wed Mar 30 18:16:09 EDT 2016


https://github.com/krb5/krb5/commit/e033a81c891030741952e4743a0b5503bdbcea17
commit e033a81c891030741952e4743a0b5503bdbcea17
Author: Sarah Day <sarahday at mit.edu>
Date:   Thu Mar 3 16:49:06 2016 -0500

    Add cleanup label in ms2mit
    
    ticket: 8390 (new)
    tags: pullup
    target_version: 1.13-next
    target_version: 1.14-next

 src/windows/ms2mit/ms2mit.c |   62 ++++++++++++++-----------------------------
 1 files changed, 20 insertions(+), 42 deletions(-)

diff --git a/src/windows/ms2mit/ms2mit.c b/src/windows/ms2mit/ms2mit.c
index 31c269a..9018707 100644
--- a/src/windows/ms2mit/ms2mit.c
+++ b/src/windows/ms2mit/ms2mit.c
@@ -39,19 +39,16 @@ xusage(void)
     exit(1);
 }
 
-void
-main(
-    int argc,
-    char *argv[]
-    )
+int
+main(int argc, char *argv[])
 {
-    krb5_context kcontext;
+    krb5_context kcontext = NULL;
     krb5_error_code code;
     krb5_ccache ccache=NULL;
     krb5_ccache mslsa_ccache=NULL;
     krb5_cc_cursor cursor;
     krb5_creds creds;
-    krb5_principal princ;
+    krb5_principal princ = NULL;
     int initial_ticket = 0;
     int option;
     char * ccachestr = 0;
@@ -73,28 +70,23 @@ main(
 
     if (code = krb5_init_context(&kcontext)) {
         com_err(argv[0], code, "while initializing kerberos library");
-        exit(1);
+        goto cleanup;
     }
 
     if (code = krb5_cc_resolve(kcontext, "MSLSA:", &mslsa_ccache)) {
         com_err(argv[0], code, "while opening MS LSA ccache");
-        krb5_free_context(kcontext);
-        exit(1);
+        goto cleanup;
     }
 
     if (code = krb5_cc_set_flags(kcontext, mslsa_ccache, KRB5_TC_NOTICKET)) {
         com_err(argv[0], code, "while setting KRB5_TC_NOTICKET flag");
-        krb5_cc_close(kcontext, mslsa_ccache);
-        krb5_free_context(kcontext);
-        exit(1);
+        goto cleanup;
     }
 
     /* Enumerate tickets from cache looking for an initial ticket */
     if ((code = krb5_cc_start_seq_get(kcontext, mslsa_ccache, &cursor))) {
         com_err(argv[0], code, "while initiating the cred sequence of MS LSA ccache");
-        krb5_cc_close(kcontext, mslsa_ccache);
-        krb5_free_context(kcontext);
-        exit(1);
+        goto cleanup;
     }
 
     while (!(code = krb5_cc_next_cred(kcontext, mslsa_ccache, &cursor, &creds)))
@@ -110,24 +102,18 @@ main(
 
     if (code = krb5_cc_set_flags(kcontext, mslsa_ccache, 0)) {
         com_err(argv[0], code, "while clearing flags");
-        krb5_cc_close(kcontext, mslsa_ccache);
-        krb5_free_context(kcontext);
-        exit(1);
+        goto cleanup;
     }
 
     if ( !initial_ticket ) {
         fprintf(stderr, "%s: Initial Ticket Getting Tickets are not available from the MS LSA\n",
                 argv[0]);
-        krb5_cc_close(kcontext, mslsa_ccache);
-        krb5_free_context(kcontext);
-        exit(1);
+        goto cleanup;
     }
 
     if (code = krb5_cc_get_principal(kcontext, mslsa_ccache, &princ)) {
         com_err(argv[0], code, "while obtaining MS LSA principal");
-        krb5_cc_close(kcontext, mslsa_ccache);
-        krb5_free_context(kcontext);
-        exit(1);
+        goto cleanup;
     }
 
     if (ccachestr)
@@ -136,32 +122,24 @@ main(
         code = krb5_cc_default(kcontext, &ccache);
     if (code) {
         com_err(argv[0], code, "while getting default ccache");
-        krb5_free_principal(kcontext, princ);
-        krb5_cc_close(kcontext, mslsa_ccache);
-        krb5_free_context(kcontext);
-        exit(1);
+        goto cleanup;
     }
     if (code = krb5_cc_initialize(kcontext, ccache, princ)) {
         com_err (argv[0], code, "when initializing ccache");
-        krb5_free_principal(kcontext, princ);
-        krb5_cc_close(kcontext, mslsa_ccache);
-        krb5_cc_close(kcontext, ccache);
-        krb5_free_context(kcontext);
-        exit(1);
+        goto cleanup;
     }
 
     if (code = krb5_cc_copy_creds(kcontext, mslsa_ccache, ccache)) {
         com_err (argv[0], code, "while copying MS LSA ccache to default ccache");
-        krb5_free_principal(kcontext, princ);
-        krb5_cc_close(kcontext, ccache);
-        krb5_cc_close(kcontext, mslsa_ccache);
-        krb5_free_context(kcontext);
-        exit(1);
+        goto cleanup;
     }
 
+cleanup:
     krb5_free_principal(kcontext, princ);
-    krb5_cc_close(kcontext, ccache);
-    krb5_cc_close(kcontext, mslsa_ccache);
+    if (ccache != NULL)
+        krb5_cc_close(kcontext, ccache);
+    if (mslsa_ccache != NULL)
+        krb5_cc_close(kcontext, mslsa_ccache);
     krb5_free_context(kcontext);
-    return(0);
+    return code ? 1 : 0;
 }


More information about the cvs-krb5 mailing list