From 783db67b0bf0ea3b10e842cbb210870a4947320e Mon Sep 17 00:00:00 2001 From: Laszlo Ersek Date: Sun, 11 Sep 2022 14:32:37 +0200 Subject: [PATCH] miniexpect.c: fix always-true comparison MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit According to : > STRING LENGTHS AND OFFSETS > > The PCRE2 API uses string lengths and offsets into strings of code units > in several places. These values are always of type PCRE2_SIZE, which is > an unsigned integer type, currently always defined as size_t. The > largest value that can be stored in such a type (that is ~(PCRE2_SIZE)0) > is reserved as a special indicator for zero-terminated strings and unset > offsets. Therefore, the longest string that can be handled is one less > than this maximum. Therefore check the validity of ovector[1] with ovector[1] != ~(PCRE2_SIZE)0 rather than the always-true ovector[1] >= 0 This was caught by gcc's > miniexpect.c:359:45: warning: comparison of unsigned expression in ‘>= > 0’ is always true [-Wtype-limits] which is part of "-Wextra". Signed-off-by: Laszlo Ersek --- miniexpect.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/miniexpect.c b/miniexpect.c index 77d781c..8533514 100644 --- a/miniexpect.c +++ b/miniexpect.c @@ -356,7 +356,7 @@ mexp_expect (mexp_h *h, const mexp_regexp *regexps, if (match_data) ovector = pcre2_get_ovector_pointer (match_data); - if (ovector != NULL && ovector[1] >= 0) + if (ovector != NULL && ovector[1] != ~(PCRE2_SIZE)0) h->next_match = ovector[1]; else h->next_match = -1; -- 1.8.3.1