# [PATCH] reftable/iter: fix undefined behavior in indexed_table_ref_iter_next

6 messages from 2026-01-02 to 2026-01-04. Participants: Tsahi Elkayam, Pushkar Singh, Junio C Hamano.
Thread: https://gitlist.dev/t/64713

## Tsahi Elkayam, 2026-01-02 19:16

Subject: [PATCH] reftable/iter: fix undefined behavior in indexed_table_ref_iter_next
Message-ID: <Q0zfHYp-_TO2h_5PXPG9KjHwpMKIf2o2u2dsaoAjIsScmA3W6t7IvqIEeLfM7auEFIQyazlNnA3MGAuS4AANF0yfEBJAjkU1bWp-NH9m89U=@protonmail.com>
URL: https://gitlist.dev/e/Q0zfHYp-_TO2h_5PXPG9KjHwpMKIf2o2u2dsaoAjIsScmA3W6t7IvqIEeLfM7auEFIQyazlNnA3MGAuS4AANF0yfEBJAjkU1bWp-NH9m89U%3D%40protonmail.com

```


  The indexed_table_ref_iter_next() function accesses ref->value.val2
  without first checking the ref's value_type. This is undefined behavior
  when the ref is not of type REFTABLE_REF_VAL2.

  The correct pattern is already used in filtering_ref_iterator_next()
  which checks value_type before accessing the appropriate union member.
  Apply the same pattern here:

   - Check for REFTABLE_REF_VAL2 before accessing val2 members
   - Add missing check for REFTABLE_REF_VAL1 to handle single-value refs

  This was marked with a "/* BUG */" comment indicating the issue was
  known but not yet fixed.

  Signed-off-by: Tsahi Elkayam <Tsahi.Elkayam@protonmail.com>
  ---
   reftable/iter.c | 13 ++++++++-----
   1 file changed, 8 insertions(+), 5 deletions(-)

  diff --git a/reftable/iter.c b/reftable/iter.c
  index 2ecc52b336..2eee65bb1e 100644
  --- a/reftable/iter.c
  +++ b/reftable/iter.c
  @@ -171,12 +171,15 @@ static int indexed_table_ref_iter_next(void *p, struct reftable_record *rec)
   			}
   			continue;
   		}
  -		/* BUG */
  -		if (!memcmp(it->oid.buf, ref->value.val2.target_value,
  -			    it->oid.len) ||
  -		    !memcmp(it->oid.buf, ref->value.val2.value, it->oid.len)) {
  +		if (ref->value_type == REFTABLE_REF_VAL2 &&
  +		    (!memcmp(it->oid.buf, ref->value.val2.target_value,
  +			     it->oid.len) ||
  +		     !memcmp(it->oid.buf, ref->value.val2.value, it->oid.len)))
  +			return 0;
  +
  +		if (ref->value_type == REFTABLE_REF_VAL1 &&
  +		    !memcmp(it->oid.buf, ref->value.val1, it->oid.len))
   			return 0;
  -		}
   	}
   }

  -- 
  2.37.1 (Apple Git-137.1)

```

## Pushkar Singh, 2026-01-03 07:35

Subject: Re: [PATCH] reftable/iter: fix undefined behavior in indexed_table_ref_iter_next
Message-ID: <CALE2CrQTvHeu21yLXtRg=A6ak9AB_vvwPirQNFDjZ2AmhoTzTQ@mail.gmail.com>
URL: https://gitlist.dev/e/CALE2CrQTvHeu21yLXtRg%3DA6ak9AB_vvwPirQNFDjZ2AmhoTzTQ%40mail.gmail.com
In-Reply-To: <Q0zfHYp-_TO2h_5PXPG9KjHwpMKIf2o2u2dsaoAjIsScmA3W6t7IvqIEeLfM7auEFIQyazlNnA3MGAuS4AANF0yfEBJAjkU1bWp-NH9m89U=@protonmail.com>

