Skip to content

Commit 7f40759

Browse files
peffgitster
authored andcommitted
fast-export: tighten anonymize_mem() interface to handle only strings
While the anonymize_mem() interface _can_ store arbitrary byte sequences, none of the callers uses this feature (as of the previous commit). We'd like to keep it that way, as we'll be exposing the string-like nature of the anonymization routines to the user. So let's tighten up the interface a bit: - don't treat "len" as an out-parameter from anonymize_mem(); this ensures callers treat the pointer result as a NUL-terminated string - likewise, don't treat "len" as an out-parameter from generator functions - swap out "void *" for "char *" as appropriate to signal that we don't handle arbitrary memory - rename the function to anonymize_str() This will also open up some optimization opportunities in a future patch. Note that we can't drop the "len" parameter entirely. Some callers do pass in partial strings (e.g., "foo/bar", len=3) to avoid copying, and we need to handle those still. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 750bb32 commit 7f40759

File tree

1 file changed

+26
-27
lines changed

1 file changed

+26
-27
lines changed

builtin/fast-export.c

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -145,31 +145,30 @@ static int anonymized_entry_cmp(const void *unused_cmp_data,
145145
* the same anonymized string with another. The actual generation
146146
* is farmed out to the generate function.
147147
*/
148-
static const void *anonymize_mem(struct hashmap *map,
149-
void *(*generate)(const void *, size_t *),
150-
const void *orig, size_t *len)
148+
static const char *anonymize_str(struct hashmap *map,
149+
char *(*generate)(const char *, size_t),
150+
const char *orig, size_t len)
151151
{
152152
struct anonymized_entry key, *ret;
153153

154154
if (!map->cmpfn)
155155
hashmap_init(map, anonymized_entry_cmp, NULL, 0);
156156

157-
hashmap_entry_init(&key.hash, memhash(orig, *len));
157+
hashmap_entry_init(&key.hash, memhash(orig, len));
158158
key.orig = orig;
159-
key.orig_len = *len;
159+
key.orig_len = len;
160160
ret = hashmap_get_entry(map, &key, hash, NULL);
161161

162162
if (!ret) {
163163
ret = xmalloc(sizeof(*ret));
164164
hashmap_entry_init(&ret->hash, key.hash.hash);
165-
ret->orig = xmemdupz(orig, *len);
166-
ret->orig_len = *len;
165+
ret->orig = xmemdupz(orig, len);
166+
ret->orig_len = len;
167167
ret->anon = generate(orig, len);
168-
ret->anon_len = *len;
168+
ret->anon_len = strlen(ret->anon);
169169
hashmap_put(map, &ret->hash);
170170
}
171171

172-
*len = ret->anon_len;
173172
return ret->anon;
174173
}
175174

@@ -181,13 +180,13 @@ static const void *anonymize_mem(struct hashmap *map,
181180
*/
182181
static void anonymize_path(struct strbuf *out, const char *path,
183182
struct hashmap *map,
184-
void *(*generate)(const void *, size_t *))
183+
char *(*generate)(const char *, size_t))
185184
{
186185
while (*path) {
187186
const char *end_of_component = strchrnul(path, '/');
188187
size_t len = end_of_component - path;
189-
const char *c = anonymize_mem(map, generate, path, &len);
190-
strbuf_add(out, c, len);
188+
const char *c = anonymize_str(map, generate, path, len);
189+
strbuf_addstr(out, c);
191190
path = end_of_component;
192191
if (*path)
193192
strbuf_addch(out, *path++);
@@ -361,12 +360,12 @@ static void print_path_1(const char *path)
361360
printf("%s", path);
362361
}
363362

364-
static void *anonymize_path_component(const void *path, size_t *len)
363+
static char *anonymize_path_component(const char *path, size_t len)
365364
{
366365
static int counter;
367366
struct strbuf out = STRBUF_INIT;
368367
strbuf_addf(&out, "path%d", counter++);
369-
return strbuf_detach(&out, len);
368+
return strbuf_detach(&out, NULL);
370369
}
371370

372371
static void print_path(const char *path)
@@ -383,7 +382,7 @@ static void print_path(const char *path)
383382
}
384383
}
385384

386-
static void *generate_fake_oid(const void *old, size_t *len)
385+
static char *generate_fake_oid(const char *old, size_t len)
387386
{
388387
static uint32_t counter = 1; /* avoid null oid */
389388
const unsigned hashsz = the_hash_algo->rawsz;
@@ -399,7 +398,7 @@ static const char *anonymize_oid(const char *oid_hex)
399398
{
400399
static struct hashmap objs;
401400
size_t len = strlen(oid_hex);
402-
return anonymize_mem(&objs, generate_fake_oid, oid_hex, &len);
401+
return anonymize_str(&objs, generate_fake_oid, oid_hex, len);
403402
}
404403

405404
static void show_filemodify(struct diff_queue_struct *q,
@@ -496,12 +495,12 @@ static const char *find_encoding(const char *begin, const char *end)
496495
return bol;
497496
}
498497

499-
static void *anonymize_ref_component(const void *old, size_t *len)
498+
static char *anonymize_ref_component(const char *old, size_t len)
500499
{
501500
static int counter;
502501
struct strbuf out = STRBUF_INIT;
503502
strbuf_addf(&out, "ref%d", counter++);
504-
return strbuf_detach(&out, len);
503+
return strbuf_detach(&out, NULL);
505504
}
506505

507506
static const char *anonymize_refname(const char *refname)
@@ -550,13 +549,13 @@ static char *anonymize_commit_message(const char *old)
550549
}
551550

552551
static struct hashmap idents;
553-
static void *anonymize_ident(const void *old, size_t *len)
552+
static char *anonymize_ident(const char *old, size_t len)
554553
{
555554
static int counter;
556555
struct strbuf out = STRBUF_INIT;
557556
strbuf_addf(&out, "User %d <user%[email protected]>", counter, counter);
558557
counter++;
559-
return strbuf_detach(&out, len);
558+
return strbuf_detach(&out, NULL);
560559
}
561560

562561
/*
@@ -591,9 +590,9 @@ static void anonymize_ident_line(const char **beg, const char **end)
591590
size_t len;
592591

593592
len = split.mail_end - split.name_begin;
594-
ident = anonymize_mem(&idents, anonymize_ident,
595-
split.name_begin, &len);
596-
strbuf_add(out, ident, len);
593+
ident = anonymize_str(&idents, anonymize_ident,
594+
split.name_begin, len);
595+
strbuf_addstr(out, ident);
597596
strbuf_addch(out, ' ');
598597
strbuf_add(out, split.date_begin, split.tz_end - split.date_begin);
599598
} else {
@@ -733,12 +732,12 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
733732
show_progress();
734733
}
735734

736-
static void *anonymize_tag(const void *old, size_t *len)
735+
static char *anonymize_tag(const char *old, size_t len)
737736
{
738737
static int counter;
739738
struct strbuf out = STRBUF_INIT;
740739
strbuf_addf(&out, "tag message %d", counter++);
741-
return strbuf_detach(&out, len);
740+
return strbuf_detach(&out, NULL);
742741
}
743742

744743
static void handle_tail(struct object_array *commits, struct rev_info *revs,
@@ -808,8 +807,8 @@ static void handle_tag(const char *name, struct tag *tag)
808807
name = anonymize_refname(name);
809808
if (message) {
810809
static struct hashmap tags;
811-
message = anonymize_mem(&tags, anonymize_tag,
812-
message, &message_size);
810+
message = anonymize_str(&tags, anonymize_tag,
811+
message, message_size);
813812
}
814813
}
815814

0 commit comments

Comments
 (0)