summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorache <ache@FreeBSD.org>2002-08-05 09:58:45 +0000
committerache <ache@FreeBSD.org>2002-08-05 09:58:45 +0000
commitbb7a785bc85df937bb2afee8f9cb4eb074ac9f4b (patch)
treec91703dab75fa901ec9c1d3e107a007a0511312a /lib
parentfbb42c58e2ac222340d969f06ce50338c2d7c71d (diff)
downloadFreeBSD-src-bb7a785bc85df937bb2afee8f9cb4eb074ac9f4b.zip
FreeBSD-src-bb7a785bc85df937bb2afee8f9cb4eb074ac9f4b.tar.gz
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.
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/locale/setlocale.c54
1 files changed, 35 insertions, 19 deletions
diff --git a/lib/libc/locale/setlocale.c b/lib/libc/locale/setlocale.c
index 184326c..6b249a9 100644
--- a/lib/libc/locale/setlocale.c
+++ b/lib/libc/locale/setlocale.c
@@ -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]);
OpenPOWER on IntegriCloud