```
Hi Tsahi,

Thanks for working on this.

The issue and fix make sense to me. Guarding access to the val2 members
behind a value_type check avoids the undefined behavior noted by the
existing comment, and explicitly handling REFTABLE_REF_VAL1 here matches
the pattern already used in filtering_ref_iterator_next().

I didn’t spot any issues with the control flow or logic in this change.

Thanks for addressing this.

Pushkar

On Sat, Jan 3, 2026 at 12:47 AM Tsahi Elkayam
<Tsahi.Elkayam@protonmail.com> wrote:
>
>
>
>   The indexed_table_ref_iter_next() function accesses ref->value.val2
>   without first checking the ref's value_type. This is undefined behavior
>   when the ref is not of type REFTABLE_REF_VAL2.
>
>   The correct pattern is already used in filtering_ref_iterator_next()
>   which checks value_type before accessing the appropriate union member.
>   Apply the same pattern here:
>
>    - Check for REFTABLE_REF_VAL2 before accessing val2 members
>    - Add missing check for REFTABLE_REF_VAL1 to handle single-value refs
>
>   This was marked with a "/* BUG */" comment indicating the issue was
>   known but not yet fixed.
>
>   Signed-off-by: Tsahi Elkayam <Tsahi.Elkayam@protonmail.com>
>   ---
>    reftable/iter.c | 13 ++++++++-----
>    1 file changed, 8 insertions(+), 5 deletions(-)
>
>   diff --git a/reftable/iter.c b/reftable/iter.c
>   index 2ecc52b336..2eee65bb1e 100644
>   --- a/reftable/iter.c
>   +++ b/reftable/iter.c
>   @@ -171,12 +171,15 @@ static int indexed_table_ref_iter_next(void *p, struct reftable_record *rec)
>                         }
>                         continue;
>                 }
>   -             /* BUG */
>   -             if (!memcmp(it->oid.buf, ref->value.val2.target_value,
>   -                         it->oid.len) ||
>   -                 !memcmp(it->oid.buf, ref->value.val2.value, it->oid.len)) {
>   +             if (ref->value_type == REFTABLE_REF_VAL2 &&
>   +                 (!memcmp(it->oid.buf, ref->value.val2.target_value,
>   +                          it->oid.len) ||
>   +                  !memcmp(it->oid.buf, ref->value.val2.value, it->oid.len)))
>   +                     return 0;
>   +
>   +             if (ref->value_type == REFTABLE_REF_VAL1 &&
>   +                 !memcmp(it->oid.buf, ref->value.val1, it->oid.len))
>                         return 0;
>   -             }
>         }
>    }
>
>   --
>   2.37.1 (Apple Git-137.1)
>

```

## Junio C Hamano, 2026-01-04 02:49

Subject: Re: [PATCH] reftable/iter: fix undefined behavior in indexed_table_ref_iter_next
Message-ID: <xmqqy0menlc3.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqy0menlc3.fsf%40gitster.g
In-Reply-To: <Q0zfHYp-_TO2h_5PXPG9KjHwpMKIf2o2u2dsaoAjIsScmA3W6t7IvqIEeLfM7auEFIQyazlNnA3MGAuS4AANF0yfEBJAjkU1bWp-NH9m89U=@protonmail.com>

```
Tsahi Elkayam <Tsahi.Elkayam@protonmail.com> writes:

>   The indexed_table_ref_iter_next() function accesses ref->value.val2
>   without first checking the ref's value_type. This is undefined behavior
>   when the ref is not of type REFTABLE_REF_VAL2.
>
>   The correct pattern is already used in filtering_ref_iterator_next()
>   which checks value_type before accessing the appropriate union member.
>   Apply the same pattern here:
>
>    - Check for REFTABLE_REF_VAL2 before accessing val2 members
>    - Add missing check for REFTABLE_REF_VAL1 to handle single-value refs
>
>   This was marked with a "/* BUG */" comment indicating the issue was
>   known but not yet fixed.
>
>   Signed-off-by: Tsahi Elkayam <Tsahi.Elkayam@protonmail.com>
>   ---
>    reftable/iter.c | 13 ++++++++-----
>    1 file changed, 8 insertions(+), 5 deletions(-)
>
>   diff --git a/reftable/iter.c b/reftable/iter.c
>   index 2ecc52b336..2eee65bb1e 100644
>   --- a/reftable/iter.c
>   +++ b/reftable/iter.c

What are these lines with two-whitespace indent about?  When sending
a patch purely for discussion (because the actual change may be iffy
or dangerous), we sometimes deliberately corrupt the patch not to
apply mechanically, but this patch does not seem to be such a
"request for discussion" patch.


```

## Tsahi Elkayam, 2026-01-04 10:13

Subject: Re: [PATCH] reftable/iter: fix undefined behavior in indexed_table_ref_iter_next
Message-ID: <vKH4wrIZEyr9jZkXQad8wEowMcZq4fewU0z92Fdvv06lwH3e-pDFrvqjvehUFnmxO48-VGbwBP-ZzFubTlQEoIMeruyhqGBdRLmyU61nZ-k=@protonmail.com>
URL: https://gitlist.dev/e/vKH4wrIZEyr9jZkXQad8wEowMcZq4fewU0z92Fdvv06lwH3e-pDFrvqjvehUFnmxO48-VGbwBP-ZzFubTlQEoIMeruyhqGBdRLmyU61nZ-k%3D%40protonmail.com
In-Reply-To: <CALE2CrQTvHeu21yLXtRg=A6ak9AB_vvwPirQNFDjZ2AmhoTzTQ@mail.gmail.com>

