gen_scanf_fmt(): delete function and use snprintf() instead

To replace "%.*s" with "%s", all we have to do is use snprintf() to interpolate "%s" into the pattern. Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Michael Haggerty committed Jan 8, 2014 at 15:43 UTC 4346663a14fe2af5e5cec94213203e199b7dfc3f
1 file changed +9 -26
refs.c
+9 -26
@@ -3334,29 +3334,6 @@ cleanup:
3334 return ret;
3335 }
3336
3337 -/*
3338 - * generate a format suitable for scanf from a ref_rev_parse_rules
3339 - * rule, that is replace the "%.*s" spec with a "%s" spec
3340 - */
3341 -static void gen_scanf_fmt(char *scanf_fmt, const char *rule)
3342 -{
3343 - char *spec;
3344 -
3345 - spec = strstr(rule, "%.*s");
3346 - if (!spec || strstr(spec + 4, "%.*s"))
3347 - die("invalid rule in ref_rev_parse_rules: %s", rule);
3348 -
3349 - /* copy all until spec */
3350 - strncpy(scanf_fmt, rule, spec - rule);
3351 - scanf_fmt[spec - rule] = '\0';
3352 - /* copy new spec */
3353 - strcat(scanf_fmt, "%s");
3354 - /* copy remaining rule */
3355 - strcat(scanf_fmt, spec + 4);
3356 -
3357 - return;
3358 -}
3359 -
3337 char *shorten_unambiguous_ref(const char *refname, int strict)
3338 {
3339 int i;
@@ -3364,8 +3341,13 @@ char *shorten_unambiguous_ref(const char *refname, int strict)
3341 static int nr_rules;
3342 char *short_name;
3343
3367 - /* pre generate scanf formats from ref_rev_parse_rules[] */
3344 if (!nr_rules) {
3345 + /*
3346 + * Pre-generate scanf formats from ref_rev_parse_rules[].
3347 + * Generate a format suitable for scanf from a
3348 + * ref_rev_parse_rules rule by interpolating "%s" at the
3349 + * location of the "%.*s".
3350 + */
3351 size_t total_len = 0;
3352 size_t offset = 0;
3353
@@ -3378,9 +3360,10 @@ char *shorten_unambiguous_ref(const char *refname, int strict)
3360
3361 offset = 0;
3362 for (i = 0; i < nr_rules; i++) {
3363 + assert(offset < total_len);
3364 scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset;
3382 - gen_scanf_fmt(scanf_fmts[i], ref_rev_parse_rules[i]);
3383 - offset += strlen(ref_rev_parse_rules[i]);
3365 + offset += snprintf(scanf_fmts[i], total_len - offset,
3366 + ref_rev_parse_rules[i], 2, "%s") + 1;
3367 }
3368 }
3369