From 84d5633f986933d640d5dace561f46afe762d20f Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Wed, 8 Jan 2014 15:43:38 +0100 Subject: shorten_unambiguous_ref(): introduce a new local variable When filling the scanf_fmts array, use a separate variable to keep track of the offset to avoid clobbering total_len (which we will need in the next commit). Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- refs.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 5e5a3824b..7ac3043f8 100644 --- a/refs.c +++ b/refs.c @@ -3367,6 +3367,7 @@ char *shorten_unambiguous_ref(const char *refname, int strict) /* pre generate scanf formats from ref_rev_parse_rules[] */ if (!nr_rules) { 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++) @@ -3375,12 +3376,11 @@ char *shorten_unambiguous_ref(const char *refname, int strict) 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; + scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset; gen_scanf_fmt(scanf_fmts[i], ref_rev_parse_rules[i]); - total_len += strlen(ref_rev_parse_rules[i]); + offset += strlen(ref_rev_parse_rules[i]); } } -- cgit v1.2.1 From 4346663a14fe2af5e5cec94213203e199b7dfc3f Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Wed, 8 Jan 2014 15:43:39 +0100 Subject: 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 Signed-off-by: Junio C Hamano --- refs.c | 35 +++++++++-------------------------- 1 file changed, 9 insertions(+), 26 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 7ac3043f8..5e54af267 100644 --- a/refs.c +++ b/refs.c @@ -3334,29 +3334,6 @@ cleanup: 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; @@ -3364,8 +3341,13 @@ char *shorten_unambiguous_ref(const char *refname, int strict) 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; @@ -3378,9 +3360,10 @@ char *shorten_unambiguous_ref(const char *refname, int strict) offset = 0; for (i = 0; i < nr_rules; i++) { + assert(offset < total_len); scanf_fmts[i] = (char *)&scanf_fmts[nr_rules] + offset; - gen_scanf_fmt(scanf_fmts[i], ref_rev_parse_rules[i]); - offset += strlen(ref_rev_parse_rules[i]); + offset += snprintf(scanf_fmts[i], total_len - offset, + ref_rev_parse_rules[i], 2, "%s") + 1; } } -- cgit v1.2.1 From 7902fe03f97e2e74f95c96b8d18a7752bbb2ef6a Mon Sep 17 00:00:00 2001 From: Michael Haggerty Date: Wed, 8 Jan 2014 15:43:40 +0100 Subject: shorten_unambiguous_ref(): tighten up pointer arithmetic As long as we're being pathologically stingy with mallocs, we might as well do the math right and save 6 (!) bytes. Signed-off-by: Michael Haggerty Signed-off-by: Junio C Hamano --- refs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'refs.c') diff --git a/refs.c b/refs.c index 5e54af267..676bfd550 100644 --- a/refs.c +++ b/refs.c @@ -3353,8 +3353,8 @@ char *shorten_unambiguous_ref(const char *refname, int strict) /* 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); -- cgit v1.2.1