Check itr == end before dereferencing [PR127083]

Message ID 20260901122549.493550-1-j@lambda.is
State Committed
Commit 5da256175a9f7a5f82afcd475ac71eb5c065fac8
Headers
Series Check itr == end before dereferencing [PR127083] |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap fail Patch failed to apply
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap fail Patch failed to apply

Commit Message

Jørgen Kvalsvik Sept. 1, 2026, 12:25 p.m. UTC
  Guard the dereference in the case where the current cursor in the
needle isn't a tombstone in case we're at the end of the
haystack. Example pair:

  NEEDLE:   6 x x 4 6
  HAYSTACK: 4 6 x x 4

We find the subsequence 6 4, but the needle is not yet at end (missing
6):

  NEEDLE:   6 x x 4 6
                    ^
  HAYSTACK: 4 6 x x 4
                      ^

If the haystack (super) is exhausted before the needle (sub) we know
it isn't a proper subsequence.

This example was in the testsuite and caught by ASAN.

	PR gcov-profile/127038

gcc/ChangeLog:

	* gcov.cc (tombstone_subsequence_p): Guard iterator
	dereference.
---
 gcc/gcov.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
  

Comments

Richard Biener Sept. 2, 2026, 7:38 a.m. UTC | #1
On Tue, Sep 1, 2026 at 2:26 PM Jørgen Kvalsvik <j@lambda.is> wrote:
>
> Guard the dereference in the case where the current cursor in the
> needle isn't a tombstone in case we're at the end of the
> haystack. Example pair:
>
>   NEEDLE:   6 x x 4 6
>   HAYSTACK: 4 6 x x 4
>
> We find the subsequence 6 4, but the needle is not yet at end (missing
> 6):
>
>   NEEDLE:   6 x x 4 6
>                     ^
>   HAYSTACK: 4 6 x x 4
>                       ^
>
> If the haystack (super) is exhausted before the needle (sub) we know
> it isn't a proper subsequence.
>
> This example was in the testsuite and caught by ASAN.

OK.

>         PR gcov-profile/127038
>
> gcc/ChangeLog:
>
>         * gcov.cc (tombstone_subsequence_p): Guard iterator
>         dereference.
> ---
>  gcc/gcov.cc | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/gcc/gcov.cc b/gcc/gcov.cc
> index 87deeadcc75..0ad48eb1b54 100644
> --- a/gcc/gcov.cc
> +++ b/gcc/gcov.cc
> @@ -1015,7 +1015,7 @@ tombstone_subsequence_p (const vector<unsigned>& sub,
>         if (yitr == yend)
>           return false;
>        }
> -    else if (*yitr != *xitr)
> +    else if (yitr == yend || *yitr != *xitr)
>        return false;
>
>    yitr = find_if_not (yitr, yend, tombstone_p);
> --
> 2.47.3
>
  

Patch

diff --git a/gcc/gcov.cc b/gcc/gcov.cc
index 87deeadcc75..0ad48eb1b54 100644
--- a/gcc/gcov.cc
+++ b/gcc/gcov.cc
@@ -1015,7 +1015,7 @@  tombstone_subsequence_p (const vector<unsigned>& sub,
 	if (yitr == yend)
 	  return false;
       }
-    else if (*yitr != *xitr)
+    else if (yitr == yend || *yitr != *xitr)
       return false;
 
   yitr = find_if_not (yitr, yend, tombstone_p);