From 0a0ca17c3d6a77fc36f96189ccff9be12af66e63 Mon Sep 17 00:00:00 2001 From: Jakob Kaivo Date: Tue, 4 Jun 2024 13:41:06 -0400 Subject: track LC_CTYPE epoch in character conversion functions to detect a change --- src/locale/_locale.h | 2 ++ src/stdlib/_stdlib.h | 14 ++++++++++++++ src/stdlib/mblen.c | 1 + src/stdlib/mbtowc.c | 1 + src/stdlib/wctomb.c | 1 + 5 files changed, 19 insertions(+) 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 #include +#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 -- cgit v1.2.1