On 12/7/21 09:38, Robbie Harwood wrote:

My*guess*  is that Coverity has noticed that `mctx->state_log` is
checked against NULL in many other places in that file, and was unable
to prove to itself that it couldn't be NULL there too.  If that's the
case, a DEBUG_ASSERT would presumably do the trick better.

Yes, I can see why Coverity can't deduce the code is safe.

I installed the attached patch into Gnulib; it adds a DEBUG_ASSERT which should be a reasonable prophylactic even if we don't use Coverity. I hope this also suffices to pacify Coverity.

I put the DEBUG_ASSERT in a different place, since we shouldn't need to worry about the assertion unless top < next_state_log_idx.
From 12df33fed758c4cb71b8ae38b973657ca941faec Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Tue, 7 Dec 2021 14:34:21 -0800
Subject: [PATCH] regex: pacify Coverity clean_state_log_if_needed

Problem reported by Robbie Harwood in:
https://lists.gnu.org/r/bug-gnulib/2021-12/msg00005.html
* lib/regexec.c (clean_state_log_if_needed):
Add a DEBUG_ASSERT; this both pacifies Coverity and
will help to debug in case some other change mistakenly
causes the assertion to become false.
---
 ChangeLog     | 10 ++++++++++
 lib/regexec.c |  1 +
 2 files changed, 11 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index d2e96eb7f7..bb3edbe44a 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2021-12-07  Paul Eggert  <egg...@cs.ucla.edu>
+
+	regex: pacify Coverity clean_state_log_if_needed
+	Problem reported by Robbie Harwood in:
+	https://lists.gnu.org/r/bug-gnulib/2021-12/msg00005.html
+	* lib/regexec.c (clean_state_log_if_needed):
+	Add a DEBUG_ASSERT; this both pacifies Coverity and
+	will help to debug in case some other change mistakenly
+	causes the assertion to become false.
+
 2021-12-07  Bruno Haible  <br...@clisp.org>
 
 	gettext-h: Optimize also for clang.
diff --git a/lib/regexec.c b/lib/regexec.c
index 085bf27b09..3196708373 100644
--- a/lib/regexec.c
+++ b/lib/regexec.c
@@ -1670,6 +1670,7 @@ clean_state_log_if_needed (re_match_context_t *mctx, Idx next_state_log_idx)
 
   if (top < next_state_log_idx)
     {
+      DEBUG_ASSERT (mctx->state_log != NULL);
       memset (mctx->state_log + top + 1, '\0',
 	      sizeof (re_dfastate_t *) * (next_state_log_idx - top));
       mctx->state_log_top = next_state_log_idx;
-- 
2.33.1

Reply via email to