public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
* [PATCH] reftable/iter: fix undefined behavior in indexed_table_ref_iter_next
@ 2026-01-02 19:16 Tsahi Elkayam
  2026-01-03  7:35 ` Pushkar Singh
  2026-01-04  2:49 ` Junio C Hamano
  0 siblings, 2 replies; 6+ messages in thread
From: Tsahi Elkayam @ 2026-01-02 19:16 UTC (permalink / raw)
  To: git@vger•kernel.org; +Cc: ps@pks•im



  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)

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-01-04 10:33 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-01-02 19:16 [PATCH] reftable/iter: fix undefined behavior in indexed_table_ref_iter_next Tsahi Elkayam
2026-01-03  7:35 ` Pushkar Singh
2026-01-04 10:13   ` Tsahi Elkayam
2026-01-04  2:49 ` Junio C Hamano
2026-01-04 10:22   ` Tsahi Elkayam
2026-01-04 10:33     ` Tsahi Elkayam

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox