Skip to content

Commit d79db92

Browse files
committed
Merge branch 'jk/path-name-safety-2.7' into maint
* jk/path-name-safety-2.7: list-objects: pass full pathname to callbacks list-objects: drop name_path entirely list-objects: convert name_path to a strbuf show_object_with_name: simplify by using path_name() http-push: stop using name_path tree-diff: catch integer overflow in combine_diff_path allocation add helpers for detecting size_t overflow
2 parents 594730e + 55c45a7 commit d79db92

File tree

10 files changed

+46
-142
lines changed

10 files changed

+46
-142
lines changed

builtin/pack-objects.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2285,21 +2285,11 @@ static void show_commit(struct commit *commit, void *data)
22852285
index_commit_for_bitmap(commit);
22862286
}
22872287

2288-
static void show_object(struct object *obj,
2289-
const struct name_path *path, const char *last,
2290-
void *data)
2288+
static void show_object(struct object *obj, const char *name, void *data)
22912289
{
2292-
char *name = path_name(path, last);
2293-
22942290
add_preferred_base_object(name);
22952291
add_object_entry(obj->oid.hash, obj->type, name, 0);
22962292
obj->flags |= OBJECT_ADDED;
2297-
2298-
/*
2299-
* We will have generated the hash from the name,
2300-
* but not saved a pointer to it - we can free it
2301-
*/
2302-
free((char *)name);
23032293
}
23042294

23052295
static void show_edge(struct commit *commit)
@@ -2481,8 +2471,7 @@ static int get_object_list_from_bitmap(struct rev_info *revs)
24812471
}
24822472

24832473
static void record_recent_object(struct object *obj,
2484-
const struct name_path *path,
2485-
const char *last,
2474+
const char *name,
24862475
void *data)
24872476
{
24882477
sha1_array_append(&recent_objects, obj->oid.hash);

builtin/rev-list.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ static void finish_commit(struct commit *commit, void *data)
177177
free_commit_buffer(commit);
178178
}
179179

180-
static void finish_object(struct object *obj,
181-
const struct name_path *path, const char *name,
182-
void *cb_data)
180+
static void finish_object(struct object *obj, const char *name, void *cb_data)
183181
{
184182
struct rev_list_info *info = cb_data;
185183
if (obj->type == OBJ_BLOB && !has_object_file(&obj->oid))
@@ -188,15 +186,13 @@ static void finish_object(struct object *obj,
188186
parse_object(obj->oid.hash);
189187
}
190188

191-
static void show_object(struct object *obj,
192-
const struct name_path *path, const char *component,
193-
void *cb_data)
189+
static void show_object(struct object *obj, const char *name, void *cb_data)
194190
{
195191
struct rev_list_info *info = cb_data;
196-
finish_object(obj, path, component, cb_data);
192+
finish_object(obj, name, cb_data);
197193
if (info->flags & REV_LIST_QUIET)
198194
return;
199-
show_object_with_name(stdout, obj, path, component);
195+
show_object_with_name(stdout, obj, name);
200196
}
201197

202198
static void show_edge(struct commit *commit)

http-push.c

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,9 +1277,7 @@ static struct object_list **add_one_object(struct object *obj, struct object_lis
12771277
}
12781278

12791279
static struct object_list **process_blob(struct blob *blob,
1280-
struct object_list **p,
1281-
struct name_path *path,
1282-
const char *name)
1280+
struct object_list **p)
12831281
{
12841282
struct object *obj = &blob->object;
12851283

@@ -1293,14 +1291,11 @@ static struct object_list **process_blob(struct blob *blob,
12931291
}
12941292

12951293
static struct object_list **process_tree(struct tree *tree,
1296-
struct object_list **p,
1297-
struct name_path *path,
1298-
const char *name)
1294+
struct object_list **p)
12991295
{
13001296
struct object *obj = &tree->object;
13011297
struct tree_desc desc;
13021298
struct name_entry entry;
1303-
struct name_path me;
13041299

13051300
obj->flags |= LOCAL;
13061301

@@ -1310,21 +1305,17 @@ static struct object_list **process_tree(struct tree *tree,
13101305
die("bad tree object %s", oid_to_hex(&obj->oid));
13111306

13121307
obj->flags |= SEEN;
1313-
name = xstrdup(name);
13141308
p = add_one_object(obj, p);
1315-
me.up = path;
1316-
me.elem = name;
1317-
me.elem_len = strlen(name);
13181309

13191310
init_tree_desc(&desc, tree->buffer, tree->size);
13201311

13211312
while (tree_entry(&desc, &entry))
13221313
switch (object_type(entry.mode)) {
13231314
case OBJ_TREE:
1324-
p = process_tree(lookup_tree(entry.sha1), p, &me, name);
1315+
p = process_tree(lookup_tree(entry.sha1), p);
13251316
break;
13261317
case OBJ_BLOB:
1327-
p = process_blob(lookup_blob(entry.sha1), p, &me, name);
1318+
p = process_blob(lookup_blob(entry.sha1), p);
13281319
break;
13291320
default:
13301321
/* Subproject commit - not in this repository */
@@ -1343,7 +1334,7 @@ static int get_delta(struct rev_info *revs, struct remote_lock *lock)
13431334
int count = 0;
13441335

13451336
while ((commit = get_revision(revs)) != NULL) {
1346-
p = process_tree(commit->tree, p, NULL, "");
1337+
p = process_tree(commit->tree, p);
13471338
commit->object.flags |= LOCAL;
13481339
if (!(commit->object.flags & UNINTERESTING))
13491340
count += add_send_request(&commit->object, lock);
@@ -1362,11 +1353,11 @@ static int get_delta(struct rev_info *revs, struct remote_lock *lock)
13621353
continue;
13631354
}
13641355
if (obj->type == OBJ_TREE) {
1365-
p = process_tree((struct tree *)obj, p, NULL, name);
1356+
p = process_tree((struct tree *)obj, p);
13661357
continue;
13671358
}
13681359
if (obj->type == OBJ_BLOB) {
1369-
p = process_blob((struct blob *)obj, p, NULL, name);
1360+
p = process_blob((struct blob *)obj, p);
13701361
continue;
13711362
}
13721363
die("unknown pending object %s (%s)", oid_to_hex(&obj->oid), name);

list-objects.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@
1111
static void process_blob(struct rev_info *revs,
1212
struct blob *blob,
1313
show_object_fn show,
14-
struct name_path *path,
14+
struct strbuf *path,
1515
const char *name,
1616
void *cb_data)
1717
{
1818
struct object *obj = &blob->object;
19+
size_t pathlen;
1920

2021
if (!revs->blob_objects)
2122
return;
@@ -24,7 +25,11 @@ static void process_blob(struct rev_info *revs,
2425
if (obj->flags & (UNINTERESTING | SEEN))
2526
return;
2627
obj->flags |= SEEN;
27-
show(obj, path, name, cb_data);
28+
29+
pathlen = path->len;
30+
strbuf_addstr(path, name);
31+
show(obj, path->buf, cb_data);
32+
strbuf_setlen(path, pathlen);
2833
}
2934

3035
/*
@@ -52,7 +57,7 @@ static void process_blob(struct rev_info *revs,
5257
static void process_gitlink(struct rev_info *revs,
5358
const unsigned char *sha1,
5459
show_object_fn show,
55-
struct name_path *path,
60+
struct strbuf *path,
5661
const char *name,
5762
void *cb_data)
5863
{
@@ -62,15 +67,13 @@ static void process_gitlink(struct rev_info *revs,
6267
static void process_tree(struct rev_info *revs,
6368
struct tree *tree,
6469
show_object_fn show,
65-
struct name_path *path,
6670
struct strbuf *base,
6771
const char *name,
6872
void *cb_data)
6973
{
7074
struct object *obj = &tree->object;
7175
struct tree_desc desc;
7276
struct name_entry entry;
73-
struct name_path me;
7477
enum interesting match = revs->diffopt.pathspec.nr == 0 ?
7578
all_entries_interesting: entry_not_interesting;
7679
int baselen = base->len;
@@ -86,17 +89,12 @@ static void process_tree(struct rev_info *revs,
8689
return;
8790
die("bad tree object %s", oid_to_hex(&obj->oid));
8891
}
92+
8993
obj->flags |= SEEN;
90-
show(obj, path, name, cb_data);
91-
me.up = path;
92-
me.elem = name;
93-
me.elem_len = strlen(name);
94-
95-
if (!match) {
96-
strbuf_addstr(base, name);
97-
if (base->len)
98-
strbuf_addch(base, '/');
99-
}
94+
strbuf_addstr(base, name);
95+
show(obj, base->buf, cb_data);
96+
if (base->len)
97+
strbuf_addch(base, '/');
10098

10199
init_tree_desc(&desc, tree->buffer, tree->size);
102100

@@ -113,16 +111,16 @@ static void process_tree(struct rev_info *revs,
113111
if (S_ISDIR(entry.mode))
114112
process_tree(revs,
115113
lookup_tree(entry.sha1),
116-
show, &me, base, entry.path,
114+
show, base, entry.path,
117115
cb_data);
118116
else if (S_ISGITLINK(entry.mode))
119117
process_gitlink(revs, entry.sha1,
120-
show, &me, entry.path,
118+
show, base, entry.path,
121119
cb_data);
122120
else
123121
process_blob(revs,
124122
lookup_blob(entry.sha1),
125-
show, &me, entry.path,
123+
show, base, entry.path,
126124
cb_data);
127125
}
128126
strbuf_setlen(base, baselen);
@@ -213,19 +211,19 @@ void traverse_commit_list(struct rev_info *revs,
213211
continue;
214212
if (obj->type == OBJ_TAG) {
215213
obj->flags |= SEEN;
216-
show_object(obj, NULL, name, data);
214+
show_object(obj, name, data);
217215
continue;
218216
}
219217
if (!path)
220218
path = "";
221219
if (obj->type == OBJ_TREE) {
222220
process_tree(revs, (struct tree *)obj, show_object,
223-
NULL, &base, path, data);
221+
&base, path, data);
224222
continue;
225223
}
226224
if (obj->type == OBJ_BLOB) {
227225
process_blob(revs, (struct blob *)obj, show_object,
228-
NULL, path, data);
226+
&base, path, data);
229227
continue;
230228
}
231229
die("unknown pending object %s (%s)",

list-objects.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define LIST_OBJECTS_H
33

44
typedef void (*show_commit_fn)(struct commit *, void *);
5-
typedef void (*show_object_fn)(struct object *, const struct name_path *, const char *, void *);
5+
typedef void (*show_object_fn)(struct object *, const char *, void *);
66
void traverse_commit_list(struct rev_info *, show_commit_fn, show_object_fn, void *);
77

88
typedef void (*show_edge_fn)(struct commit *);

pack-bitmap-write.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,7 @@ static uint32_t find_object_pos(const unsigned char *sha1)
148148
return entry->in_pack_pos;
149149
}
150150

151-
static void show_object(struct object *object, const struct name_path *path,
152-
const char *last, void *data)
151+
static void show_object(struct object *object, const char *name, void *data)
153152
{
154153
struct bitmap *base = data;
155154
bitmap_set(base, find_object_pos(object->oid.hash));

pack-bitmap.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -417,19 +417,15 @@ static int ext_index_add_object(struct object *object, const char *name)
417417
return bitmap_pos + bitmap_git.pack->num_objects;
418418
}
419419

420-
static void show_object(struct object *object, const struct name_path *path,
421-
const char *last, void *data)
420+
static void show_object(struct object *object, const char *name, void *data)
422421
{
423422
struct bitmap *base = data;
424423
int bitmap_pos;
425424

426425
bitmap_pos = bitmap_position(object->oid.hash);
427426

428-
if (bitmap_pos < 0) {
429-
char *name = path_name(path, last);
427+
if (bitmap_pos < 0)
430428
bitmap_pos = ext_index_add_object(object, name);
431-
free(name);
432-
}
433429

434430
bitmap_set(base, bitmap_pos);
435431
}
@@ -897,9 +893,8 @@ struct bitmap_test_data {
897893
size_t seen;
898894
};
899895

900-
static void test_show_object(struct object *object,
901-
const struct name_path *path,
902-
const char *last, void *data)
896+
static void test_show_object(struct object *object, const char *name,
897+
void *data)
903898
{
904899
struct bitmap_test_data *tdata = data;
905900
int bitmap_pos;

reachable.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,14 @@ static int add_one_ref(const char *path, const struct object_id *oid,
4343
* The traversal will have already marked us as SEEN, so we
4444
* only need to handle any progress reporting here.
4545
*/
46-
static void mark_object(struct object *obj, const struct name_path *path,
47-
const char *name, void *data)
46+
static void mark_object(struct object *obj, const char *name, void *data)
4847
{
4948
update_progress(data);
5049
}
5150

5251
static void mark_commit(struct commit *c, void *data)
5352
{
54-
mark_object(&c->object, NULL, NULL, data);
53+
mark_object(&c->object, NULL, data);
5554
}
5655

5756
struct recent_data {

revision.c

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -25,69 +25,13 @@ volatile show_early_output_fn_t show_early_output;
2525
static const char *term_bad;
2626
static const char *term_good;
2727

28-
char *path_name(const struct name_path *path, const char *name)
29-
{
30-
const struct name_path *p;
31-
char *n, *m;
32-
int nlen = strlen(name);
33-
int len = nlen + 1;
34-
35-
for (p = path; p; p = p->up) {
36-
if (p->elem_len)
37-
len += p->elem_len + 1;
38-
}
39-
n = xmalloc(len);
40-
m = n + len - (nlen + 1);
41-
memcpy(m, name, nlen + 1);
42-
for (p = path; p; p = p->up) {
43-
if (p->elem_len) {
44-
m -= p->elem_len + 1;
45-
memcpy(m, p->elem, p->elem_len);
46-
m[p->elem_len] = '/';
47-
}
48-
}
49-
return n;
50-
}
51-
52-
static int show_path_component_truncated(FILE *out, const char *name, int len)
53-
{
54-
int cnt;
55-
for (cnt = 0; cnt < len; cnt++) {
56-
int ch = name[cnt];
57-
if (!ch || ch == '\n')
58-
return -1;
59-
fputc(ch, out);
60-
}
61-
return len;
62-
}
63-
64-
static int show_path_truncated(FILE *out, const struct name_path *path)
65-
{
66-
int emitted, ours;
67-
68-
if (!path)
69-
return 0;
70-
emitted = show_path_truncated(out, path->up);
71-
if (emitted < 0)
72-
return emitted;
73-
if (emitted)
74-
fputc('/', out);
75-
ours = show_path_component_truncated(out, path->elem, path->elem_len);
76-
if (ours < 0)
77-
return ours;
78-
return ours || emitted;
79-
}
80-
81-
void show_object_with_name(FILE *out, struct object *obj,
82-
const struct name_path *path, const char *component)
28+
void show_object_with_name(FILE *out, struct object *obj, const char *name)
8329
{
84-
struct name_path leaf;
85-
leaf.up = (struct name_path *)path;
86-
leaf.elem = component;
87-
leaf.elem_len = strlen(component);
30+
const char *p;
8831

8932
fprintf(out, "%s ", oid_to_hex(&obj->oid));
90-
show_path_truncated(out, &leaf);
33+
for (p = name; *p && *p != '\n'; p++)
34+
fputc(*p, out);
9135
fputc('\n', out);
9236
}
9337

0 commit comments

Comments
 (0)