static int read_tree(git_index *index, const char *buffer, size_t buffer_size) { const char *buffer_end = buffer + buffer_size; index->tree = read_tree_internal(&buffer, buffer_end, NULL); return (index->tree != NULL && buffer == buffer_end) ? 0 : GIT_EOBJCORRUPTED; }
static int read_tree(git_index *index, const char *buffer, size_t buffer_size) { const char *buffer_end = buffer + buffer_size; int error; error = read_tree_internal(&index->tree, &buffer, buffer_end, NULL); if (buffer < buffer_end) return GIT_EOBJCORRUPTED; return error; }
int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer_size) { const char *buffer_end = buffer + buffer_size; if (read_tree_internal(tree, &buffer, buffer_end, NULL) < 0) return -1; if (buffer < buffer_end) { giterr_set(GITERR_INDEX, "Corrupted TREE extension in index (unexpected trailing data)"); return -1; } return 0; }
static git_index_tree *read_tree_internal( const char **buffer_in, const char *buffer_end, git_index_tree *parent) { git_index_tree *tree; const char *name_start, *buffer; if ((tree = git__malloc(sizeof(git_index_tree))) == NULL) return NULL; memset(tree, 0x0, sizeof(git_index_tree)); tree->parent = parent; buffer = name_start = *buffer_in; if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL) goto error_cleanup; /* NUL-terminated tree name */ tree->name = git__strdup(name_start); if (++buffer >= buffer_end) goto error_cleanup; /* Blank-terminated ASCII decimal number of entries in this tree */ tree->entries = strtol(buffer, (char **)&buffer, 10); if (*buffer != ' ' || ++buffer >= buffer_end) goto error_cleanup; /* Number of children of the tree, newline-terminated */ tree->children_count = strtol(buffer, (char **)&buffer, 10); if (*buffer != '\n' || ++buffer >= buffer_end) goto error_cleanup; /* 160-bit SHA-1 for this tree and it's children */ if (buffer + GIT_OID_RAWSZ > buffer_end) goto error_cleanup; git_oid_mkraw(&tree->oid, (const unsigned char *)buffer); buffer += GIT_OID_RAWSZ; /* Parse children: */ if (tree->children_count > 0) { unsigned int i; tree->children = git__malloc(tree->children_count * sizeof(git_index_tree *)); if (tree->children == NULL) goto error_cleanup; for (i = 0; i < tree->children_count; ++i) { tree->children[i] = read_tree_internal(&buffer, buffer_end, tree); if (tree->children[i] == NULL) goto error_cleanup; } } *buffer_in = buffer; return tree; error_cleanup: git_index_tree__free(tree); return NULL; }
static int read_tree_internal(git_tree_cache **out, const char **buffer_in, const char *buffer_end, git_tree_cache *parent) { git_tree_cache *tree = NULL; const char *name_start, *buffer; int count; size_t name_len; buffer = name_start = *buffer_in; if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL) goto corrupted; if (++buffer >= buffer_end) goto corrupted; name_len = strlen(name_start); tree = git__malloc(sizeof(git_tree_cache) + name_len + 1); GITERR_CHECK_ALLOC(tree); memset(tree, 0x0, sizeof(git_tree_cache)); tree->parent = parent; /* NUL-terminated tree name */ memcpy(tree->name, name_start, name_len); tree->name[name_len] = '\0'; /* Blank-terminated ASCII decimal number of entries in this tree */ if (git__strtol32(&count, buffer, &buffer, 10) < 0) goto corrupted; tree->entries = count; if (*buffer != ' ' || ++buffer >= buffer_end) goto corrupted; /* Number of children of the tree, newline-terminated */ if (git__strtol32(&count, buffer, &buffer, 10) < 0 || count < 0) goto corrupted; tree->children_count = count; if (*buffer != '\n' || ++buffer > buffer_end) goto corrupted; /* The SHA1 is only there if it's not invalidated */ if (tree->entries >= 0) { /* 160-bit SHA-1 for this tree and it's children */ if (buffer + GIT_OID_RAWSZ > buffer_end) goto corrupted; git_oid_fromraw(&tree->oid, (const unsigned char *)buffer); buffer += GIT_OID_RAWSZ; } /* Parse children: */ if (tree->children_count > 0) { unsigned int i; tree->children = git__malloc(tree->children_count * sizeof(git_tree_cache *)); GITERR_CHECK_ALLOC(tree->children); memset(tree->children, 0x0, tree->children_count * sizeof(git_tree_cache *)); for (i = 0; i < tree->children_count; ++i) { if (read_tree_internal(&tree->children[i], &buffer, buffer_end, tree) < 0) goto corrupted; } } *buffer_in = buffer; *out = tree; return 0; corrupted: git_tree_cache_free(tree); giterr_set(GITERR_INDEX, "Corrupted TREE extension in index"); return -1; }
static int read_tree_internal(git_index_tree **out, const char **buffer_in, const char *buffer_end, git_index_tree *parent) { git_index_tree *tree; const char *name_start, *buffer; long count; int error = GIT_SUCCESS; if ((tree = git__malloc(sizeof(git_index_tree))) == NULL) return GIT_ENOMEM; memset(tree, 0x0, sizeof(git_index_tree)); tree->parent = parent; buffer = name_start = *buffer_in; if ((buffer = memchr(buffer, '\0', buffer_end - buffer)) == NULL) { error = GIT_EOBJCORRUPTED; goto cleanup; } /* NUL-terminated tree name */ tree->name = git__strdup(name_start); if (tree->name == NULL) { error = GIT_ENOMEM; goto cleanup; } if (++buffer >= buffer_end) { error = GIT_EOBJCORRUPTED; goto cleanup; } /* Blank-terminated ASCII decimal number of entries in this tree */ if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS || count < -1) { error = GIT_EOBJCORRUPTED; goto cleanup; } /* Invalidated TREE. Free the tree but report success */ if (count == -1) { /* FIXME: return buffer_end or the end position for * this single tree entry */ *buffer_in = buffer_end; *out = NULL; free_tree(tree); /* Needs to be done manually */ return GIT_SUCCESS; } tree->entries = count; if (*buffer != ' ' || ++buffer >= buffer_end) { error = GIT_EOBJCORRUPTED; goto cleanup; } /* Number of children of the tree, newline-terminated */ if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS || count < 0) { error = GIT_EOBJCORRUPTED; goto cleanup; } tree->children_count = count; if (*buffer != '\n' || ++buffer >= buffer_end) { error = GIT_EOBJCORRUPTED; goto cleanup; } /* 160-bit SHA-1 for this tree and it's children */ if (buffer + GIT_OID_RAWSZ > buffer_end) { error = GIT_EOBJCORRUPTED; goto cleanup; } git_oid_fromraw(&tree->oid, (const unsigned char *)buffer); buffer += GIT_OID_RAWSZ; /* Parse children: */ if (tree->children_count > 0) { unsigned int i; int err; tree->children = git__malloc(tree->children_count * sizeof(git_index_tree *)); if (tree->children == NULL) goto cleanup; for (i = 0; i < tree->children_count; ++i) { err = read_tree_internal(&tree->children[i], &buffer, buffer_end, tree); if (err < GIT_SUCCESS) goto cleanup; } } *buffer_in = buffer; *out = tree; return GIT_SUCCESS; cleanup: free_tree(tree); return error; }