```
This is my first contribution attempt I am so happy I didn’t messed it up
I found few more issues I will address them one by one
You made my day thank you


Sent from Proton Mail for iOS.

-------- Original Message --------
On Saturday, 01/03/26 at 09:35 Pushkar Singh <pushkarkumarsingh1970@gmail.com> wrote:
Hi Tsahi,

Thanks for working on this.

The issue and fix make sense to me. Guarding access to the val2 members
behind a value_type check avoids the undefined behavior noted by the
existing comment, and explicitly handling REFTABLE_REF_VAL1 here matches
the pattern already used in filtering_ref_iterator_next().

I didn’t spot any issues with the control flow or logic in this change.

Thanks for addressing this.

Pushkar

On Sat, Jan 3, 2026 at 12:47 AM Tsahi Elkayam
<Tsahi.Elkayam@protonmail.com> wrote:
>
>
>
>   The indexed_table_ref_iter_next() function accesses ref->value.val2
>   without first checking the ref's value_type. This is undefined behavior
>   when the ref is not of type REFTABLE_REF_VAL2.
>
>   The correct pattern is already used in filtering_ref_iterator_next()
>   which checks value_type before accessing the appropriate union member.
>   Apply the same pattern here:
>
>    - Check for REFTABLE_REF_VAL2 before accessing val2 members
>    - Add missing check for REFTABLE_REF_VAL1 to handle single-value refs
>
>   This was marked with a "/* BUG */" comment indicating the issue was
>   known but not yet fixed.
>
>   Signed-off-by: Tsahi Elkayam <Tsahi.Elkayam@protonmail.com>
>   ---
>    reftable/iter.c | 13 ++++++++-----
>    1 file changed, 8 insertions(+), 5 deletions(-)
>
>   diff --git a/reftable/iter.c b/reftable/iter.c
>   index 2ecc52b336..2eee65bb1e 100644
>   --- a/reftable/iter.c
>   +++ b/reftable/iter.c
>   @@ -171,12 +171,15 @@ static int indexed_table_ref_iter_next(void *p, struct reftable_record *rec)
>                         }
>                         continue;
>                 }
>   -             /* BUG */
>   -             if (!memcmp(it->oid.buf, ref->value.val2.target_value,
>   -                         it->oid.len) ||
>   -                 !memcmp(it->oid.buf, ref->value.val2.value, it->oid.len)) {
>   +             if (ref->value_type == REFTABLE_REF_VAL2 &&
>   +                 (!memcmp(it->oid.buf, ref->value.val2.target_value,
>   +                          it->oid.len) ||
>   +                  !memcmp(it->oid.buf, ref->value.val2.value, it->oid.len)))
>   +                     return 0;
>   +
>   +             if (ref->value_type == REFTABLE_REF_VAL1 &&
>   +                 !memcmp(it->oid.buf, ref->value.val1, it->oid.len))
>                         return 0;
>   -             }
>         }
>    }
>
>   --
>   2.37.1 (Apple Git-137.1)
>


```

## Tsahi Elkayam, 2026-01-04 10:22

Subject: Re: [PATCH] reftable/iter: fix undefined behavior in indexed_table_ref_iter_next
Message-ID: <W6v12kkhI_qyFV03jZJriHkrs5pYt8tHCD4ve0bWxikUYAGasvwwWDV3Df67dM2ttRh49EECD_Ph84NbtpxE1Opv-Z03UcE7vtbMjsYhfWs=@protonmail.com>
URL: https://gitlist.dev/e/W6v12kkhI_qyFV03jZJriHkrs5pYt8tHCD4ve0bWxikUYAGasvwwWDV3Df67dM2ttRh49EECD_Ph84NbtpxE1Opv-Z03UcE7vtbMjsYhfWs%3D%40protonmail.com
In-Reply-To: <xmqqy0menlc3.fsf@gitster.g>

