Skip to content

Commit c971ddf

Browse files
René Scharfegitster
authored andcommitted
refs: use strncmp() instead of strlen() and memcmp()
Simplify ref_entry_cmp_sslice() by using strncmp() to compare the length-limited key and a NUL-terminated entry. While we're at it, retain the const attribute of the input pointers. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e1980c9 commit c971ddf

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

refs.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -322,14 +322,12 @@ struct string_slice {
322322

323323
static int ref_entry_cmp_sslice(const void *key_, const void *ent_)
324324
{
325-
struct string_slice *key = (struct string_slice *)key_;
326-
struct ref_entry *ent = *(struct ref_entry **)ent_;
327-
int entlen = strlen(ent->name);
328-
int cmplen = key->len < entlen ? key->len : entlen;
329-
int cmp = memcmp(key->str, ent->name, cmplen);
325+
const struct string_slice *key = key_;
326+
const struct ref_entry *ent = *(const struct ref_entry * const *)ent_;
327+
int cmp = strncmp(key->str, ent->name, key->len);
330328
if (cmp)
331329
return cmp;
332-
return key->len - entlen;
330+
return '\0' - (unsigned char)ent->name[key->len];
333331
}
334332

335333
/*

0 commit comments

Comments
 (0)