diff options
author | Carlos Martín Nieto <cmn@dwim.me> | 2015-12-02 20:56:31 +0300 |
---|---|---|
committer | Carlos Martín Nieto <cmn@dwim.me> | 2015-12-02 20:59:58 +0300 |
commit | 0174f21b0ab1abb24496939d95c0cc84187aba00 (patch) | |
tree | def880c638721798492e79cb74926c6ce44dbb04 | |
parent | 15e6a5afb9217b09e60cd0aef48e0a7781f3922f (diff) |
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.
-rw-r--r-- | src/tree.c | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/src/tree.c b/src/tree.c index 0e3738afa..2de8e72e1 100644 --- a/src/tree.c +++ b/src/tree.c @@ -416,6 +416,25 @@ static int tree_error(const char *str, const char *path) return -1; } +static int parse_mode(unsigned int *modep, const char *buffer, const char **buffer_out) +{ + unsigned char c; + unsigned int mode = 0; + + if (*buffer == ' ') + return -1; + + while ((c = *buffer++) != ' ') { + if (c < '0' || c > '7') + return -1; + mode = (mode << 3) + (c - '0'); + } + *modep = mode; + *buffer_out = buffer; + + return 0; +} + int git_tree__parse(void *_tree, git_odb_object *odb_obj) { git_tree *tree = _tree; @@ -430,14 +449,11 @@ int git_tree__parse(void *_tree, git_odb_object *odb_obj) git_tree_entry *entry; size_t filename_len; const char *nul; - int attr; + unsigned int attr; - if (git__strtol32(&attr, buffer, &buffer, 8) < 0 || !buffer) + if (parse_mode(&attr, buffer, &buffer) < 0 || !buffer) return tree_error("Failed to parse tree. Can't parse filemode", NULL); - if (*buffer++ != ' ') - return tree_error("Failed to parse tree. Object is corrupted", NULL); - if ((nul = memchr(buffer, 0, buffer_end - buffer)) == NULL) return tree_error("Failed to parse tree. Object is corrupted", NULL); |