```
So I did messed up
sorry lesson learned
but still...
wow very exciting 


Sent from Proton Mail for iOS.

-------- Original Message --------
On Sunday, 01/04/26 at 04:49 Junio C Hamano <gitster@pobox.com> wrote:
Tsahi Elkayam <Tsahi.Elkayam@protonmail.com> writes:

>   The indexed_table_ref_iter_next() function accesses ref->value.val2
>   without first checking the ref's value_type. This is undefined behavior
>   when the ref is not of type REFTABLE_REF_VAL2.
>
>   The correct pattern is already used in filtering_ref_iterator_next()
>   which checks value_type before accessing the appropriate union member.
>   Apply the same pattern here:
>
>    - Check for REFTABLE_REF_VAL2 before accessing val2 members
>    - Add missing check for REFTABLE_REF_VAL1 to handle single-value refs
>
>   This was marked with a "/* BUG */" comment indicating the issue was
>   known but not yet fixed.
>
>   Signed-off-by: Tsahi Elkayam <Tsahi.Elkayam@protonmail.com>
>   ---
>    reftable/iter.c | 13 ++++++++-----
>    1 file changed, 8 insertions(+), 5 deletions(-)
>
>   diff --git a/reftable/iter.c b/reftable/iter.c
>   index 2ecc52b336..2eee65bb1e 100644
>   --- a/reftable/iter.c
>   +++ b/reftable/iter.c

What are these lines with two-whitespace indent about?  When sending
a patch purely for discussion (because the actual change may be iffy
or dangerous), we sometimes deliberately corrupt the patch not to
apply mechanically, but this patch does not seem to be such a
"request for discussion" patch.



```

## Tsahi Elkayam, 2026-01-04 10:33

Subject: Re: [PATCH] reftable/iter: fix undefined behavior in indexed_table_ref_iter_next
Message-ID: <aHbWlZCRqQoHJ3jf12sxAyC7dEuzxCJ82PCtxH1RLcE23XSa7n8jl3yyoJ382TZ45H4pTuewB4WR72yL_zVzadotQ8UIVOBXpIkld45Ieew=@protonmail.com>
URL: https://gitlist.dev/e/aHbWlZCRqQoHJ3jf12sxAyC7dEuzxCJ82PCtxH1RLcE23XSa7n8jl3yyoJ382TZ45H4pTuewB4WR72yL_zVzadotQ8UIVOBXpIkld45Ieew%3D%40protonmail.com
In-Reply-To: <W6v12kkhI_qyFV03jZJriHkrs5pYt8tHCD4ve0bWxikUYAGasvwwWDV3Df67dM2ttRh49EECD_Ph84NbtpxE1Opv-Z03UcE7vtbMjsYhfWs=@protonmail.com>

```
Hi Junio,

Thank you very much for your feedback. It is a great honor for me to receive a response from you.

I apologize for the formatting issues in my previous email. I am a new developer and still learning the community's workflow. English is not my native language, and I mistakenly added indentation when composing the email, which I now realize corrupted the patch.

I will send a corrected Version 2 (v2) of the patch shortly, ensuring that the format is preserved correctly.

Thank you for your patience and for the "lesson learned."

Best regards, Tsahi




Sent with Proton Mail secure email.

On Sunday, January 4th, 2026 at 12:22 PM, Tsahi Elkayam <Tsahi.Elkayam@protonmail.com> wrote:

> 
> 
> So I did messed up
> sorry lesson learned
> but still...
> wow very exciting
> 
> 
> Sent from Proton Mail for iOS.
> 
> -------- Original Message --------
> On Sunday, 01/04/26 at 04:49 Junio C Hamano gitster@pobox.com wrote:
> 
> Tsahi Elkayam Tsahi.Elkayam@protonmail.com writes:
> 
> > The indexed_table_ref_iter_next() function accesses ref->value.val2
> > without first checking the ref's value_type. This is undefined behavior
> > when the ref is not of type REFTABLE_REF_VAL2.
> > 
> > The correct pattern is already used in filtering_ref_iterator_next()
> > which checks value_type before accessing the appropriate union member.
> > Apply the same pattern here:
> > 
> > - Check for REFTABLE_REF_VAL2 before accessing val2 members
> > - Add missing check for REFTABLE_REF_VAL1 to handle single-value refs
> > 
> > This was marked with a "/* BUG */" comment indicating the issue was
> > known but not yet fixed.
> > 
> > Signed-off-by: Tsahi Elkayam Tsahi.Elkayam@protonmail.com
> > ---
> > reftable/iter.c | 13 ++++++++-----
> > 1 file changed, 8 insertions(+), 5 deletions(-)
> > 
> > diff --git a/reftable/iter.c b/reftable/iter.c
> > index 2ecc52b336..2eee65bb1e 100644
> > --- a/reftable/iter.c
> > +++ b/reftable/iter.c
> 
> 
> What are these lines with two-whitespace indent about? When sending
> a patch purely for discussion (because the actual change may be iffy
> or dangerous), we sometimes deliberately corrupt the patch not to
> apply mechanically, but this patch does not seem to be such a
> "request for discussion" patch.

```
