Skip to content

Commit 31f8f82

Browse files
pks-tethomson
authored andcommitted
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 parent 9ceafb5 commit 31f8f82

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/diff_driver.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,9 @@ static int git_diff_driver_load(
281281
/* TODO: warn if diff.<name>.command or diff.<name>.textconv are set */
282282

283283
git_buf_truncate(&name, namelen + strlen("diff.."));
284-
git_buf_put(&name, "xfuncname", strlen("xfuncname"));
284+
if ((error = git_buf_PUTS(&name, "xfuncname")) < 0)
285+
goto done;
286+
285287
if ((error = git_config_get_multivar_foreach(
286288
cfg, name.ptr, NULL, diff_driver_xfuncname, drv)) < 0) {
287289
if (error != GIT_ENOTFOUND)
@@ -290,7 +292,9 @@ static int git_diff_driver_load(
290292
}
291293

292294
git_buf_truncate(&name, namelen + strlen("diff.."));
293-
git_buf_put(&name, "funcname", strlen("funcname"));
295+
if ((error = git_buf_PUTS(&name, "funcname")) < 0)
296+
goto done;
297+
294298
if ((error = git_config_get_multivar_foreach(
295299
cfg, name.ptr, NULL, diff_driver_funcname, drv)) < 0) {
296300
if (error != GIT_ENOTFOUND)
@@ -305,7 +309,9 @@ static int git_diff_driver_load(
305309
}
306310

307311
git_buf_truncate(&name, namelen + strlen("diff.."));
308-
git_buf_put(&name, "wordregex", strlen("wordregex"));
312+
if ((error = git_buf_PUTS(&name, "wordregex")) < 0)
313+
goto done;
314+
309315
if ((error = git_config__lookup_entry(&ce, cfg, name.ptr, false)) < 0)
310316
goto done;
311317
if (!ce || !ce->value)

0 commit comments

Comments
 (0)