[PATCH v2] Make mark parsing much more restrictive

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Jörg Sommer
Date: Friday, April 25, 2008 - 2:04 am

The current implementation of mark parsing doesn't care for trailing
garbage like in :12a and doesn't check for unsigned numbers, i.e. it
accepts :-12 as a valid mark.

This patch enforces a number follows the colon and there comes nothing
after the bignum.

Signed-off-by: Jörg Sommer <joerg@alea.gnuu.de>
---
 fast-import.c |   49 ++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 38 insertions(+), 11 deletions(-)

"Shawn O. Pearce" <spearce@spearce.org> wrote:

Then I propose the following patch.

diff --git a/fast-import.c b/fast-import.c
index 73e5439..0c71da8 100644
--- a/fast-import.c
+++ b/fast-import.c
@@ -1690,12 +1690,31 @@ static void skip_optional_lf(void)
 		ungetc(term_char, stdin);
 }
 
+static inline uintmax_t parse_mark(const const char *str, char **after_mark)
+{
+	char *am;
+	uintmax_t m;
+
+	if (!str || str[0] != ':' || !isdigit(str[1]))
+		return 0;
+
+	m = strtoumax(&str[1], &am, 10);
+	if (m != UINTMAX_MAX || errno == 0) {
+		*after_mark = am;
+		return m;
+	}
+	return 0;
+}
+
 static void cmd_mark(void)
 {
-	if (!prefixcmp(command_buf.buf, "mark :")) {
-		next_mark = strtoumax(command_buf.buf + 6, NULL, 10);
+	uintmax_t mark;
+	char *after_mark = NULL;
+
+	if (!prefixcmp(command_buf.buf, "mark ") &&
+		(next_mark = parse_mark(&command_buf.buf[5], &after_mark)) &&
+		*after_mark == '\0')
 		read_next_command();
-	}
 	else
 		next_mark = 0;
 }
