From 6b2185530b56fc1230cb639016df40d44c7bb2e7 Mon Sep 17 00:00:00 2001 From: Alexandra Ellwood Date: Mon, 27 Oct 2008 21:01:00 +0000 Subject: [PATCH] localize format strings, not final error string errors.c should localize the incoming format string, not the string produced by vasprintf. The format string is constant and thus can be added to a localization table, whereas the output string is not. Note that this change depends on error_message also localizing error table strings (which it does for KfM already). ticket: new git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@20927 dc483132-0cff-0310-8789-dd5450dbe970 --- src/util/support/errors.c | 110 ++++++++++++-------------------------- 1 file changed, 34 insertions(+), 76 deletions(-) diff --git a/src/util/support/errors.c b/src/util/support/errors.c index b0c2ae013..d85ba4244 100644 --- a/src/util/support/errors.c +++ b/src/util/support/errors.c @@ -11,9 +11,8 @@ #include "k5-platform.h" #include "supp-int.h" -#ifdef __APPLE__ -#import -#include "kim_library_private.h" +#ifdef USE_KIM +#include "kim_string_private.h" #endif /* It would be nice to just use error_message() always. Pity that @@ -48,81 +47,45 @@ void krb5int_vset_error (struct errinfo *ep, long code, const char *fmt, va_list args) { - char *p; - char *str = NULL; va_list args2; - - if (ep->msg && ep->msg != ep->scratch_buf) { - free ((char *) ep->msg); - ep->msg = NULL; + char *str = NULL; + const char *loc_fmt = NULL; + +#ifdef USE_KIM + /* Try to localize the format string */ + if (kim_os_string_create_localized(&loc_fmt, fmt) != KIM_NO_ERROR) { + loc_fmt = fmt; } - ep->code = code; +#else + loc_fmt = fmt; +#endif + + /* try vasprintf first */ va_copy(args2, args); - if (vasprintf(&str, fmt, args2) >= 0 && str != NULL) { - va_end(args2); - ep->msg = str; - return; + if (vasprintf(&str, loc_fmt, args2) < 0) { + str = NULL; } va_end(args2); - /* Allocation failure? */ - vsnprintf(ep->scratch_buf, sizeof(ep->scratch_buf), fmt, args); - /* Try again, just in case. */ - p = strdup(ep->scratch_buf); - ep->msg = p ? p : ep->scratch_buf; -} - -static inline char * -krb5int_get_localized_error (const char *string) -{ - char *loc_string = NULL; -#ifdef __APPLE__ - if (kim_library_allow_home_directory_access ()) { - CFStringRef cfstring = NULL; - - cfstring = CFStringCreateWithCStringNoCopy(kCFAllocatorDefault, - string, - kCFStringEncodingUTF8, - kCFAllocatorNull); - if (cfstring) { - CFStringRef loc_cfstring = NULL; - - loc_cfstring = CFCopyLocalizedString(cfstring, ""); - if (loc_cfstring) { - char *loc_ptr = NULL; - - /* check if loc_cfstring is a UTF8 string internally - * so we can avoid using CFStringGetMaximumSizeForEncoding */ - loc_ptr = (char *) CFStringGetCStringPtr(loc_cfstring, - kCFStringEncodingUTF8); - if (loc_ptr) { - loc_string = strdup(loc_ptr); - - } else { - CFIndex len = 0; - - len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(loc_cfstring), - kCFStringEncodingUTF8) + 1; - - loc_string = malloc(len); - if (loc_string) { - if (!CFStringGetCString(loc_cfstring, loc_string, - len, kCFStringEncodingUTF8)) { - /* Conversion to C string failed. */ - free (loc_string); - loc_string = NULL; - } - } - } - - CFRelease(loc_cfstring); - } - - CFRelease(cfstring); - } + /* If that failed, try using scratch_buf */ + if (str == NULL) { + vsnprintf(ep->scratch_buf, sizeof(ep->scratch_buf), loc_fmt, args); + str = strdup(ep->scratch_buf); /* try allocating again */ } + + /* free old string before setting new one */ + if (ep->msg && ep->msg != ep->scratch_buf) { + free ((char *) ep->msg); + ep->msg = NULL; + } + ep->code = code; + ep->msg = str ? str : ep->scratch_buf; + +#ifdef USE_KIM + if (loc_fmt != fmt) { kim_string_free(&loc_fmt); } +#else + if (loc_fmt != fmt) { free((char *) loc_fmt); } #endif - return loc_string; } const char * @@ -190,13 +153,8 @@ krb5int_get_error (struct errinfo *ep, long code) unlock(); goto format_number; } - - r2 = krb5int_get_localized_error(r); - if (r2 == NULL) { - r2 = strdup(r); - } - + r2 = strdup(r); if (r2 == NULL) { strncpy(ep->scratch_buf, r, sizeof(ep->scratch_buf)); unlock(); -- 2.26.2