Commit 31f8f82a65c11dfe9e57e618446aaff8cc21d84a

Patrick Steinhardt 2018-03-02T12:18:59

diff_driver: detect memory allocation errors when loading diff driver When searching for a configuration key for the diff driver, we construct the config key by modifying a buffer and then passing it to `git_config_get_multivar_foreach`. We do not check though whether the modification of the buffer actually succeded, so we could in theory end up passing the OOM buffer to the config function. Fix that by checking return codes. While at it, switch to use `git_buf_PUTS` to avoid repetition of the appended string to calculate its length.

diff --git a/src/diff_driver.c b/src/diff_driver.c
index 0ca652c..740b5c9 100644
--- a/src/diff_driver.c
+++ b/src/diff_driver.c
@@ -281,7 +281,9 @@ static int git_diff_driver_load(
 	/* TODO: warn if diff.<name>.command or diff.<name>.textconv are set */
 
 	git_buf_truncate(&name, namelen + strlen("diff.."));
-	git_buf_put(&name, "xfuncname", strlen("xfuncname"));
+	if ((error = git_buf_PUTS(&name, "xfuncname")) < 0)
+		goto done;
+
 	if ((error = git_config_get_multivar_foreach(
 			cfg, name.ptr, NULL, diff_driver_xfuncname, drv)) < 0) {
 		if (error != GIT_ENOTFOUND)
@@ -290,7 +292,9 @@ static int git_diff_driver_load(
 	}
 
 	git_buf_truncate(&name, namelen + strlen("diff.."));
-	git_buf_put(&name, "funcname", strlen("funcname"));
+	if ((error = git_buf_PUTS(&name, "funcname")) < 0)
+		goto done;
+
 	if ((error = git_config_get_multivar_foreach(
 			cfg, name.ptr, NULL, diff_driver_funcname, drv)) < 0) {
 		if (error != GIT_ENOTFOUND)
@@ -305,7 +309,9 @@ static int git_diff_driver_load(
 	}
 
 	git_buf_truncate(&name, namelen + strlen("diff.."));
-	git_buf_put(&name, "wordregex", strlen("wordregex"));
+	if ((error = git_buf_PUTS(&name, "wordregex")) < 0)
+		goto done;
+
 	if ((error = git_config__lookup_entry(&ce, cfg, name.ptr, false)) < 0)
 		goto done;
 	if (!ce || !ce->value)