[RFC] newlib/libc/include/langinfo.h: nl_langinfo enum off by one error causing pointer overwrite

Brian Inglis <[email protected]>
Newsgroups gmane.comp.lib.newlib
Organization Systematic Software
Message-ID <d1a0b9da87e90420bbaf192ba01a141386f07966.1724283807.git.Brian.Inglis@SystematicSW.ab.ca>
if __HAVE_LOCALE_INFO__ is defined, then _NL_MESSAGES_CODESET is defined
instead of _NL_CTYPE_CODESET after _NL_CTYPE_MB_CUR_MAX, and
_NL_MESSAGES_CODESET is omitted before _NL_MESSAGES_WYESEXPR, resulting
in _NL_MESSAGES_WNOSTR pointer being overwritten by _NL_COLLATE_CODESET
pointer

demonstration of pointer overwrite by nl_langinfo dump program generated
with additional langinfo.h hack after fix:

    $ diff -4 /usr{/src/newlib-cygwin/newlib/libc,}/include/langinfo.h
    --- /usr/src/newlib-cygwin/newlib/libc/include/langinfo.h       2024-08-21 17:11:00.852069900 -0600
    +++ /usr/include/langinfo.h     2024-08-21 16:59:59.609998900 -0600
    @@ -301,9 +301,9 @@ enum
       _NL_MESSAGES_CODESET,
       _NL_MESSAGES_WYESEXPR,
       _NL_MESSAGES_WNOEXPR,
       _NL_MESSAGES_WYESSTR,
    -  _NL_MESSAGES_WNOSTR,
    +#define _NL_MESSAGES_WNOSTR _NL_MESSAGES_WYESSTR

       _NL_COLLATE_CODESET,

MESSAGES_CODESET                 UTF-8
MESSAGES_WYESEXPR                ^[+1yY]
MESSAGES_WNOEXPR                 ^[-0nN]
MESSAGES_WYESSTR                 yes
79 y 65 e 73 s 0 ^@ 6e n 6f o 0 ^@ 0 ^@	    # %hx %C dump
COLLATE_CODESET                  UTF-8

if __HAVE_LOCALE_INFO__ is not defined, then _NL_MESSAGES_CODESET is
also not defined, so it is unclear if _NL_MESSAGES_CODESET should be
defined to _NL_CTYPE_CODESET if neither __HAVE_LOCALE_INFO__ nor
__HAVE_LOCALE_INFO_EXTENDED__ are defined, or added as another field
depending on those definitions

Signed-off-by: Brian Inglis <[email protected]>
---
 newlib/libc/include/langinfo.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/newlib/libc/include/langinfo.h b/newlib/libc/include/langinfo.h
index 41d090d3710e..e383dc59d83f 100644
--- a/newlib/libc/include/langinfo.h
+++ b/newlib/libc/include/langinfo.h
@@ -179,7 +179,7 @@ enum
 
 #ifdef __HAVE_LOCALE_INFO__
   _NL_CTYPE_MB_CUR_MAX,
-  _NL_MESSAGES_CODESET,
+  _NL_CTYPE_CODESET,
 
 #ifdef __HAVE_LOCALE_INFO_EXTENDED__
 
@@ -298,6 +298,7 @@ enum
   _NL_MONETARY_WPOSITIVE_SIGN,
   _NL_MONETARY_WNEGATIVE_SIGN,
 
+  _NL_MESSAGES_CODESET,
   _NL_MESSAGES_WYESEXPR,
   _NL_MESSAGES_WNOEXPR,
   _NL_MESSAGES_WYESSTR,
-- 
2.45.1
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.