Skip to content

Commit 4346663

Browse files
mhaggergitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 84d5633 commit 4346663

File tree

1 file changed

+9
-26
lines changed

1 file changed

+9
-26
lines changed

refs.c

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3334,38 +3334,20 @@ int update_refs(const char *action, const struct ref_update **updates_orig,
33343334
return ret;
33353335
}
33363336

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-
33603337
char *shorten_unambiguous_ref(const char *refname, int strict)
33613338
{
33623339
int i;
33633340
static char **scanf_fmts;
33643341
static int nr_rules;
33653342
char *short_name;
33663343

3367-
/* pre generate scanf formats from ref_rev_parse_rules[] */
33683344
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+
*/
33693351
size_t total_len = 0;
33703352
size_t offset = 0;
33713353

@@ -3378,9 +3360,10 @@ char *shorten_unambiguous_ref(const char *refname, int strict)
33783360

33793361
offset = 0;
33803362
for (i = 0; i < nr_rules; i++) {
3363+
assert(offset < total_len);
33813364
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;
33843367
}
33853368
}
33863369

0 commit comments

Comments
 (0)