[PATCH 2/5] newlib: test reentrancy context argument forwarding

H. Peter Anvin hpa@zytor.com
Thu Dec 3 22:01:36 GMT 2026


Exercise __reent_ptr_t through direct and indirect calls, variadic
integer and floating-point arguments, stdio and conversion callbacks,
and allocation interfaces.

Check that non-TLS builds preserve independent explicit reentrancy
contexts. Cover the empty TLS representation once it is enabled by
subsequent patches.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
---
 newlib/testsuite/newlib.stdlib/reent_ptr.c | 72 ++++++++++++++++++++++
 1 file changed, 72 insertions(+)
 create mode 100644 newlib/testsuite/newlib.stdlib/reent_ptr.c

diff --git a/newlib/testsuite/newlib.stdlib/reent_ptr.c b/newlib/testsuite/newlib.stdlib/reent_ptr.c
new file mode 100644
index 000000000000..37d43035c27a
--- /dev/null
+++ b/newlib/testsuite/newlib.stdlib/reent_ptr.c
@@ -0,0 +1,72 @@
+#include <errno.h>
+#include <inttypes.h>
+#include <locale.h>
+#include <reent.h>
+#include <signal.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <wchar.h>
+
+static int
+forward (__reent_ptr_t reent, int value, ...)
+{
+  va_list ap;
+  int result;
+
+  va_start (ap, value);
+  result = value + va_arg (ap, int);
+  result += (int) va_arg (ap, double);
+  va_end (ap);
+  _REENT_ERRNO(reent) = result;
+  return result;
+}
+
+static int
+callback (__reent_ptr_t reent, FILE *file)
+{
+  return _fprintf_r (reent, file, "%s", "");
+}
+
+int
+main (void)
+{
+  __reent_ptr_t reent = _REENT;
+  int (*call) (__reent_ptr_t, int, ...) = forward;
+  int (*walk) (__reent_ptr_t, FILE *) = callback;
+  char buffer[32];
+  char *end;
+  void *allocation;
+  mbstate_t state = { 0 };
+  wchar_t wc;
+
+  if (call (reent, 10, 30, 2.0) != 42 || _REENT_ERRNO(reent) != 42)
+    abort ();
+  if (_strtol_r (reent, "123!", &end, 10) != 123 || *end != '!')
+    abort ();
+  if (_snprintf_r (reent, buffer, sizeof (buffer), "%d:%s", 42, "ok") != 5
+      || strcmp (buffer, "42:ok") != 0)
+    abort ();
+  if (_wctomb_r (reent, buffer, L'A', &state) != 1 || buffer[0] != 'A'
+      || _mbtowc_r (reent, &wc, "Z", 1, &state) != 1 || wc != L'Z')
+    abort ();
+  allocation = _malloc_r (reent, 16);
+  if (allocation == NULL)
+    abort ();
+  _free_r (reent, allocation);
+  if (walk (reent, stdout) != 0)
+    abort ();
+
+#ifndef _REENT_THREAD_LOCAL
+  {
+    struct _reent other = _REENT_INIT (other);
+    if (forward (&other, 10, 20, 0.0) != 30 || _REENT_ERRNO(&other) != 30
+	|| _REENT_ERRNO(reent) == 30)
+      abort ();
+    _reclaim_reent (&other);
+  }
+#endif
+
+  return 0;
+}
-- 
2.56.1



More information about the Newlib mailing list