Reject encoding > ENCODING_LEN at early stage instead of truncating it.

Use ptr == NULL instead of !ptr in few places.
Move saverr declaration to global section.
This commit is contained in:
ache 2002-08-05 09:58:45 +00:00
parent fbb42c58e2
commit bb7a785bc8

View File

@ -100,7 +100,7 @@ setlocale(category, locale)
int category;
const char *locale;
{
int i, j, len;
int i, j, len, saverr;
char *env, *r;
if (category < LC_ALL || category >= _LC_LAST) {
@ -108,7 +108,7 @@ setlocale(category, locale)
return (NULL);
}
if (!locale)
if (locale == NULL)
return (category != LC_ALL ?
current_categories[category] : currentlocale());
@ -124,32 +124,47 @@ setlocale(category, locale)
if (!*locale) {
env = getenv("LC_ALL");
if (category != LC_ALL && (!env || !*env))
if (category != LC_ALL && (env == NULL || !*env))
env = getenv(categories[category]);
if (!env || !*env)
if (env == NULL || !*env)
env = getenv("LANG");
if (!env || !*env)
if (env == NULL || !*env)
env = "C";
(void)strlcpy(new_categories[category], env, ENCODING_LEN + 1);
if (strlen(env) > ENCODING_LEN) {
errno = EINVAL;
return (NULL);
}
(void)strcpy(new_categories[category], env);
if (category == LC_ALL) {
for (i = 1; i < _LC_LAST; ++i) {
if (!(env = getenv(categories[i])) || !*env)
if ((env = getenv(categories[i])) == NULL ||
!*env)
env = new_categories[LC_ALL];
(void)strlcpy(new_categories[i], env,
ENCODING_LEN + 1);
else if (strlen(env) > ENCODING_LEN) {
errno = EINVAL;
return (NULL);
}
(void)strcpy(new_categories[i], env);
}
}
} else if (category != LC_ALL)
(void)strlcpy(new_categories[category], locale,
ENCODING_LEN + 1);
else {
} else if (category != LC_ALL) {
if (strlen(locale) > ENCODING_LEN) {
errno = EINVAL;
return (NULL);
}
(void)strcpy(new_categories[category], locale);
} else {
if ((r = strchr(locale, '/')) == NULL) {
if (strlen(locale) > ENCODING_LEN) {
errno = EINVAL;
return (NULL);
}
for (i = 1; i < _LC_LAST; ++i)
(void)strlcpy(new_categories[i], locale,
ENCODING_LEN + 1);
(void)strcpy(new_categories[i], locale);
} else {
for (i = 1; r[1] == '/'; ++r)
;
@ -160,8 +175,10 @@ setlocale(category, locale)
do {
if (i == _LC_LAST)
break; /* Too many slashes... */
len = r - locale > ENCODING_LEN ?
ENCODING_LEN : r - locale;
if ((len = r - locale) > ENCODING_LEN) {
errno = EINVAL;
return (NULL);
}
(void)strlcpy(new_categories[i], locale,
len + 1);
i++;
@ -185,8 +202,7 @@ setlocale(category, locale)
for (i = 1; i < _LC_LAST; ++i) {
(void)strcpy(saved_categories[i], current_categories[i]);
if (loadlocale(i) == NULL) {
int saverr = errno;
saverr = errno;
for (j = 1; j < i; j++) {
(void)strcpy(new_categories[j],
saved_categories[j]);