@@ -1877,8 +1896,8 @@ static void file_change_m(struct branch *b)
 	}
 
 	if (*p == ':') {
-		char *x;
-		oe = find_mark(strtoumax(p + 1, &x, 10));
+		char *x = NULL;
+		oe = find_mark(parse_mark(p, &x));
 		hashcpy(sha1, oe->sha1);
 		p = x;
 	} else if (!prefixcmp(p, "inline")) {
@@ -2045,7 +2064,10 @@ static int cmd_from(struct branch *b)
 		hashcpy(b->branch_tree.versions[0].sha1, t);
 		hashcpy(b->branch_tree.versions[1].sha1, t);
 	} else if (*from == ':') {
-		uintmax_t idnum = strtoumax(from + 1, NULL, 10);
+		char *after_mark;
+		uintmax_t idnum = parse_mark(from, &after_mark);
+		if (!idnum || *after_mark != '\0')
+			die("Not a valid mark: %s", from);
 		struct object_entry *oe = find_mark(idnum);
 		if (oe->type != OBJ_COMMIT)
 			die("Mark :%" PRIuMAX " not a commit", idnum);
@@ -2080,7 +2102,10 @@ static struct hash_list *cmd_merge(unsigned int *count)
 		if (s)
 			hashcpy(n->sha1, s->sha1);
 		else if (*from == ':') {
-			uintmax_t idnum = strtoumax(from + 1, NULL, 10);
+			char *after_mark;
+			uintmax_t idnum = parse_mark(from, &after_mark);
+			if (!idnum || *after_mark != '\0')
+				die("Not a valid mark: %s", from);
 			struct object_entry *oe = find_mark(idnum);
 			if (oe->type != OBJ_COMMIT)
 				die("Mark :%" PRIuMAX " not a commit", idnum);
@@ -2228,7 +2253,10 @@ static void cmd_new_tag(void)
 		hashcpy(sha1, s->sha1);
 	} else if (*from == ':') {
 		struct object_entry *oe;
-		from_mark = strtoumax(from + 1, NULL, 10);
+		char *after_mark;
+		from_mark = parse_mark(from, &after_mark);
+		if (!from_mark || *after_mark != '\0')
+			die("Not a valid mark: %s", from);
 		oe = find_mark(from_mark);
 		if (oe->type != OBJ_COMMIT)
 			die("Mark :%" PRIuMAX " not a commit", from_mark);
@@ -2333,9 +2361,8 @@ static void import_marks(const char *input_file)
 		if (line[0] != ':' || !end)
 			die("corrupt mark line: %s", line);
 		*end = 0;
-		mark = strtoumax(line + 1, &end, 10);
-		if (!mark || end == line + 1
-			|| *end != ' ' || get_sha1(end + 1, sha1))
+		mark = parse_mark(line, &end);
+		if (!mark || *end != ' ' || get_sha1(end + 1, sha1))
 			die("corrupt mark line: %s", line);
 		e = find_object(sha1);
 		if (!e) {
-- 
1.5.5.1

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 1/4] Move redo merge code in a function, Jörg Sommer, (Sun Mar 23, 2:42 pm)
[PATCH 2/4] Rework redo_merge, Jörg Sommer, (Sun Mar 23, 2:42 pm)
Re: [PATCH 1/4] Move redo merge code in a function, Johannes Schindelin, (Sun Mar 23, 3:26 pm)
Re: [PATCH 2/4] Rework redo_merge, Johannes Schindelin, (Sun Mar 23, 3:29 pm)
Re: [PATCH 3/4] Add a function for get the parents of a commit, Johannes Schindelin, (Sun Mar 23, 3:33 pm)
Re: [PATCH 4/4] git-rebase -i: New option to support rebas ..., Johannes Schindelin, (Sun Mar 23, 3:41 pm)
Re: [PATCH 4/4] git-rebase -i: New option to support rebas ..., Johannes Schindelin, (Mon Mar 24, 6:08 am)
[PATCH/RFC 04/10] Move redo merge code in a function, Jörg Sommer, (Wed Apr 9, 4:58 pm)
[PATCH/RFC 05/10] Rework redo_merge, Jörg Sommer, (Wed Apr 9, 4:58 pm)
[PATCH/RFC 09/10] Select all lines with fake-editor, Jörg Sommer, (Wed Apr 9, 4:58 pm)
[PATCH v2 06/13] Move redo merge code in a function, Jörg Sommer, (Sun Apr 13, 5:21 pm)
[PATCH v2 09/13] Select all lines with fake-editor, Jörg Sommer, (Sun Apr 13, 5:21 pm)
[PATCH v2 11/13] Add option --first-parent, Jörg Sommer, (Sun Apr 13, 5:21 pm)
[PATCH v2 13/13] Add option --preserve-tags, Jörg Sommer, (Sun Apr 13, 5:21 pm)
[PATCH v2.1] Teach rebase interactive the mark command, Jörg Sommer, (Mon Apr 14, 3:39 am)
Re: [PATCH v2.1] Teach rebase interactive the mark command, Shawn O. Pearce, (Mon Apr 14, 4:29 pm)
mark parsing in fast-import, Jörg, (Sun Apr 20, 4:44 pm)
Re: mark parsing in fast-import, Shawn O. Pearce, (Sun Apr 20, 5:26 pm)
Re: mark parsing in fast-import, Jörg, (Mon Apr 21, 1:41 am)
Re: mark parsing in fast-import, Shawn O. Pearce, (Mon Apr 21, 4:59 pm)
Re: [PATCH v2 04/13] Teach rebase interactive the mark command, Johannes Schindelin, (Tue Apr 22, 1:52 am)
Re: mark parsing in fast-import, Jörg, (Tue Apr 22, 2:39 am)
Re: [PATCH v2 04/13] Teach rebase interactive the mark command, Johannes Schindelin, (Tue Apr 22, 3:31 am)
Re: mark parsing in fast-import, Shawn O. Pearce, (Tue Apr 22, 4:15 pm)
[PATCH v2] Make mark parsing much more restrictive, Jörg Sommer, (Fri Apr 25, 2:04 am)
[PATCH v2.2] Teach rebase interactive the mark command, Jörg Sommer, (Fri Apr 25, 2:44 am)
Re: [PATCH v2.2] Teach rebase interactive the mark command, Junio C Hamano, (Sat Apr 26, 11:13 pm)