Skip to content

Commit 6585575

Browse files
KarthikNayakgitster
authored andcommitted
ref-cache: use 'size_t' instead of int for length
The commit 090eb5336c (refs: selectively set prefix in the seek functions, 2025-07-15) modified the ref-cache iterator to support seeking to a specified marker without setting the prefix. The commit adds and uses an integer 'len' to capture the length of the seek marker to compare with the entries of a given directory. Since the type of the variable is 'int', this is met with a typecast of converting a `strlen` to 'int' so it can be assigned to the 'len' variable. This is whole operation is a bit wrong: 1. Since the 'len' variable is eventually used in a 'strncmp', it should have been of type 'size_t'. 2. This also truncates the value provided from 'strlen' to an int, which could cause a large refname to produce a negative number. Let's do the correct thing here and simply use 'size_t' for `len`. Signed-off-by: Karthik Nayak <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 9201261 commit 6585575

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

refs/ref-cache.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,13 +498,14 @@ static int cache_ref_iterator_seek(struct ref_iterator *ref_iterator,
498498
* indexing to each level as needed.
499499
*/
500500
do {
501-
int len, idx;
501+
int idx;
502+
size_t len;
502503
int cmp = 0;
503504

504505
sort_ref_dir(dir);
505506

506507
slash = strchr(slash, '/');
507-
len = slash ? slash - refname : (int)strlen(refname);
508+
len = slash ? (size_t)(slash - refname) : strlen(refname);
508509

509510
for (idx = 0; idx < dir->nr; idx++) {
510511
cmp = strncmp(refname, dir->entries[idx]->name, len);

0 commit comments

Comments
 (0)