@matheusbsbs / matheus-bagliano / commits / 7f40759496
Fork 0

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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Jun 23, 2020 at 11:24 UTC 7f4075949686dcd01e364049350ac989c5fc2913
1 file changed +26 -27
builtin/fast-export.c
+26 -27
@@ -145,31 +145,30 @@ static int anonymized_entry_cmp(const void *unused_cmp_data,
145 * the same anonymized string with another. The actual generation
146 * is farmed out to the generate function.
147 */
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)
151 {
152 struct anonymized_entry key, *ret;
153
154 if (!map->cmpfn)
155 hashmap_init(map, anonymized_entry_cmp, NULL, 0);
156
157 - hashmap_entry_init(&key.hash, memhash(orig, *len));
157 + hashmap_entry_init(&key.hash, memhash(orig, len));
158 key.orig = orig;
159 - key.orig_len = *len;
159 + key.orig_len = len;
160 ret = hashmap_get_entry(map, &key, hash, NULL);
161
162 if (!ret) {
163 ret = xmalloc(sizeof(*ret));
164 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;
167 ret->anon = generate(orig, len);
168 - ret->anon_len = *len;
168 + ret->anon_len = strlen(ret->anon);
169 hashmap_put(map, &ret->hash);
170 }
171
172 - *len = ret->anon_len;
172 return ret->anon;
173 }
174
@@ -181,13 +180,13 @@ static const void *anonymize_mem(struct hashmap *map,
180 */
181 static void anonymize_path(struct strbuf *out, const char *path,
182 struct hashmap *map,
184 - void *(*generate)(const void *, size_t *))
183 + char *(*generate)(const char *, size_t))
184 {
185 while (*path) {
186 const char *end_of_component = strchrnul(path, '/');
187 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);
190 path = end_of_component;
191 if (*path)
192 strbuf_addch(out, *path++);
@@ -361,12 +360,12 @@ static void print_path_1(const char *path)
360 printf("%s", path);
361 }
362
364 -static void *anonymize_path_component(const void *path, size_t *len)
363 +static char *anonymize_path_component(const char *path, size_t len)
364 {
365 static int counter;
366 struct strbuf out = STRBUF_INIT;
367 strbuf_addf(&out, "path%d", counter++);
369 - return strbuf_detach(&out, len);
368 + return strbuf_detach(&out, NULL);
369 }
370
371 static void print_path(const char *path)
@@ -383,7 +382,7 @@ static void print_path(const char *path)
382 }
383 }
384
386 -static void *generate_fake_oid(const void *old, size_t *len)
385 +static char *generate_fake_oid(const char *old, size_t len)
386 {
387 static uint32_t counter = 1; /* avoid null oid */
388 const unsigned hashsz = the_hash_algo->rawsz;
@@ -399,7 +398,7 @@ static const char *anonymize_oid(const char *oid_hex)
398 {
399 static struct hashmap objs;
400 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);
402 }
403
404 static void show_filemodify(struct diff_queue_struct *q,
@@ -496,12 +495,12 @@ static const char *find_encoding(const char *begin, const char *end)
495 return bol;
496 }
497
499 -static void *anonymize_ref_component(const void *old, size_t *len)
498 +static char *anonymize_ref_component(const char *old, size_t len)
499 {
500 static int counter;
501 struct strbuf out = STRBUF_INIT;
502 strbuf_addf(&out, "ref%d", counter++);
504 - return strbuf_detach(&out, len);
503 + return strbuf_detach(&out, NULL);
504 }
505
506 static const char *anonymize_refname(const char *refname)
@@ -550,13 +549,13 @@ static char *anonymize_commit_message(const char *old)
549 }
550
551 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)
553 {
554 static int counter;
555 struct strbuf out = STRBUF_INIT;
556 strbuf_addf(&out, "User %d <user%d@example.com>", counter, counter);
557 counter++;
559 - return strbuf_detach(&out, len);
558 + return strbuf_detach(&out, NULL);
559 }
560
561 /*
@@ -591,9 +590,9 @@ static void anonymize_ident_line(const char **beg, const char **end)
590 size_t len;
591
592 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);
596 strbuf_addch(out, ' ');
597 strbuf_add(out, split.date_begin, split.tz_end - split.date_begin);
598 } else {
@@ -733,12 +732,12 @@ static void handle_commit(struct commit *commit, struct rev_info *rev,
732 show_progress();
733 }
734
736 -static void *anonymize_tag(const void *old, size_t *len)
735 +static char *anonymize_tag(const char *old, size_t len)
736 {
737 static int counter;
738 struct strbuf out = STRBUF_INIT;
739 strbuf_addf(&out, "tag message %d", counter++);
741 - return strbuf_detach(&out, len);
740 + return strbuf_detach(&out, NULL);
741 }
742
743 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)
807 name = anonymize_refname(name);
808 if (message) {
809 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);
812 }
813 }
814