From 1fbffa765c7c52ab4eb89108096c207c562589d6 Mon Sep 17 00:00:00 2001 From: ache Date: Thu, 6 Feb 1997 09:11:06 +0000 Subject: Use symbolic constants instead of hardcoded digits Add range check for setrunelocale since it can be called directly. Remove _startup_setlocale compatibility function Should go into 2.2 --- lib/libc/locale/collate.c | 2 +- lib/libc/locale/setlocale.c | 44 ++++++++++++++++------------------------- lib/libc/locale/setlocale.h | 30 ++++++++++++++++++++++++++++ lib/libc/locale/setrunelocale.c | 7 +++---- 4 files changed, 51 insertions(+), 32 deletions(-) create mode 100644 lib/libc/locale/setlocale.h (limited to 'lib/libc') diff --git a/lib/libc/locale/collate.c b/lib/libc/locale/collate.c index b875acb..0d479a1 100644 --- a/lib/libc/locale/collate.c +++ b/lib/libc/locale/collate.c @@ -35,8 +35,8 @@ #include #include #include "collate.h" +#include "setlocale.h" -char *_PathLocale; int __collate_load_error = 1; char __collate_version[STR_LEN]; u_char __collate_substitute_table[UCHAR_MAX + 1][STR_LEN]; diff --git a/lib/libc/locale/setlocale.c b/lib/libc/locale/setlocale.c index 686e36d..656ed85 100644 --- a/lib/libc/locale/setlocale.c +++ b/lib/libc/locale/setlocale.c @@ -54,6 +54,7 @@ static char sccsid[] = "@(#)setlocale.c 8.1 (Berkeley) 7/4/93"; #include #include #include "collate.h" +#include "setlocale.h" /* * Category names for getenv() @@ -70,7 +71,7 @@ static char *categories[_LC_LAST] = { /* * Current locales for each category */ -static char current_categories[_LC_LAST][32] = { +static char current_categories[_LC_LAST][ENCODING_LEN + 1] = { "C", "C", "C", @@ -82,11 +83,10 @@ static char current_categories[_LC_LAST][32] = { /* * The locales we are going to try and load */ -static char new_categories[_LC_LAST][32]; -static char saved_categories[_LC_LAST][32]; +static char new_categories[_LC_LAST][ENCODING_LEN + 1]; +static char saved_categories[_LC_LAST][ENCODING_LEN + 1]; -static char current_locale_string[_LC_LAST * 33]; -char *_PathLocale; +static char current_locale_string[_LC_LAST * (ENCODING_LEN + 1/*"/"*/ + 1)]; static char *currentlocale __P((void)); static char *loadlocale __P((int)); @@ -134,33 +134,34 @@ setlocale(category, locale) if (!env || !*env) env = "C"; - (void) strncpy(new_categories[category], env, 31); - new_categories[category][31] = 0; + (void) strncpy(new_categories[category], env, ENCODING_LEN); + new_categories[category][ENCODING_LEN] = '\0'; if (category == LC_ALL) { for (i = 1; i < _LC_LAST; ++i) { if (!(env = getenv(categories[i])) || !*env) env = new_categories[LC_ALL]; - (void)strncpy(new_categories[i], env, 31); - new_categories[i][31] = 0; + (void)strncpy(new_categories[i], env, ENCODING_LEN); + new_categories[i][ENCODING_LEN] = '\0'; } } } else if (category != LC_ALL) { - (void)strncpy(new_categories[category], locale, 31); - new_categories[category][31] = 0; + (void)strncpy(new_categories[category], locale, ENCODING_LEN); + new_categories[category][ENCODING_LEN] = '\0'; } else { - if ((r = strchr(locale, '/')) == 0) { + if ((r = strchr(locale, '/')) == NULL) { for (i = 1; i < _LC_LAST; ++i) { - (void)strncpy(new_categories[i], locale, 31); - new_categories[i][31] = 0; + (void)strncpy(new_categories[i], locale, ENCODING_LEN); + new_categories[i][ENCODING_LEN] = '\0'; } } else { for (i = 1; r[1] == '/'; ++r); if (!r[1]) return (NULL); /* Hmm, just slashes... */ do { - len = r - locale > 31 ? 31 : r - locale; + len = r - locale > ENCODING_LEN ? ENCODING_LEN : r - locale; (void)strncpy(new_categories[i], locale, len); - new_categories[i++][len] = 0; + new_categories[i][len] = '\0'; + i++; locale = r; while (*locale == '/') ++locale; @@ -190,17 +191,6 @@ setlocale(category, locale) return (currentlocale()); } -/* To be compatible with crt0 hack */ -void -_startup_setlocale(category, locale) - int category; - const char *locale; -{ -#ifndef XPG4 - (void) setlocale(category, locale); -#endif -} - static char * currentlocale() { diff --git a/lib/libc/locale/setlocale.h b/lib/libc/locale/setlocale.h new file mode 100644 index 0000000..d96abac --- /dev/null +++ b/lib/libc/locale/setlocale.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 1997 by Andrey A. Chernov, Moscow, Russia. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + */ + +#define ENCODING_LEN 31 + +char *_PathLocale; + diff --git a/lib/libc/locale/setrunelocale.c b/lib/libc/locale/setrunelocale.c index 4297206..d422e39 100644 --- a/lib/libc/locale/setrunelocale.c +++ b/lib/libc/locale/setrunelocale.c @@ -40,8 +40,7 @@ #include #include #include - -char *_PathLocale; +#include "setlocale.h" extern int _none_init __P((_RuneLocale *)); #ifdef XPG4 @@ -72,7 +71,7 @@ _xpg4_setrunelocale(encoding) char name[PATH_MAX]; _RuneLocale *rl; - if (!encoding) + if (!encoding || strlen(encoding) > ENCODING_LEN) return(EFAULT); /* @@ -85,7 +84,7 @@ _xpg4_setrunelocale(encoding) if (!_PathLocale) _PathLocale = _PATH_LOCALE; - /* Range checking not needed, encoding has fixed size */ + /* Range checking not needed, encoding length already checked above */ (void) strcpy(name, _PathLocale); (void) strcat(name, "/"); (void) strcat(name, encoding); -- cgit v1.1