summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/locale/_locale.h2
-rw-r--r--src/stdlib/_stdlib.h14
-rw-r--r--src/stdlib/mblen.c1
-rw-r--r--src/stdlib/mbtowc.c1
-rw-r--r--src/stdlib/wctomb.c1
5 files changed, 19 insertions, 0 deletions
diff --git a/src/locale/_locale.h b/src/locale/_locale.h
index b7867731..623ca4f3 100644
--- a/src/locale/_locale.h
+++ b/src/locale/_locale.h
@@ -92,6 +92,8 @@ struct __locale_t {
char * __load_locale(struct __locale_t *loc, int mask, const char *name);
struct __locale_t * __get_locale(void);
+#define __get_ctype_epoch() (__get_locale()->ctype_epoch)
+
/*
STDC(-1)
*/
diff --git a/src/stdlib/_stdlib.h b/src/stdlib/_stdlib.h
index 7edb9d98..d5f89b09 100644
--- a/src/stdlib/_stdlib.h
+++ b/src/stdlib/_stdlib.h
@@ -3,8 +3,22 @@
#include <stdlib.h>
#include <limits.h>
+#include "locale/_locale.h"
#include "_safety.h"
+#ifndef NDEBUG
+#define ASSERT_CTYPE(__s_ptr) do { \
+ static unsigned int __ctype_epoch = 0; \
+ if ((__s_ptr) == 0) { \
+ __ctype_epoch = __get_ctype_epoch(); \
+ } else if (__ctype_epoch != __get_ctype_epoch()) { \
+ UNDEFINED("LC_CTYPE has changed since previous call which did not reset internal state"); \
+ } \
+} while (0)
+#else
+#define ASSERT_CTYPE(__s_ptr) (void)(__s_ptr)
+#endif
+
#ifdef NEED_COMPAR
#ifdef NDEBUG
#define SAFE_COMPAR(__comp, __p1, __p2, __sz, __fn) __comp(__p1, __p2)
diff --git a/src/stdlib/mblen.c b/src/stdlib/mblen.c
index 42188418..57f64790 100644
--- a/src/stdlib/mblen.c
+++ b/src/stdlib/mblen.c
@@ -6,6 +6,7 @@
int mblen(const char * s, size_t n)
{
SIGNAL_SAFE(0);
+ ASSERT_CTYPE(s);
/* FIXME: forward dependency on AMD1 */
#if 0
diff --git a/src/stdlib/mbtowc.c b/src/stdlib/mbtowc.c
index c2780bf6..57ae03b9 100644
--- a/src/stdlib/mbtowc.c
+++ b/src/stdlib/mbtowc.c
@@ -7,6 +7,7 @@ int mbtowc(wchar_t * restrict pwc, const char * restrict s, size_t n)
{
SIGNAL_SAFE(0);
ASSERT_NOOVERLAP(pwc, sizeof(*pwc), s, n);
+ ASSERT_CTYPE(s);
/* FIXME: forward dependency on AMD1 */
#if 0
diff --git a/src/stdlib/wctomb.c b/src/stdlib/wctomb.c
index ce4c8e82..d1de2dd8 100644
--- a/src/stdlib/wctomb.c
+++ b/src/stdlib/wctomb.c
@@ -6,6 +6,7 @@
int wctomb(char * s, wchar_t wchar)
{
SIGNAL_SAFE(0);
+ ASSERT_CTYPE(s);
/* FIXME: forward dependency on AMD1 */
#if 0