From ab0b1c37bb2cc13c971461bba419f6ce5f6774bf Mon Sep 17 00:00:00 2001 From: Alexandra Ellwood Date: Wed, 1 Oct 2008 21:59:16 +0000 Subject: [PATCH] Make unset strings in kim_options and kim_selection_hints 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 | 4 +- src/kim/lib/kim_library_private.h | 1 + src/kim/lib/kim_options.c | 28 ++--- src/kim/lib/kim_selection_hints.c | 155 ++++++++++---------------- src/kim/lib/kim_string.c | 17 ++- src/kim/lib/kim_string_private.h | 2 + src/lib/krb5/os/init_os_ctx.c | 2 +- 7 files changed, 94 insertions(+), 115 deletions(-) diff --git a/src/include/kim/kim_selection_hints.h b/src/include/kim/kim_selection_hints.h index 6e7ee24c5..afeae635e 100644 --- a/src/include/kim/kim_selection_hints.h +++ b/src/include/kim/kim_selection_hints.h @@ -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. diff --git a/src/kim/lib/kim_library_private.h b/src/kim/lib/kim_library_private.h index 83c06d791..160fba3a5 100644 --- a/src/kim/lib/kim_library_private.h +++ b/src/kim/lib/kim_library_private.h @@ -28,6 +28,7 @@ #define KIM_LIBRARY_PRIVATE_H #include +#include kim_error kim_library_init (void); diff --git a/src/kim/lib/kim_options.c b/src/kim/lib/kim_options.c index 8cefa2ecf..06c25ae88 100644 --- a/src/kim/lib/kim_options.c +++ b/src/kim/lib/kim_options.c @@ -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; } } diff --git a/src/kim/lib/kim_selection_hints.c b/src/kim/lib/kim_selection_hints.c index 795435359..c9d5df16d 100644 --- a/src/kim/lib/kim_selection_hints.c +++ b/src/kim/lib/kim_selection_hints.c @@ -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); diff --git a/src/kim/lib/kim_string.c b/src/kim/lib/kim_string.c index b84a12c8e..8b9af7010 100644 --- a/src/kim/lib/kim_string.c +++ b/src/kim/lib/kim_string.c @@ -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; } diff --git a/src/kim/lib/kim_string_private.h b/src/kim/lib/kim_string_private.h index 48d7bae13..6f4e0ad36 100644 --- a/src/kim/lib/kim_string_private.h +++ b/src/kim/lib/kim_string_private.h @@ -29,6 +29,8 @@ #include +extern const char kim_empty_string[1]; + /* ------------------------------------------------------------------------ */ static inline kim_count kim_string_buflen (kim_string in_string) diff --git a/src/lib/krb5/os/init_os_ctx.c b/src/lib/krb5/os/init_os_ctx.c index 5011d548c..4799e9107 100644 --- a/src/lib/krb5/os/init_os_ctx.c +++ b/src/lib/krb5/os/init_os_ctx.c @@ -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) -- 2.26.2