Avoid side effects in assert expressions
authorGreg Hudson <ghudson@mit.edu>
Fri, 9 Mar 2012 18:30:31 +0000 (18:30 +0000)
committerGreg Hudson <ghudson@mit.edu>
Fri, 9 Mar 2012 18:30:31 +0000 (18:30 +0000)
asserts may be compiled out with -DNDEBUG, so it's wrong to use an
assert expression with an important side effect.

(We also have scores of side-effecting asserts in test programs, but
those are less important and can be dealt with separately.)

ticket: 7105

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

src/lib/apputils/net-server.c
src/lib/crypto/krb/cf2.c
src/util/et/com_err.c

index fcc660d351fd14fa4d004aead9202a6a879452ce..8b4af14295ce6697d48282f9eb7ad6ae8df81105 100644 (file)
@@ -1066,7 +1066,10 @@ static void
 do_network_reconfig(verto_ctx *ctx, verto_ev *ev)
 {
     struct connection *conn = verto_get_private(ev);
-    assert(loop_setup_network(ctx, conn->handle, conn->prog) == 0);
+    if (loop_setup_network(ctx, conn->handle, conn->prog) != 0) {
+        krb5_klog_syslog(LOG_ERR, _("Failed to reconfigure network, exiting"));
+        verto_break(ctx);
+    }
 }
 
 static int
index 5f82d62afd66723d77f3f955bf9c15cc79588f6d..7334ed168d966e803bb61b9fd655d3fec6455193 100644 (file)
@@ -107,7 +107,8 @@ krb5_c_fx_cf2_simple(krb5_context context,
         return KRB5_BAD_ENCTYPE;
     out_enctype_num = k1->enctype;
     assert(out != NULL);
-    assert((out_enctype = find_enctype(out_enctype_num)) != NULL);
+    out_enctype = find_enctype(out_enctype_num);
+    assert(out_enctype != NULL);
     if (out_enctype->prf == NULL) {
         if (context)
             krb5int_set_error(&(context->err), KRB5_CRYPTO_INTERNAL,
index aaba89744f792216b39be21dd2d37dd55f318d9c..96922ec24f49de581d86f340b2ca81f67b3196c5 100644 (file)
@@ -154,8 +154,10 @@ et_old_error_hook_func set_com_err_hook (et_old_error_hook_func new_proc)
     et_old_error_hook_func x;
 
     /* Broken initialization?  What can we do?  */
-    assert(com_err_finish_init() == 0);
-    assert(com_err_lock_hook_handle() == 0);
+    if (com_err_finish_init() != 0)
+        abort();
+    if (com_err_lock_hook_handle() != 0)
+        abort();
     x = com_err_hook;
     com_err_hook = new_proc;
     k5_mutex_unlock(&com_err_hook_lock);
@@ -167,8 +169,10 @@ et_old_error_hook_func reset_com_err_hook ()
     et_old_error_hook_func x;
 
     /* Broken initialization?  What can we do?  */
-    assert(com_err_finish_init() == 0);
-    assert(com_err_lock_hook_handle() == 0);
+    if (com_err_finish_init() != 0)
+        abort();
+    if (com_err_lock_hook_handle() != 0)
+        abort();
     x = com_err_hook;
     com_err_hook = NULL;
     k5_mutex_unlock(&com_err_hook_lock);