This is an automated email from Gerrit.

"Antonio Borneo <[email protected]>" just uploaded a new patch set to 
Gerrit, which you can find at https://review.openocd.org/c/openocd/+/9544

-- gerrit

commit 703e0daabc0d0d3d5f2d93cf83ab13cebae299b2
Author: Antonio Borneo <[email protected]>
Date:   Sun Mar 8 15:05:16 2026 +0100

    openocd: compile test: optionally use ctype.h from newlib
    
    This helps identifying wrong use of ctype functions, like isalpha().
    The ctype.h from newlib uses an array to implement the functions
    like isalpha(). The same file is used by MSYS2 and Cygwin.
    
    Use in OpenOCD a copy of ctype.h from newlib 4.6.0.
    Use it in place of the system ctype.h when the configure flag
    '--enable-newlib-ctype.h' is provided.
    
    Change-Id: Id126a92e4930f4f888b0606ea85fb72c880bba54
    Signed-off-by: Antonio Borneo <[email protected]>

diff --git a/HACKING b/HACKING
index 785179efe9..adfa411779 100644
--- a/HACKING
+++ b/HACKING
@@ -108,6 +108,24 @@ patch:
   # ... Open coverage_report/index.html in a web browser ...
   @endcode
 
+- Check for type mismatch of ctype functions
+
+  Several functions in ctype.h, like isalpha(), have special requirement for
+  the argument as reported in the manpage
+  @code
+  man isalpha
+  ...
+  NOTES
+    The standards require that the argument c for these functions is either
+     EOF or a value that is representable in the type unsigned char. If the
+     argument c is of type char, it must be cast to unsigned char, ...
+  @endcode
+  While GNU libc doesn't implements any compile-time test on the argument,
+  the way newlib implements ctype functions helps identifying the wrong use
+  at compile-time.
+  OpenOCD includes copy of newlib's ctype for compile-test only purpose, and
+  it can be enabled with configure flag '--enable-newlib-ctype.h'.
+
 Please consider performing these additional checks where appropriate
 (especially Clang Static Analyzer for big portions of new code) and
 mention the results (e.g. "Valgrind-clean, no new Clang analyzer
diff --git a/configure.ac b/configure.ac
index 39f5e074ed..fe45d03c68 100644
--- a/configure.ac
+++ b/configure.ac
@@ -218,7 +218,8 @@ m4_define([DUMMY_ADAPTER],
        [[[dummy], [Dummy Adapter], [DUMMY]]])
 
 m4_define([OPTIONAL_LIBRARIES],
-       [[[capstone], [Use Capstone disassembly framework], []]])
+       [[[capstone], [Use Capstone disassembly framework], []],
+       [[newlib_ctype_h], [Compile with ctype.h from newlib],[]]])
 
 m4_define([COVERAGE],
        [[[gcov], [Collect coverage using gcov], []]])
@@ -264,6 +265,16 @@ AS_IF([test "x$enable_gcov" = "xyes"], [
   AC_DEFINE([USE_GCOV], [0], [0 to leave coverage collection disabled.])
 ])
 
+AC_ARG_ENABLE([newlib-ctype.h],
+  AS_HELP_STRING([--enable-newlib-ctype.h], [Enable compile with ctype.h from 
newlib]),
+  [enable_newlib_ctype_h=$enableval], [enable_newlib_ctype_h=no])
+
+AS_IF([test "x$enable_newlib_ctype_h" = "xyes"], [
+  AC_DEFINE([USE_NEWLIB_CTYPE_H], [1], [1 to compile with ctype.h from newlib, 
instead of system file.]),
+], [
+  AC_DEFINE([USE_NEWLIB_CTYPE_H], [0], [0 to compile with ctype.h from system 
file, instead of from newlib.])
+])
+
 m4_define([AC_ARG_ADAPTERS], [
   m4_foreach([adapter_driver], [$1],
        [AC_ARG_ENABLE(ADAPTER_OPT([adapter_driver]),
diff --git a/src/helper/newlib_ctype.h b/src/helper/newlib_ctype.h
index b29594a455..3bef24db15 100644
--- a/src/helper/newlib_ctype.h
+++ b/src/helper/newlib_ctype.h
@@ -1,11 +1,47 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * File copied from newlib 4.6.0 with minimal changes from
+ * newlib/libc/include/ctype.h
+ *
+ * Used to verify the build with newlib ctype functions.
+ * It has several incompatibility with OpenOCD coding style, but
+ * these are not fixed to prevent alignment with future newlib.
+ */
+
+#ifndef OPENOCD_HELPER_NEWLIB_CTYPE_H
+#define OPENOCD_HELPER_NEWLIB_CTYPE_H
+
+#ifndef __ISO_C_VISIBLE
+#define __ISO_C_VISIBLE 1999
+#endif
+#ifndef __XSI_VISIBLE
+#define __XSI_VISIBLE 0
+#endif
+#ifndef __POSIX_VISIBLE
+#define __POSIX_VISIBLE 200809
+#endif
+#ifndef __MISC_VISIBLE
+#define __MISC_VISIBLE 1
+#endif
+#ifndef __IMPORT
+#define __IMPORT
+#endif
+#ifndef _BEGIN_STD_C
+#define _BEGIN_STD_C
+#endif
+#ifndef _END_STD_C
+#define _END_STD_C
+#endif
+
 #ifndef _CTYPE_H_
 #define _CTYPE_H_
 
-#include "_ansi.h"
+// #include "_ansi.h"
 #include <sys/cdefs.h>
 
 #if __POSIX_VISIBLE >= 200809 || __MISC_VISIBLE || defined (_LIBC)
-#include <sys/_locale.h>
+// #include <sys/_locale.h>
+#include <locale.h>
 #endif
 
 _BEGIN_STD_C
@@ -67,7 +103,8 @@ extern int toascii_l (int __c, locale_t __l);
 #define        _B      0200
 
 /* For C++ backward-compatibility only.  */
-extern __IMPORT const char     _ctype_[];
+// extern      __IMPORT const char     _ctype_[];
+extern __IMPORT const char     _ctype_[1 + 256];
 
 #ifdef __HAVE_LOCALE_INFO__
 const char *__locale_ctype_ptr (void);
@@ -181,3 +218,4 @@ __locale_ctype_ptr_l(locale_t _l)
 _END_STD_C
 
 #endif /* _CTYPE_H_ */
+#endif /* OPENOCD_HELPER_NEWLIB_CTYPE_H */
diff --git a/src/helper/system.h b/src/helper/system.h
index 60308abcd6..011a27447b 100644
--- a/src/helper/system.h
+++ b/src/helper/system.h
@@ -10,6 +10,12 @@
 #ifndef OPENOCD_HELPER_SYSTEM_H
 #define OPENOCD_HELPER_SYSTEM_H
 
+#if USE_NEWLIB_CTYPE_H
+#include <helper/newlib_ctype.h>
+#else
+#include <ctype.h>
+#endif
+
 /* +++ platform specific headers +++ */
 #ifdef _WIN32
 #include <winsock2.h>
@@ -24,7 +30,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <assert.h>
-#include <ctype.h>
 #include <errno.h>
 #include <time.h>
 

-- 

Reply via email to