Skip to content

Commit

Permalink
Merge branch 'mh/shorten-unambigous-ref'
Browse files Browse the repository at this point in the history
* mh/shorten-unambigous-ref:
  shorten_unambiguous_ref(): tighten up pointer arithmetic
  gen_scanf_fmt(): delete function and use snprintf() instead
  shorten_unambiguous_ref(): introduce a new local variable
  • Loading branch information
Junio C Hamano committed Jan 13, 2014
2 parents 272220f + 7902fe0 commit 540cc75
Showing 1 changed file with 14 additions and 31 deletions.
45 changes: 14 additions & 31 deletions refs.c
Original file line number Diff line number Diff line change
Expand Up @@ -3334,53 +3334,36 @@ int update_refs(const char *action, const struct ref_update **updates_orig,
return ret;
}

/*
* generate a format suitable for scanf from a ref_rev_parse_rules
* rule, that is replace the "%.*s" spec with a "%s" spec
*/
static void gen_scanf_fmt(char *scanf_fmt, const char *rule)
{
char *spec;

spec = strstr(rule, "%.*s");
if (!spec || strstr(spec + 4, "%.*s"))
die("invalid rule in ref_rev_parse_rules: %s", rule);

/* copy all until spec */
strncpy(scanf_fmt, rule, spec - rule);
scanf_fmt[spec - rule] = '\0';
/* copy new spec */
strcat(scanf_fmt, "%s");
/* copy remaining rule */
strcat(scanf_fmt, spec + 4);

return;
}

char *shorten_unambiguous_ref(const char *refname, int strict)
{
int i;
static char **scanf_fmts;
static int nr_rules;
char *short_name;

/* pre generate scanf formats from ref_rev_parse_rules[] */
if (!nr_rules) {
/*
* Pre-generate scanf formats from ref_rev_parse_rules[].
* Generate a format suitable for scanf from a
* ref_rev_parse_rules rule by interpolating "%s" at the
* location of the "%.*s".
*/
size_t total_len = 0;
size_t offset = 0;

/* the rule list is NULL terminated, count them first */
for (nr_rules = 0; ref_rev_parse_rules[nr_rules]; nr_rules++)
/* no +1 because strlen("%s") < strlen("%.*s") */
total_len += strlen(ref_rev_parse_rules[nr_rules]);
/* -2 for strlen("%.*s") - strlen("%s"); +1 for NUL */
total_len += strlen(ref_rev_parse_rules[nr_rules]) - 2 + 1;

scanf_fmts = xmalloc(nr_rules * sizeof(char *) + total_len);

total_len = 0;
offset = 0;
for (i = 0; i < nr_rules; i++) {
scanf_fmts[i] = (char *)&scanf_fmts[nr_rules]
+ total_len;
gen_scanf_fmt(scanf_fmts[i], ref_rev_parse_rules[i]);
total_len += strlen(ref_rev_parse_rules[i]);
assert(offset < total_len);
scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset;
offset += snprintf(scanf_fmts[i], total_len - offset,
ref_rev_parse_rules[i], 2, "%s") + 1;
}
}

Expand Down

0 comments on commit 540cc75

Please sign in to comment.