From 2b455da03fb9de9af75fecf5792844a1de108899 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Tue, 17 Aug 2021 10:30:08 -0700 Subject: grep: avoid some size_t casts This helps move the code away from unsigned types. * src/grep.c (buf_has_encoding_errors, contains_encoding_error): * src/searchutils.c (mb_goback): Compare to MB_LEN_MAX, not to (size_t) -2. This is a bit safer anyway, as grep relies on MB_LEN_MAX limits elsewhere. * src/search.h (mb_clen): Compare to -2 before converting to size_t. --- src/searchutils.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/searchutils.c') diff --git a/src/searchutils.c b/src/searchutils.c index 80585118..b63990a9 100644 --- a/src/searchutils.c +++ b/src/searchutils.c @@ -68,8 +68,8 @@ kwsinit (bool mb_trans) When returning zero, set *MB_START to CUR. When returning a positive value, set *MB_START to the next boundary after CUR, - or to END if there is no such boundary, and set *MBCLEN to the - length of the preceding character. */ + or to END if there is no such boundary, and if MBCLEN is nonnull + set *MBCLEN to the length of the preceding character. */ ptrdiff_t mb_goback (char const **mb_start, size_t *mbclen, char const *cur, char const *end) @@ -92,7 +92,7 @@ mb_goback (char const **mb_start, size_t *mbclen, char const *cur, { mbstate_t mbs = { 0 }; clen = mb_clen (cur - i, end - (cur - i), &mbs); - if (i < clen && clen < (size_t) -2) + if (i < clen && clen <= MB_LEN_MAX) { p0 = cur - i; p = p0 + clen; @@ -107,7 +107,7 @@ mb_goback (char const **mb_start, size_t *mbclen, char const *cur, { clen = mb_clen (p, end - p, &mbs); - if ((size_t) -2 <= clen) + if (MB_LEN_MAX < clen) { /* An invalid sequence, or a truncated multibyte character. Treat it as a single byte character. */ -- cgit v1.2.1