Make unset strings in kim_options and kim_selection_hints
authorAlexandra Ellwood <lxs@mit.edu>
Wed, 1 Oct 2008 21:59:16 +0000 (21:59 +0000)
committerAlexandra Ellwood <lxs@mit.edu>
Wed, 1 Oct 2008 21:59:16 +0000 (21:59 +0000)
be empty strings rather than NULL.  This simplifies the
stream code (and makes it easier to read and debug).
In order to prevent copying tons of NUL bytes around,
special case kim_string functions to use a special
constant kim_empty_string.

ticket: 6055

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

src/include/kim/kim_selection_hints.h
src/kim/lib/kim_library_private.h
src/kim/lib/kim_options.c
src/kim/lib/kim_selection_hints.c
src/kim/lib/kim_string.c
src/kim/lib/kim_string_private.h
src/lib/krb5/os/init_os_ctx.c

index 6e7ee24c55074b18a22b02e8ef5ed4c6d56edf26..afeae635e9f5b2c974c79c7aa33b5e8d9fcf3872 100644 (file)
@@ -263,8 +263,10 @@ kim_error kim_selection_hints_set_hint (kim_selection_hints io_selection_hints,
  * \param in_selection_hints    a selection hints object.
  * \param in_hint_key           A string representing the type of hint to 
  *                              obtain.
- * \param out_hint_string       A string representation of the hint 
+ * \param out_hint_string       On exit, a string representation of the hint 
  *                              \a in_hint_key in \a in_selection_hints.
+ *                              If the hint is not set, sets the value pointed
+ *                              to by \a out_hint_string to NULL;
  *                              Must be freed with kim_string_free().
  * \return On success, #KIM_NO_ERROR.  On failure, an error code representing the failure.
  * \brief Get the string value of a hint used for identity selection.
index 83c06d7914138d95ffe95474b172afa99a1da2d1..160fba3a579a71183ab52a56efae89092b88e38d 100644 (file)
@@ -28,6 +28,7 @@
 #define KIM_LIBRARY_PRIVATE_H
 
 #include <kim/kim.h>
+#include <kim/kim_library.h>
 
 kim_error kim_library_init (void);
 
index 8cefa2ecf3ebb7e91f40ddc0ab23a6f7f6d76701..06c25ae88132a8a4824ee6c9aaead7eed3dce994 100644 (file)
@@ -49,7 +49,7 @@ kim_default_renewal_lifetime,
 kim_default_forwardable,
 kim_default_proxiable,
 kim_default_addressless,
-NULL,
+kim_empty_string,
 NULL,
 NULL };
 
