Skip to contentSkip to conversation

builtin/commit.c: convert trivial snprintf calls to xsnprintf

PATCH In review Elia Pinto 3 participants 4 messages Dec 13, 2016

lore
Share & tools
lore

Elia Pinto

author
#0 lore raw

PATCH 1/1 builtin/commit.c: convert trivial snprintf calls to xsnprintf

With the commits f2f02675 and 5096d490 we have been converted in some files the call from snprintf/sprintf/strcpy to xsnprintf. This patch supersedes the snprintf with several calls that make use of the heap rather than fixed
length buffers (eg. PATH_MAX) that may be incorrect on some systems.

---
 builtin/commit.c | 32 +++++++++++++++-----------------
 1 file changed, 15 insertions(+), 17 deletions(-)
builtin/commit.c +15 -17
index 0ed634b26..37228330c 100644
@@ -960,15 +960,16 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		return 0;
 
 	if (use_editor) {
-		char index[PATH_MAX];
-		const char *env[2] = { NULL };
-		env[0] =  index;
-		snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
-		if (launch_editor(git_path_commit_editmsg(), NULL, env)) {
+		struct argv_array env = ARGV_ARRAY_INIT;
+
+		argv_array_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
+		if (launch_editor(git_path_commit_editmsg(), NULL, env.argv)) {
 			fprintf(stderr,
 			_("Please supply the message using either -m or -F option.\n"));
+			argv_array_clear(&env);
 			exit(1);
 		}
+		argv_array_clear(&env);
 	}
 
 	if (!no_verify &&
@@ -1525,12 +1526,10 @@ static int git_commit_config(const char *k, const char *v, void *cb)
 static int run_rewrite_hook(const unsigned char *oldsha1,
 			    const unsigned char *newsha1)
 {
-	/* oldsha1 SP newsha1 LF NUL */
-	static char buf[2*40 + 3];
+	char *buf;
 	struct child_process proc = CHILD_PROCESS_INIT;
 	const char *argv[3];
 	int code;
-	size_t n;
 
 	argv[0] = find_hook("post-rewrite");
 	if (!argv[0])
@@ -1546,34 +1545,33 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
 	code = start_command(&proc);
 	if (code)
 		return code;
-	n = snprintf(buf, sizeof(buf), "%s %s\n",
-		     sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
+	buf = xstrfmt("%s %s\n", sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
 	sigchain_push(SIGPIPE, SIG_IGN);
-	write_in_full(proc.in, buf, n);
+	write_in_full(proc.in, buf, strlen(buf));
 	close(proc.in);
+	free(buf);
 	sigchain_pop(SIGPIPE);
 	return finish_command(&proc);
 }
 
 int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...)
 {
-	const char *hook_env[3] =  { NULL };
-	char index[PATH_MAX];
+	struct argv_array hook_env = ARGV_ARRAY_INIT;
 	va_list args;
 	int ret;
 
-	snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
-	hook_env[0] = index;
+	argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
 
 	/*
 	 * Let the hook know that no editor will be launched.
 	 */
 	if (!editor_is_used)
-		hook_env[1] = "GIT_EDITOR=:";
+		argv_array_push(&hook_env, "GIT_EDITOR=:");
 
 	va_start(args, name);
-	ret = run_hook_ve(hook_env, name, args);
+	ret = run_hook_ve(hook_env.argv,name, args);
 	va_end(args);
+	argv_array_clear(&hook_env);
 
 	return ret;
 }
Signature · 1 line
2.11.0.153.gacc9cba
#1 lore raw
Quoted 3 lines · Elia Pinto

With the commits f2f02675 and 5096d490 we have been converted in some files the call from snprintf/sprintf/strcpy to xsnprintf. This patch supersedes the snprintf with several calls that make use of the heap rather than fixed
length buffers (eg. PATH_MAX) that may be incorrect on some systems.

I had trouble parsing this, because it leads with mention of commits that _aren't_ doing the same thing we are doing here. I think the argument you want to make is basically:

1. snprintf is bad because it may silently truncate results if we're wrong.

2. In cases where we use PATH_MAX, we'd want to handle larger paths anyway, so we should switch to dynamic allocation.

3. In other cases where we know the input is bounded to a certain length we could use xsnprintf(), which asserts that we don't truncate. But by switching to dynamic allocation, we can avoid dealing with magic numbers in the code.

I'd actually consider splitting cases (2) and (3) into separate patches. Even though the end result is the same, the reasoning is quite different.

As far as the patch itself goes, they all look like improvements to me. The extra allocations shouldn't matter because these are all heavy operations already.

A few minor nits:

Quoted 13 lines
builtin/commit.c +0 -5
index 0ed634b26..37228330c 100644
@@ -960,15 +960,16 @@ static int prepare_to_commit(const char *index_file, const char *prefix,
 		return 0;
 
 	if (use_editor) {
-		char index[PATH_MAX];
-		const char *env[2] = { NULL };
-		env[0] =  index;
-		snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
-		if (launch_editor(git_path_commit_editmsg(), NULL, env)) {

builtin/commit.c static int prepare_to_commit(const char *index_file, const char *prefix, · line 972

Quoted 10 lines
builtin/commit.c +6 -0
+		struct argv_array env = ARGV_ARRAY_INIT;
+
+		argv_array_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
+		if (launch_editor(git_path_commit_editmsg(), NULL, env.argv)) {
 			fprintf(stderr,
 			_("Please supply the message using either -m or -F option.\n"));
+			argv_array_clear(&env);
 			exit(1);
 		}
+		argv_array_clear(&env);

I'd generally skip the clear() right before exiting, though I saw somebody disagree with me recently on that. I wonder if we should decide as a project on whether it is worth doing or not.

Quoted 16 lines
builtin/commit.c +1 -3
@@ -1525,12 +1526,10 @@ static int git_commit_config(const char *k, const char *v, void *cb)
 static int run_rewrite_hook(const unsigned char *oldsha1,
 			    const unsigned char *newsha1)
 {
-	/* oldsha1 SP newsha1 LF NUL */
-	static char buf[2*40 + 3];
+	char *buf;
 	struct child_process proc = CHILD_PROCESS_INIT;
 	const char *argv[3];
 	int code;
-	size_t n;
 
 	argv[0] = find_hook("post-rewrite");
 	if (!argv[0])
@@ -1546,34 +1545,33 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
 	code = start_command(&proc);

builtin/commit.c static int run_rewrite_hook(const unsigned char *oldsha1, · line 1552

Quoted 10 lines
builtin/commit.c +3 -3
 	if (code)
 		return code;
-	n = snprintf(buf, sizeof(buf), "%s %s\n",
-		     sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
+	buf = xstrfmt("%s %s\n", sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
 	sigchain_push(SIGPIPE, SIG_IGN);
-	write_in_full(proc.in, buf, n);
+	write_in_full(proc.in, buf, strlen(buf));
 	close(proc.in);
+	free(buf);

Any time you care about the length of the result, I'd generally use an actual strbuf instead of xstrfmt. The extra strlen isn't a big deal here, but it somehow seems simpler to me. It probably doesn't matter much either way, though.

Quoted 14 lines
builtin/commit.c +2 -4
 int run_commit_hook(int editor_is_used, const char *index_file, const char *name, ...)
 {
-	const char *hook_env[3] =  { NULL };
-	char index[PATH_MAX];
+	struct argv_array hook_env = ARGV_ARRAY_INIT;
 	va_list args;
 	int ret;
 
-	snprintf(index, sizeof(index), "GIT_INDEX_FILE=%s", index_file);
-	hook_env[0] = index;
+	argv_array_pushf(&hook_env, "GIT_INDEX_FILE=%s", index_file);
 
 	/*
 	 * Let the hook know that no editor will be launched.

builtin/commit.c static int run_rewrite_hook(const unsigned char *oldsha1, · line 1574

Quoted 10 lines
builtin/commit.c +3 -2
 	 */
 	if (!editor_is_used)
-		hook_env[1] = "GIT_EDITOR=:";
+		argv_array_push(&hook_env, "GIT_EDITOR=:");
 
 	va_start(args, name);
-	ret = run_hook_ve(hook_env, name, args);
+	ret = run_hook_ve(hook_env.argv,name, args);
 	va_end(args);
+	argv_array_clear(&hook_env);

Missing whitespace between arguments in the run_hook_ve() call.

-Peff

#2 lore raw
Quoted 4 lines · Jeff King
Quoted 4 lines
builtin/commit.c +2 -0
+		argv_array_pushf(&env, "GIT_INDEX_FILE=%s", index_file);
+		if (launch_editor(git_path_commit_editmsg(), NULL, env.argv)) {
 			fprintf(stderr,
 			_("Please supply the message using either -m or -F option.\n"));
Quoted 8 lines

builtin/commit.c static int prepare_to_commit(const char *index_file, const char *prefix, · line 972

Quoted 4 lines
builtin/commit.c +2 -0
+			argv_array_clear(&env);
 			exit(1);
 		}
+		argv_array_clear(&env);

I'd generally skip the clear() right before exiting, though I saw somebody disagree with me recently on that. I wonder if we should decide as a project on whether it is worth doing or not.

I'd say it is a responsibility of the person who turns exit(1) to return -1 to ensure the code does not leak.

Quoted 23 lines
Quoted 23 lines
builtin/commit.c +2 -6
@@ -1525,12 +1526,10 @@ static int git_commit_config(const char *k, const char *v, void *cb)
 static int run_rewrite_hook(const unsigned char *oldsha1,
 			    const unsigned char *newsha1)
 {
-	/* oldsha1 SP newsha1 LF NUL */
-	static char buf[2*40 + 3];
+	char *buf;
 	struct child_process proc = CHILD_PROCESS_INIT;
 	const char *argv[3];
 	int code;
-	size_t n;
 
 	argv[0] = find_hook("post-rewrite");
 	if (!argv[0])
@@ -1546,34 +1545,33 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
 	code = start_command(&proc);
 	if (code)
 		return code;
-	n = snprintf(buf, sizeof(buf), "%s %s\n",
-		     sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
+	buf = xstrfmt("%s %s\n", sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
 	sigchain_push(SIGPIPE, SIG_IGN);
-	write_in_full(proc.in, buf, n);
Quoted 8 lines

builtin/commit.c static int run_rewrite_hook(const unsigned char *oldsha1, · line 1552

Quoted 3 lines
builtin/commit.c +2 -0
+	write_in_full(proc.in, buf, strlen(buf));
 	close(proc.in);
+	free(buf);

Any time you care about the length of the result, I'd generally use an actual strbuf instead of xstrfmt. The extra strlen isn't a big deal here, but it somehow seems simpler to me. It probably doesn't matter much either way, though.

Your justification for this extra allocation was that it is a heavy-weight operation. While I agree that the runtime cost of allocation and deallocation does not matter, I would be a bit worried about extra cognitive burden to programmers. They did not have to worry about leaking because they are writing a fixed length string. Now they do, whether they use xstrfmt() or struct strbuf. When they need to update what they write, they do have to remember to adjust the size of the "fixed string", and the original is not free from the "programmers' cognitive cost" point of view, of course. Probably use of strbuf/xstrfmt is an overall win.

And of course, I think strbuf is more appropriate if you have to do strlen().

#3 lore raw
Quoted 32 lines · Junio C Hamano
Quoted 23 lines
Quoted 23 lines
builtin/commit.c +2 -6
@@ -1525,12 +1526,10 @@ static int git_commit_config(const char *k, const char *v, void *cb)
 static int run_rewrite_hook(const unsigned char *oldsha1,
 			    const unsigned char *newsha1)
 {
-	/* oldsha1 SP newsha1 LF NUL */
-	static char buf[2*40 + 3];
+	char *buf;
 	struct child_process proc = CHILD_PROCESS_INIT;
 	const char *argv[3];
 	int code;
-	size_t n;
 
 	argv[0] = find_hook("post-rewrite");
 	if (!argv[0])
@@ -1546,34 +1545,33 @@ static int run_rewrite_hook(const unsigned char *oldsha1,
 	code = start_command(&proc);
 	if (code)
 		return code;
-	n = snprintf(buf, sizeof(buf), "%s %s\n",
-		     sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
+	buf = xstrfmt("%s %s\n", sha1_to_hex(oldsha1), sha1_to_hex(newsha1));
 	sigchain_push(SIGPIPE, SIG_IGN);
-	write_in_full(proc.in, buf, n);
Quoted 8 lines

builtin/commit.c static int run_rewrite_hook(const unsigned char *oldsha1, · line 1552

Quoted 3 lines
builtin/commit.c +2 -0
+	write_in_full(proc.in, buf, strlen(buf));
 	close(proc.in);
+	free(buf);

Any time you care about the length of the result, I'd generally use an actual strbuf instead of xstrfmt. The extra strlen isn't a big deal here, but it somehow seems simpler to me. It probably doesn't matter much either way, though.

Quoted 10 lines

Your justification for this extra allocation was that it is a heavy-weight operation. While I agree that the runtime cost of allocation and deallocation does not matter, I would be a bit worried about extra cognitive burden to programmers. They did not have to worry about leaking because they are writing a fixed length string. Now they do, whether they use xstrfmt() or struct strbuf. When they need to update what they write, they do have to remember to adjust the size of the "fixed string", and the original is not free from the "programmers' cognitive cost" point of view, of course. Probably use of strbuf/xstrfmt is an overall win.

So I think you are agreeing, but I have a minor nit to pick. :)

The fact that the extra allocation will not hurt performance is _necessary_, but not _sufficient_. So it's not a justification in itself, only something we have to check before proceeding.

The only justification here is that magic numbers like "2*40 + 3" are confusing and a potential maintenance burden. And that's why I suggested splitting this one out from the other two (whose justification is "PATH_MAX is sometimes too small").

I agree with you that it's a tradeoff between "magic numbers" versus "having to free resources". In my opinion it's a net improvement, but I think it would also be reasonable to switch to xsnprintf() here. Then the programmer has an automatic check that the buffer size is sufficient.

-Peff