Navigation Menu

Skip to content

Commit

Permalink
ticket: new
Browse files Browse the repository at this point in the history
subject: 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).


git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@20927 dc483132-0cff-0310-8789-dd5450dbe970
  • Loading branch information
lxs committed Oct 27, 2008
1 parent f5dd7d0 commit 48e1dba
Showing 1 changed file with 34 additions and 76 deletions.
110 changes: 34 additions & 76 deletions src/util/support/errors.c
Expand Up @@ -11,9 +11,8 @@
#include "k5-platform.h"
#include "supp-int.h"

#ifdef __APPLE__
#import <CoreFoundation/CoreFoundation.h>
#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
Expand Down Expand Up @@ -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 *
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 48e1dba

Please sign in to comment.