@@ -384,17 +384,16 @@ kim_error kim_options_set_service_name (kim_options  io_options,
                                         kim_string   in_service_name)
 {
     kim_error err = KIM_NO_ERROR;
-    kim_string service_name = NULL;
     
     if (!err && !io_options) { err = check_error (KIM_NULL_PARAMETER_ERR); }
     
-    if (!err && in_service_name) {
-        err = kim_string_copy (&service_name, in_service_name);
-    }
-    
     if (!err) {
        kim_string_free (&io_options->service_name);
-       io_options->service_name = service_name;
+        if (in_service_name) {
+            err = kim_string_copy (&io_options->service_name, in_service_name);
+        } else {
+            io_options->service_name = kim_empty_string;
+        }
     }
     
     return check_error (err);
@@ -411,7 +410,8 @@ kim_error kim_options_get_service_name (kim_options  in_options,
     if (!err && !out_service_name) { err = check_error (KIM_NULL_PARAMETER_ERR); }
     
     if (!err) {
-        if (in_options->service_name) {
+        if (in_options->service_name && 
+            in_options->service_name != kim_empty_string) {
             err = kim_string_copy (out_service_name, in_options->service_name);
         } else {
             *out_service_name = NULL;
@@ -428,7 +428,11 @@ kim_error kim_options_get_service_name (kim_options  in_options,
 char *kim_options_service_name (kim_options in_options)
 {
     if (in_options) {
-        return (char *) in_options->service_name;
+        if (in_options->service_name == kim_empty_string) {
+            return NULL;
+        } else {
+            return (char *) in_options->service_name;
+        }
     }
     check_error (KIM_NULL_PARAMETER_ERR);  /* log bad options input */
     return NULL;
@@ -560,9 +564,7 @@ kim_error kim_options_write_to_stream (kim_options   in_options,
     }
     
     if (!err) {
-        kim_string service_name = (options->service_name ?
-                                   options->service_name : "");
-        err = k5_ipc_stream_write_string (io_stream, service_name);
+        err = k5_ipc_stream_write_string (io_stream,  options->service_name);
     }
     
     if (options != in_options) { kim_options_free (&options); }
@@ -618,7 +620,7 @@ kim_error kim_options_read_from_stream (kim_options    io_options,
             if (service_name[0]) {
                 err = kim_string_copy (&io_options->service_name, service_name);
             } else {
-                io_options->service_name = NULL;
+                io_options->service_name = kim_empty_string;
             }
         }
         
index 7954353597629d4be1eb5c8f3f44b461deb5d2db..c9d5df16d68146b94f2adc203d10eeb76d4fc8de 100644 (file)
@@ -42,16 +42,16 @@ struct kim_selection_hints_opaque {
 
 struct kim_selection_hints_opaque kim_selection_hints_initializer = { 
     NULL,
-    NULL,
+    kim_empty_string,
     KIM_OPTIONS_DEFAULT,
     TRUE,
     TRUE,
-    NULL,
-    NULL,
-    NULL,
-    NULL,
-    NULL,
-    NULL
+    kim_empty_string,
+    kim_empty_string,
+    kim_empty_string,
+    kim_empty_string,
+    kim_empty_string,
+    kim_empty_string
 };
 
 /* ------------------------------------------------------------------------ */
@@ -196,26 +196,32 @@ kim_error kim_selection_hints_set_hint (kim_selection_hints io_selection_hints,
     
     if (!err) {
         if (!strcmp (in_hint_key, kim_hint_key_client_realm)) {
+            kim_string_free (&io_selection_hints->client_realm);
             err = kim_string_copy (&io_selection_hints->client_realm, 
                                    in_hint_string);
             
         } else if (!strcmp (in_hint_key, kim_hint_key_user)) {
+            kim_string_free (&io_selection_hints->user);
             err = kim_string_copy (&io_selection_hints->user, 
                                    in_hint_string);
             
         } else if (!strcmp (in_hint_key, kim_hint_key_service_realm)) {
+            kim_string_free (&io_selection_hints->service_realm);
             err = kim_string_copy (&io_selection_hints->service_realm, 
                                    in_hint_string);
             
         } else if (!strcmp (in_hint_key, kim_hint_key_service)) {
+            kim_string_free (&io_selection_hints->service);
             err = kim_string_copy (&io_selection_hints->service, 
                                    in_hint_string);
             
         } else if (!strcmp (in_hint_key, kim_hint_key_server)) {
+            kim_string_free (&io_selection_hints->server);
             err = kim_string_copy (&io_selection_hints->server, 
                                    in_hint_string);
             
         } else if (!strcmp (in_hint_key, kim_hint_key_service_identity)) {
+            kim_string_free (&io_selection_hints->service_identity);
             err = kim_string_copy (&io_selection_hints->service_identity, 
                                    in_hint_string);
             
@@ -235,6 +241,7 @@ kim_error kim_selection_hints_get_hint (kim_selection_hints  in_selection_hints,
                                         kim_string          *out_hint_string)
 {
     kim_error err = KIM_NO_ERROR;
+    kim_string hint = NULL;
     
     if (!err && !in_selection_hints) { err = check_error (KIM_NULL_PARAMETER_ERR); }
     if (!err && !in_hint_key       ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
@@ -242,28 +249,22 @@ kim_error kim_selection_hints_get_hint (kim_selection_hints  in_selection_hints,
     
     if (!err) {
         if (!strcmp (in_hint_key, kim_hint_key_client_realm)) {
-            err = kim_string_copy (out_hint_string, 
-                                   in_selection_hints->client_realm);
+            hint = in_selection_hints->client_realm;
             
         } else if (!strcmp (in_hint_key, kim_hint_key_user)) {
-            err = kim_string_copy (out_hint_string, 
-                                   in_selection_hints->user);
+            hint = in_selection_hints->user;
             
         } else if (!strcmp (in_hint_key, kim_hint_key_service_realm)) {
-            err = kim_string_copy (out_hint_string, 
-                                   in_selection_hints->service_realm);
+            hint = in_selection_hints->service_realm;
             
         } else if (!strcmp (in_hint_key, kim_hint_key_service)) {
-            err = kim_string_copy (out_hint_string, 
-                                   in_selection_hints->service);
-            
+            hint = in_selection_hints->service;
+           
         } else if (!strcmp (in_hint_key, kim_hint_key_server)) {
-            err = kim_string_copy (out_hint_string, 
-                                   in_selection_hints->server);
+            hint = in_selection_hints->server;
             
         } else if (!strcmp (in_hint_key, kim_hint_key_service_identity)) {
-            err = kim_string_copy (out_hint_string, 
-                                   in_selection_hints->service_identity);
+            hint = in_selection_hints->service_identity;
             
         } else {
             err = kim_error_set_message_for_code (KIM_UNSUPPORTED_HINT_ERR,
@@ -271,6 +272,14 @@ kim_error kim_selection_hints_get_hint (kim_selection_hints  in_selection_hints,
         }
     }
     
+    if (!err) {
+        if (hint && hint != kim_empty_string) {
+            err = kim_string_copy (out_hint_string, hint);
+        } else {
+            *out_hint_string = NULL;
+        }
+    }
+    
     return check_error (err);
 }
 
@@ -302,7 +311,8 @@ kim_error kim_selection_hints_get_explanation (kim_selection_hints  in_selection
     if (!err && !out_explanation   ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
     
     if (!err) {
-        if (in_selection_hints->explanation) {
+        if (in_selection_hints->explanation && 
+            in_selection_hints->explanation != kim_empty_string) {
             err = kim_string_copy (out_explanation, in_selection_hints->explanation);
         } else {
             *out_explanation = NULL;
@@ -576,49 +586,43 @@ kim_error kim_selection_hints_write_to_stream (kim_selection_hints in_selection_
     }
     
     if (!err) {
-        kim_string explanation = (in_selection_hints->explanation ?
-                                  in_selection_hints->explanation : "");
-        err = k5_ipc_stream_write_string (io_stream, explanation);
+        err = k5_ipc_stream_write_string (io_stream, 
+                                          in_selection_hints->explanation);
     }
     
     if (!err) {
-        err = kim_options_write_to_stream (in_selection_hints->options, io_stream);
+        err = kim_options_write_to_stream (in_selection_hints->options, 
+                                           io_stream);
     }
     
     if (!err) {
-        kim_string service_identity = (in_selection_hints->service_identity ?
-                                       in_selection_hints->service_identity : "");
-        err = k5_ipc_stream_write_string (io_stream, service_identity);
+        err = k5_ipc_stream_write_string (io_stream, 
+                                          in_selection_hints->service_identity);
     }
     
     if (!err) {
-        kim_string client_realm = (in_selection_hints->client_realm ?
-                                   in_selection_hints->client_realm : "");
-        err = k5_ipc_stream_write_string (io_stream, client_realm);
+        err = k5_ipc_stream_write_string (io_stream, 
+                                          in_selection_hints->client_realm);
     }
     
     if (!err) {
-        kim_string user = (in_selection_hints->user ?
-                           in_selection_hints->user : "");
-        err = k5_ipc_stream_write_string (io_stream, user);
+        err = k5_ipc_stream_write_string (io_stream, 
+                                          in_selection_hints->user);
     }
     
     if (!err) {
-        kim_string service_realm = (in_selection_hints->service_realm ?
-                                    in_selection_hints->service_realm : "");
-        err = k5_ipc_stream_write_string (io_stream, service_realm);
+        err = k5_ipc_stream_write_string (io_stream, 
+                                          in_selection_hints->service_realm);
     }
     
     if (!err) {
-        kim_string service = (in_selection_hints->service ?
-                              in_selection_hints->service : "");
-        err = k5_ipc_stream_write_string (io_stream, service);
+       err = k5_ipc_stream_write_string (io_stream, 
+                                         in_selection_hints->service);
     }
     
     if (!err) {
-        kim_string server = (in_selection_hints->server ?
-                             in_selection_hints->server : "");
-        err = k5_ipc_stream_write_string (io_stream, server);
+        err = k5_ipc_stream_write_string (io_stream, 
+                                          in_selection_hints->server);
     }
     
     return check_error (err);    
@@ -651,13 +655,8 @@ kim_error kim_selection_hints_read_from_stream (kim_selection_hints io_selection
         err = k5_ipc_stream_read_string (io_stream, &explanation);
         
         if (!err) {
-            if (!explanation[0]) {
-                err = kim_string_copy (&io_selection_hints->explanation, 
-                                       explanation);
-            } else {
-                err = kim_selection_hints_set_explanation (io_selection_hints, 
-                                                           NULL);
-            }
+            err = kim_string_copy (&io_selection_hints->explanation, 
+                                   explanation);
         }
         
         k5_ipc_stream_free_string (explanation);
@@ -678,14 +677,8 @@ kim_error kim_selection_hints_read_from_stream (kim_selection_hints io_selection
         err = k5_ipc_stream_read_string (io_stream, &service_identity);
         
         if (!err) {
-            if (!service_identity[0]) {
-                err = kim_string_copy (&io_selection_hints->service_identity, 
-                                       service_identity);
-            } else {
-                err = kim_selection_hints_set_hint (io_selection_hints, 
-                                                    kim_hint_key_service_identity,
-                                                    NULL);
-            }
+            err = kim_string_copy (&io_selection_hints->service_identity, 
+                                   service_identity);
         }
         
         k5_ipc_stream_free_string (service_identity);
@@ -696,14 +689,8 @@ kim_error kim_selection_hints_read_from_stream (kim_selection_hints io_selection
         err = k5_ipc_stream_read_string (io_stream, &client_realm);
         
         if (!err) {
-            if (!client_realm[0]) {
-                err = kim_string_copy (&io_selection_hints->client_realm, 
-                                       client_realm);
-            } else {
-                err = kim_selection_hints_set_hint (io_selection_hints, 
-                                                    kim_hint_key_client_realm,
-                                                    NULL);
-            }
+            err = kim_string_copy (&io_selection_hints->client_realm, 
+                                   client_realm);
         }
         
         k5_ipc_stream_free_string (client_realm);
@@ -714,13 +701,7 @@ kim_error kim_selection_hints_read_from_stream (kim_selection_hints io_selection
         err = k5_ipc_stream_read_string (io_stream, &user);
         
         if (!err) {
-            if (!user[0]) {
-                err = kim_string_copy (&io_selection_hints->user, user);
-            } else {
-                err = kim_selection_hints_set_hint (io_selection_hints,
-                                                    kim_hint_key_user,
-                                                    NULL);
-            }
+            err = kim_string_copy (&io_selection_hints->user, user);
         }
         
         k5_ipc_stream_free_string (user);
@@ -731,14 +712,8 @@ kim_error kim_selection_hints_read_from_stream (kim_selection_hints io_selection
         err = k5_ipc_stream_read_string (io_stream, &service_realm);
         
         if (!err) {
-            if (!service_realm[0]) {
-                err = kim_string_copy (&io_selection_hints->service_realm, 
-                                       service_realm);
-            } else {
-                err = kim_selection_hints_set_hint (io_selection_hints,
-                                                    kim_hint_key_service_realm,
-                                                    NULL);
-            }
+            err = kim_string_copy (&io_selection_hints->service_realm, 
+                                   service_realm);
         }
         
         k5_ipc_stream_free_string (service_realm);
@@ -749,13 +724,7 @@ kim_error kim_selection_hints_read_from_stream (kim_selection_hints io_selection
         err = k5_ipc_stream_read_string (io_stream, &service);
         
         if (!err) {
-            if (!service[0]) {
-                err = kim_string_copy (&io_selection_hints->service, service);
-            } else {
-                err = kim_selection_hints_set_hint (io_selection_hints,
-                                                    kim_hint_key_service,
-                                                    NULL);
-            }
+            err = kim_string_copy (&io_selection_hints->service, service);
         }
         
         k5_ipc_stream_free_string (service);
@@ -766,16 +735,10 @@ kim_error kim_selection_hints_read_from_stream (kim_selection_hints io_selection
         err = k5_ipc_stream_read_string (io_stream, &server);
         
         if (!err) {
-            if (!server[0]) {
-                err = kim_string_copy (&io_selection_hints->server, server);
-            } else {
-                err = kim_selection_hints_set_hint (io_selection_hints,
-                                                    kim_hint_key_server,
-                                                    NULL);
-            }
+            err = kim_string_copy (&io_selection_hints->server, server);
         }
         
-         k5_ipc_stream_free_string (server);
+        k5_ipc_stream_free_string (server);
     }
     
     return check_error (err);    
index b84a12c8ee17cb5bf6371a3b0758ece80754b270..8b9af7010ec956b677bba8f792391a8f021daf98 100644 (file)
@@ -26,6 +26,8 @@
 
 #include "kim_private.h"
 
+const char kim_empty_string[1] = "";
+
 /* ------------------------------------------------------------------------ */
 
 kim_error kim_string_create_from_format (kim_string *out_string, 
@@ -136,12 +138,19 @@ kim_error kim_string_copy (kim_string *out_string,
     if (!err && !in_string ) { err = check_error (KIM_NULL_PARAMETER_ERR); }
     
     if (!err) {
-        string = calloc (strlen (in_string) + 1, sizeof (char *));
-        if (!string) { err = check_error (KIM_OUT_OF_MEMORY_ERR); }
+        if (in_string[0]) {
+            string = calloc (strlen (in_string) + 1, sizeof (char *));
+            if (!string) { err = check_error (KIM_OUT_OF_MEMORY_ERR); }
+            
+            if (!err) {
+                strncpy ((char *) string, in_string, strlen (in_string) + 1);
+            }
+        } else {
+            string = kim_empty_string;
+        }
     }
     
     if (!err) {
-        strncpy ((char *) string, in_string, strlen (in_string) + 1);
         *out_string = string;
         string = NULL;
     }
@@ -167,7 +176,7 @@ kim_error kim_string_compare (kim_string      in_string,
 
 void kim_string_free (kim_string *io_string)
 {
-    if (io_string && *io_string) { 
+    if (io_string && *io_string && *io_string != kim_empty_string) { 
         free ((char *) *io_string);
         *io_string = NULL;
     }
index 48d7bae13e7510654cbb21526be25cc65aac7062..6f4e0ad360c401031c2557ea3cbf5ede2cc1907a 100644 (file)
@@ -29,6 +29,8 @@
 
 #include <kim/kim.h>
 
+extern const char kim_empty_string[1];
+
 /* ------------------------------------------------------------------------ */
 
 static inline kim_count kim_string_buflen (kim_string in_string)
index 5011d548ce1aab1b41462c22c582bfa6e54e75ae..4799e910737f1b193cc3dd7c0fb0582b9a688f65 100644 (file)
@@ -33,7 +33,7 @@
 #include "prof_int.h"          /* XXX for profile_copy, not public yet */
 
 #ifdef USE_KIM
-#include "kim/kim_library.h"
+#include "kim_library_private.h"
 #endif
 
 #if defined(_WIN32)