about summary refs log tree commit diff
path: root/src/locale/setlocale.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2018-10-20 21:54:20 -0400
committerRich Felker <dalias@aerifal.cx>2018-10-20 21:54:20 -0400
commitd88e5dfa8b989dafff4b748bfb3cba3512c8482e (patch)
tree55aed89246eda30168d6b60d86073a55f3b3f4e9 /src/locale/setlocale.c
parent8084d6ab57cdb0b8f328d3cdbad3b9d09eaaee04 (diff)
downloadmusl-d88e5dfa8b989dafff4b748bfb3cba3512c8482e.tar.gz
musl-d88e5dfa8b989dafff4b748bfb3cba3512c8482e.tar.xz
musl-d88e5dfa8b989dafff4b748bfb3cba3512c8482e.zip
adapt setlocale to support possibility of failure
introduce a new LOC_MAP_FAILED sentinel for errors, since null
pointers for a category's locale map indicate the C locale. at this
time, __get_locale does not fail, so there should be no functional
change by this commit.
Diffstat (limited to 'src/locale/setlocale.c')
-rw-r--r--src/locale/setlocale.c32
1 files changed, 20 insertions, 12 deletions
diff --git a/src/locale/setlocale.c b/src/locale/setlocale.c
index 11d823ce..637e7aa0 100644
--- a/src/locale/setlocale.c
+++ b/src/locale/setlocale.c
@@ -7,19 +7,10 @@
 
 static char buf[LC_ALL*(LOCALE_NAME_MAX+1)];
 
-static char *setlocale_one_unlocked(int cat, const char *name)
-{
-	const struct __locale_map *lm;
-
-	if (name) libc.global_locale.cat[cat] = lm = __get_locale(cat, name);
-	else lm = libc.global_locale.cat[cat];
-
-	return lm ? (char *)lm->name : "C";
-}
-
 char *setlocale(int cat, const char *name)
 {
 	static volatile int lock[1];
+	const struct __locale_map *lm;
 
 	if ((unsigned)cat > LC_ALL) return 0;
 
@@ -33,6 +24,7 @@ char *setlocale(int cat, const char *name)
 	if (cat == LC_ALL) {
 		int i;
 		if (name) {
+			struct __locale_struct tmp_locale;
 			char part[LOCALE_NAME_MAX+1] = "C.UTF-8";
 			const char *p = name;
 			for (i=0; i<LC_ALL; i++) {
@@ -42,8 +34,14 @@ char *setlocale(int cat, const char *name)
 					part[z-p] = 0;
 					if (*z) p = z+1;
 				}
-				setlocale_one_unlocked(i, part);
+				lm = __get_locale(i, name);
+				if (lm == LOC_MAP_FAILED) {
+					UNLOCK(lock);
+					return 0;
+				}
+				tmp_locale.cat[i] = lm;
 			}
+			libc.global_locale = tmp_locale;
 		}
 		char *s = buf;
 		const char *part;
@@ -63,7 +61,17 @@ char *setlocale(int cat, const char *name)
 		return same==LC_ALL ? (char *)part : buf;
 	}
 
-	char *ret = setlocale_one_unlocked(cat, name);
+	if (name) {
+		lm = __get_locale(cat, name);
+		if (lm == LOC_MAP_FAILED) {
+			UNLOCK(lock);
+			return 0;
+		}
+		libc.global_locale.cat[cat] = lm;
+	} else {
+		lm = libc.global_locale.cat[cat];
+	}
+	char *ret = lm ? (char *)lm->name : "C";
 
 	UNLOCK(lock);