From: Bruno Haible Date: Thu, 4 May 2023 21:22:14 +0000 (+0200) Subject: wcswidth: Fix result in case of overflow. X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=78b1f3957a068a18428d8109042521ea48de6c05;p=gnulib.git 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. --- diff --git a/ChangeLog b/ChangeLog index ad0d96cb4d..6a17a1e3bf 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2023-05-04 Bruno Haible + + 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 vasnprintf: Make '0' flag handling more ISO C compliant. diff --git a/lib/wcswidth-impl.h b/lib/wcswidth-impl.h index e9da91a19e..ce0b2e0877 100644 --- a/lib/wcswidth-impl.h +++ b/lib/wcswidth-impl.h @@ -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 , 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; }