Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aadad40

Browse files
author
Edward Thomson
committed
tree: zap warnings around size_t vs uint16_t
1 parent 35439f5 commit aadad40

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

src/tree.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#define DEFAULT_TREE_SIZE 16
1818
#define MAX_FILEMODE_BYTES 6
1919

20+
#define TREE_ENTRY_CHECK_NAMELEN(n) \
21+
if (n > UINT16_MAX) { giterr_set(GITERR_INVALID, "tree entry path too long"); }
22+
2023
GIT__USE_STRMAP
2124

2225
static bool valid_filemode(const int filemode)
@@ -89,10 +92,7 @@ static git_tree_entry *alloc_entry_base(git_pool *pool, const char *filename, si
8992
git_tree_entry *entry = NULL;
9093
size_t tree_len;
9194

92-
if (filename_len > UINT16_MAX) {
93-
giterr_set(GITERR_INVALID, "tree entry is over UINT16_MAX in length");
94-
return NULL;
95-
}
95+
TREE_ENTRY_CHECK_NAMELEN(filename_len);
9696

9797
if (GIT_ADD_SIZET_OVERFLOW(&tree_len, sizeof(git_tree_entry), filename_len) ||
9898
GIT_ADD_SIZET_OVERFLOW(&tree_len, tree_len, 1))
@@ -106,7 +106,7 @@ static git_tree_entry *alloc_entry_base(git_pool *pool, const char *filename, si
106106
memset(entry, 0x0, sizeof(git_tree_entry));
107107
memcpy(entry->filename, filename, filename_len);
108108
entry->filename[filename_len] = 0;
109-
entry->filename_len = filename_len;
109+
entry->filename_len = (uint16_t)filename_len;
110110

111111
return entry;
112112
}
@@ -143,8 +143,8 @@ static int homing_search_cmp(const void *key, const void *array_member)
143143
const struct tree_key_search *ksearch = key;
144144
const git_tree_entry *entry = array_member;
145145

146-
const size_t len1 = ksearch->filename_len;
147-
const size_t len2 = entry->filename_len;
146+
const uint16_t len1 = ksearch->filename_len;
147+
const uint16_t len2 = entry->filename_len;
148148

149149
return memcmp(
150150
ksearch->filename,
@@ -180,8 +180,10 @@ static int tree_key_search(
180180
const git_tree_entry *entry;
181181
size_t homing, i;
182182

183+
TREE_ENTRY_CHECK_NAMELEN(filename_len);
184+
183185
ksearch.filename = filename;
184-
ksearch.filename_len = filename_len;
186+
ksearch.filename_len = (uint16_t)filename_len;
185187

186188
/* Initial homing search; find an entry on the tree with
187189
* the same prefix as the filename we're looking for */
@@ -334,6 +336,7 @@ const git_tree_entry *git_tree_entry_byname(
334336
const git_tree *tree, const char *filename)
335337
{
336338
assert(tree && filename);
339+
337340
return entry_fromname(tree, filename, strlen(filename));
338341
}
339342

@@ -364,13 +367,16 @@ int git_tree__prefix_position(const git_tree *tree, const char *path)
364367
{
365368
const git_vector *entries = &tree->entries;
366369
struct tree_key_search ksearch;
367-
size_t at_pos;
370+
size_t at_pos, path_len;
368371

369372
if (!path)
370373
return 0;
371374

375+
path_len = strlen(path);
376+
TREE_ENTRY_CHECK_NAMELEN(path_len);
377+
372378
ksearch.filename = path;
373-
ksearch.filename_len = strlen(path);
379+
ksearch.filename_len = (uint16_t)path_len;
374380

375381
/* be safe when we cast away constness - i.e. don't trigger a sort */
376382
assert(git_vector_is_sorted(&tree->entries));

0 commit comments

Comments
 (0)