]> git.ipfire.org Git - thirdparty/git.git/commitdiff
ref-cache: use 'size_t' instead of int for length
authorKarthik Nayak <karthik.188@gmail.com>
Mon, 28 Jul 2025 20:20:46 +0000 (22:20 +0200)
committerJunio C Hamano <gitster@pobox.com>
Mon, 28 Jul 2025 21:16:36 +0000 (14:16 -0700)
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 <karthik.188@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
refs/ref-cache.c

index ceef3a2008826771f410b06cf17523107cd9f3e5..c180e0aad74e87125ee78fe619826f4107fc55fd 100644 (file)
@@ -498,13 +498,14 @@ static int cache_ref_iterator_seek(struct ref_iterator *ref_iterator,
                 * indexing to each level as needed.
                 */
                do {
-                       int len, idx;
+                       int idx;
+                       size_t len;
                        int cmp = 0;
 
                        sort_ref_dir(dir);
 
                        slash = strchr(slash, '/');
-                       len = slash ? slash - refname : (int)strlen(refname);
+                       len = slash ? (size_t)(slash - refname) : strlen(refname);
 
                        for (idx = 0; idx < dir->nr; idx++) {
                                cmp = strncmp(refname, dir->entries[idx]->name, len);