]> Savannah Git Hosting - gnulib.git/commitdiff
wcswidth: Fix result in case of overflow.
authorBruno Haible <bruno@clisp.org>
Thu, 4 May 2023 21:22:14 +0000 (23:22 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 5 May 2023 11:04:11 +0000 (13:04 +0200)
* lib/wcswidth-impl.h (wcswidth): Continue searching for a non-printing
wide character after the total width has become > INT_MAX.

ChangeLog
lib/wcswidth-impl.h

index ad0d96cb4de9b45d6c1d7270e4ae4b09c3927976..6a17a1e3bf309d0158ada3edc74b0dc0b39414fd 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2023-05-04  Bruno Haible  <bruno@clisp.org>
+
+       wcswidth: Fix result in case of overflow.
+       * lib/wcswidth-impl.h (wcswidth): Continue searching for a non-printing
+       wide character after the total width has become > INT_MAX.
+
 2023-05-03  Bruno Haible  <bruno@clisp.org>
 
        vasnprintf: Make '0' flag handling more ISO C compliant.
index e9da91a19e332bd2ed18bf5c68b4f5614b824908..ce0b2e08774bf6b05a28c64afb31e4edd9d8c30d 100644 (file)
@@ -1,5 +1,5 @@
 /* Determine number of screen columns needed for a size-bounded wide string.
-   Copyright (C) 1999, 2011-2022 Free Software Foundation, Inc.
+   Copyright (C) 1999, 2011-2023 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 1999.
 
    This file is free software: you can redistribute it and/or modify
@@ -35,9 +35,22 @@ wcswidth (const wchar_t *s, size_t n)
     }
   return count;
 
+  /* The total width has become > INT_MAX.
+     Continue searching for a non-printing wide character.  */
+  for (; n > 0; s++, n--)
+    {
+      wchar_t c = *s;
+      if (c == (wchar_t)'\0')
+        break;
+      {
+        int width = wcwidth (c);
+        if (width < 0)
+          goto found_nonprinting;
+      }
+     overflow: ;
+    }
+  return INT_MAX;
+
  found_nonprinting:
   return -1;
-
- overflow:
-  return INT_MAX;
 }