git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 2/3] gen_scanf_fmt(): delete function and use snprintf() instead

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Jan 8, 2014, 14:43 UTC
Message-ID
<1389192220-13913-3-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1389192220-13913-1-git-send-email-mhagger@alum.mit.edu>

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>
---
 refs.c | 35 +++++++++--------------------------
 1 file changed, 9 insertions(+), 26 deletions(-)
diff --git a/refs.c b/refs.c
index 9530480..ef9cdea 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;
 		}
 	}
 
-- 
1.8.5.2
Previous: Michael HaggertyNext: Junio C Hamano
Message 3 of 11 in “Generate scanf_fmts more simply”
  1. 0/3 Generate scanf_fmts more simplyMichael Haggerty, Jan 8, 2014
  2. 1/3 shorten_unambiguous_ref(): introduce a new local variableMichael Haggerty, Jan 8, 2014
  3. 2/3 gen_scanf_fmt(): delete function and use snprintf() insteadMichael Haggerty, Jan 8, 2014
  4. Junio C HamanoJan 9, 2014
  5. 3/3 shorten_unambiguous_ref(): tighten up pointer arithmeticMichael Haggerty, Jan 8, 2014
  6. Junio C HamanoJan 9, 2014
  7. Michael HaggertyJan 10, 2014
  8. Junio C HamanoJan 10, 2014
  9. refname_match(): always use the rules in ref_rev_parse_rulesMichael Haggerty, Jan 14, 2014
  10. Junio C HamanoJan 14, 2014
  11. Michael HaggertyJan 15, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.