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

Skip to content

Commit 0174f21

Browse files
committed
tree: use a specialised mode parse function
Instead of going out to strtol, which is made to parse generic numbers, copy a parse function from git which is specialised for file modes.
1 parent 15e6a5a commit 0174f21

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

src/tree.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,25 @@ static int tree_error(const char *str, const char *path)
416416
return -1;
417417
}
418418

419+
static int parse_mode(unsigned int *modep, const char *buffer, const char **buffer_out)
420+
{
421+
unsigned char c;
422+
unsigned int mode = 0;
423+
424+
if (*buffer == ' ')
425+
return -1;
426+
427+
while ((c = *buffer++) != ' ') {
428+
if (c < '0' || c > '7')
429+
return -1;
430+
mode = (mode << 3) + (c - '0');
431+
}
432+
*modep = mode;
433+
*buffer_out = buffer;
434+
435+
return 0;
436+
}
437+
419438
int git_tree__parse(void *_tree, git_odb_object *odb_obj)
420439
{
421440
git_tree *tree = _tree;
@@ -430,14 +449,11 @@ int git_tree__parse(void *_tree, git_odb_object *odb_obj)
430449
git_tree_entry *entry;
431450
size_t filename_len;
432451
const char *nul;
433-
int attr;
452+
unsigned int attr;
434453

435-
if (git__strtol32(&attr, buffer, &buffer, 8) < 0 || !buffer)
454+
if (parse_mode(&attr, buffer, &buffer) < 0 || !buffer)
436455
return tree_error("Failed to parse tree. Can't parse filemode", NULL);
437456

438-
if (*buffer++ != ' ')
439-
return tree_error("Failed to parse tree. Object is corrupted", NULL);
440-
441457
if ((nul = memchr(buffer, 0, buffer_end - buffer)) == NULL)
442458
return tree_error("Failed to parse tree. Object is corrupted", NULL);
443459

0 commit comments

Comments
 (0)