mirror of
https://github.com/git/git
synced 2024-08-27 11:39:22 +00:00
http-push: refactor parsing of remote object names
We get loose object names like "objects/??/..." from the remote side, and need to convert them to their hex representation. The code to do so is rather hard to follow, as it uses some calculated lengths whose origins are hard to understand and verify (e.g., the path must be exactly 49 characters long. why? Why doesn't the strcpy overflow obj_hex, which is the same length as path?). We can simplify this a bit by using skip_prefix, using standard 40- and 20-character buffers for hex and binary sha1s, and adding some comments. We also drop a totally bogus comment that claims strlcpy cannot be used because "path" is not NUL-terminated. Right between a call to strlen(path) and strcpy(path). Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
parent
59a642f8ac
commit
67a31f6128
38
http-push.c
38
http-push.c
|
@ -719,14 +719,10 @@ static int fetch_indices(void)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void one_remote_object(const char *hex)
|
static void one_remote_object(const unsigned char *sha1)
|
||||||
{
|
{
|
||||||
unsigned char sha1[20];
|
|
||||||
struct object *obj;
|
struct object *obj;
|
||||||
|
|
||||||
if (get_sha1_hex(hex, sha1) != 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
obj = lookup_object(sha1);
|
obj = lookup_object(sha1);
|
||||||
if (!obj)
|
if (!obj)
|
||||||
obj = parse_object(sha1);
|
obj = parse_object(sha1);
|
||||||
|
@ -1020,26 +1016,38 @@ static void remote_ls(const char *path, int flags,
|
||||||
void (*userFunc)(struct remote_ls_ctx *ls),
|
void (*userFunc)(struct remote_ls_ctx *ls),
|
||||||
void *userData);
|
void *userData);
|
||||||
|
|
||||||
|
/* extract hex from sharded "xx/x{40}" filename */
|
||||||
|
static int get_sha1_hex_from_objpath(const char *path, unsigned char *sha1)
|
||||||
|
{
|
||||||
|
char hex[40];
|
||||||
|
|
||||||
|
if (strlen(path) != 41)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
memcpy(hex, path, 2);
|
||||||
|
path += 2;
|
||||||
|
path++; /* skip '/' */
|
||||||
|
memcpy(hex, path, 38);
|
||||||
|
|
||||||
|
return get_sha1_hex(hex, sha1);
|
||||||
|
}
|
||||||
|
|
||||||
static void process_ls_object(struct remote_ls_ctx *ls)
|
static void process_ls_object(struct remote_ls_ctx *ls)
|
||||||
{
|
{
|
||||||
unsigned int *parent = (unsigned int *)ls->userData;
|
unsigned int *parent = (unsigned int *)ls->userData;
|
||||||
char *path = ls->dentry_name;
|
const char *path = ls->dentry_name;
|
||||||
char *obj_hex;
|
unsigned char sha1[20];
|
||||||
|
|
||||||
if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
|
if (!strcmp(ls->path, ls->dentry_name) && (ls->flags & IS_DIR)) {
|
||||||
remote_dir_exists[*parent] = 1;
|
remote_dir_exists[*parent] = 1;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strlen(path) != 49)
|
if (!skip_prefix(path, "objects/", &path) ||
|
||||||
|
get_sha1_hex_from_objpath(path, sha1))
|
||||||
return;
|
return;
|
||||||
path += 8;
|
|
||||||
obj_hex = xmalloc(strlen(path));
|
one_remote_object(sha1);
|
||||||
/* NB: path is not null-terminated, can not use strlcpy here */
|
|
||||||
memcpy(obj_hex, path, 2);
|
|
||||||
strcpy(obj_hex + 2, path + 3);
|
|
||||||
one_remote_object(obj_hex);
|
|
||||||
free(obj_hex);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void process_ls_ref(struct remote_ls_ctx *ls)
|
static void process_ls_ref(struct remote_ls_ctx *ls)
|
||||||
|
|
Loading…
Reference in a new issue