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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
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)