I wrote:
>  static_assert ((size_t) (PTRDIFF_MAX - 1) * 2 < SIZE_MAX);

This assertion is not effective: the multiplication on the left-hand side
may overflow. This patch fixes it.


2026-10-04  Bruno Haible  <[email protected]>

        wcswidth, c32swidth, mbswidth: Improve yesterday's patch.
        * lib/wcswidth-impl.h (FUNC): Make assertion more effective.
        * lib/mbswidth.c (mbsnwidth): Likewise.

diff --git a/lib/mbswidth.c b/lib/mbswidth.c
index 5f809a23e3..4adfdae138 100644
--- a/lib/mbswidth.c
+++ b/lib/mbswidth.c
@@ -61,7 +61,7 @@ mbsnwidth (const char *string, size_t nbytes, int flags)
 {
   /* Since STRING consists of at most PTRDIFF_MAX - 1 bytes, the value of WIDTH
      stays <= (PTRDIFF_MAX - 1) * 2 < SIZE_MAX.  */
-  static_assert ((size_t) (PTRDIFF_MAX - 1) * 2 < SIZE_MAX);
+  static_assert (PTRDIFF_MAX - 1 < SIZE_MAX / 2);
 
   const char *p = string;
   const char *plimit = p + nbytes;
diff --git a/lib/wcswidth-impl.h b/lib/wcswidth-impl.h
index e8d18ede9d..c0b5738430 100644
--- a/lib/wcswidth-impl.h
+++ b/lib/wcswidth-impl.h
@@ -20,7 +20,7 @@ FUNC (const UNIT *s, size_t n)
 {
   /* Since S consists of at most PTRDIFF_MAX - 1 units, the value of COUNT
      stays <= (PTRDIFF_MAX - 1) * 2 < SIZE_MAX.  */
-  static_assert ((size_t) (PTRDIFF_MAX - 1) * 2 < SIZE_MAX);
+  static_assert (PTRDIFF_MAX - 1 < SIZE_MAX / 2);
 
   size_t count = 0;
   for (; n > 0; s++, n--)




Reply via email to