- while (newp == NULL && size > 0 && n > 0) {
- (*xmalloc_error_handler)("reallocarray", n * size, file, line);
+ if (size == 0 || n == 0)
+ return newp;
+
+ /*
+ * GCC 13.2.0 (and some earlier versions) misdiagnose this error
+ * handling as a use-after-free of p, but the documentation of
+ * reallocarray guarantees that if reallocarray fails (which is true in
+ * every case when it returns NULL when size > 0 and n > 0), p is
+ * unchanged and still valid.
+ */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wuse-after-free"
+ while (newp == NULL) {
+ (*xmalloc_error_handler)("reallocarray", n *size, file, line);