ICU-2864 if Locale construction fails, set the Locale object to bogus instead of attempting to set the default locale ID or "en" or similar

X-SVN-Rev: 11917
This commit is contained in:
Markus Scherer 2003-05-14 00:30:50 +00:00
parent 31d1414177
commit ab37e7a4c3
2 changed files with 31 additions and 29 deletions

View file

@ -352,7 +352,7 @@ Locale& Locale::init(const char* localeID)
if(localeID == NULL) {
// not an error, just set the default locale
break;
return *this = getDefault();
}
// "canonicalize" the locale ID to ICU/Java format
@ -363,7 +363,7 @@ Locale& Locale::init(const char* localeID)
fullName = (char *)uprv_malloc(sizeof(char)*(length + 1));
if(fullName == 0) {
fullName = fullNameBuffer;
break;
break; // error: out of memory
}
err = U_ZERO_ERROR;
length = uloc_getName(localeID, fullName, length + 1, &err);
@ -384,7 +384,7 @@ Locale& Locale::init(const char* localeID)
length = (int32_t)(separator - fullName);
if(length > 0) {
if(length >= (int32_t)sizeof(language)) {
break;
break; // error: language code too long
}
uprv_memcpy(language, fullName, length);
}
@ -397,7 +397,7 @@ Locale& Locale::init(const char* localeID)
length = (int32_t)(separator - prev);
if(length > 0) {
if(length >= (int32_t)sizeof(country)) {
break;
break; // error: country code too long
}
uprv_memcpy(country, prev, length);
}
@ -407,14 +407,14 @@ Locale& Locale::init(const char* localeID)
} else {
/* variantBegin==strlen(fullName), length==strlen(language)==prev-1-fullName */
if((variantBegin - length - 1) >= (int32_t)sizeof(country)) {
break;
break; // error: country code too long
}
uprv_strcpy(country, prev);
}
} else {
/* variantBegin==strlen(fullName) */
if(variantBegin >= (int32_t)sizeof(language)) {
break;
break; // error: language code too long
}
uprv_strcpy(language, fullName);
}
@ -423,18 +423,9 @@ Locale& Locale::init(const char* localeID)
return *this;
} while(0);
umtx_lock(NULL);
UBool defaultLocaleIsOK = (gDefaultLocale != NULL);
umtx_unlock(NULL);
if (defaultLocaleIsOK) {
// when an error occurs, then set the default locale (there is no UErrorCode here)
*this = getDefault();
}
else {
// Prevent any possible infinite recursion from Locale::getDefault()
// for bad default Locale IDs
init("en");
}
// when an error occurs, then set this object to "bogus" (there is no UErrorCode here)
setToBogus();
return *this;
}

View file

@ -224,26 +224,35 @@ public:
static const Locale &getCanadaFrench(void);
/**
* Construct an empty locale. It's only used when a fill-in parameter is
* needed.
* @stable ICU 2.0
*/
/**
* Construct a default locale object, a Locale for the default locale ID.
*
* @see getDefault
* @see uloc_getDefault
* @stable ICU 2.0
*/
Locale();
/**
* Construct a locale from language, country, variant.
* If an error occurs, then the constructed object will be "bogus"
* (isBogus() will return TRUE).
*
* @param language Lowercase two-letter ISO-639 code. This parameter can
* instead be an ICU style C locale (e.g. "en_US"), but the other
* parameters must not be used. This parameter can be null, if so
* @param language Lowercase two-letter or three-letter ISO-639 code.
* This parameter can instead be an ICU style C locale (e.g. "en_US"),
* but the other parameters must not be used.
* This parameter can be NULL; if so,
* the locale is initialized to match the current default locale.
* (This is the same as using the default constructor.)
* Please note: The Java Locale class does NOT accept the form
* 'new Locale("en_US")' but only 'new Locale("en","US")'
*
* @param country Uppercase two-letter ISO-3166 code. (optional)
* @param variant Uppercase vendor and browser specific code. See class
* description. (optional)
*
* @see getDefault
* @see uloc_getDefault
* @stable ICU 2.0
*/
Locale( const char * language,
@ -304,15 +313,17 @@ public:
* different fields, e.g. in a spreadsheet.
*
* Note that the initial setting will match the host system.
* @retrun the default locale for this instance of the Java Virtual Machine
* @return a reference to the Locale object for the default locale ID
* @system
* @stable ICU 2.0
*/
static const Locale& getDefault(void);
/**
* Sets the default. Normally set once at the beginning of applet or
* application, then never reset. setDefault does NOT reset the host locale.
* Sets the default. Normally set once at the beginning of a process,
* then never reset.
* setDefault() only changes ICU's default locale ID, <strong>not</strong>
* the default locale ID of the runtime environment.
*
* @param newLocale Locale to set to.
* @param success The error code.