Compare
1af4c26d6972ebf85633e56e67e28868b43f22be..997c1daf1dce036cd52ba2a4a3d4060b74dd9d54
worktree add: don't read out of bounds in worktree_basename()
When we search for the start of the basename and `len` is zero, `name` ends up being `path` - 1, out of bounds. Avoid that by checking before decrementing. Fixes https://github.com/git-for-windows/git/issues/6346. Original-patch-by: Matthias Aßhauer <mha1993@live.de> Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe <l.s.r@web.de> · 2026-08-25 18:03 UTC · committed by Junio C Hamano
2 files changed,
+32
−38
modified builtin/checkout.c
| @@ -1340,34 +1340,6 @@ enum checkout_command { | |||
| 1340 | 1340 | CHECKOUT_RESTORE = 3, | |
| 1341 | 1341 | }; | |
| 1342 | 1342 | ||
| 1343 | - | static void advise_disambiguating_remotes(enum checkout_command which_command) | |
| 1344 | - | { | |
| 1345 | - | const char *cmdname; | |
| 1346 | - | ||
| 1347 | - | switch (which_command) { | |
| 1348 | - | case CHECKOUT_CHECKOUT: | |
| 1349 | - | cmdname = "checkout"; | |
| 1350 | - | break; | |
| 1351 | - | case CHECKOUT_SWITCH: | |
| 1352 | - | cmdname = "switch"; | |
| 1353 | - | break; | |
| 1354 | - | default: | |
| 1355 | - | BUG("command <%d> should not reach advise_disambiguating_remotes", | |
| 1356 | - | which_command); | |
| 1357 | - | break; | |
| 1358 | - | } | |
| 1359 | - | ||
| 1360 | - | advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n" | |
| 1361 | - | "you can do so by fully qualifying the name with the --track option:\n" | |
| 1362 | - | "\n" | |
| 1363 | - | " git %s --track origin/<name>\n" | |
| 1364 | - | "\n" | |
| 1365 | - | "If you'd like to always have checkouts of an ambiguous <name> prefer\n" | |
| 1366 | - | "one remote, e.g. the 'origin' remote, consider setting\n" | |
| 1367 | - | "checkout.defaultRemote=origin in your config."), | |
| 1368 | - | cmdname); | |
| 1369 | - | } | |
| 1370 | - | ||
| 1371 | 1343 | static char *parse_remote_branch(const char *arg, | |
| 1372 | 1344 | struct object_id *rev, | |
| 1373 | 1345 | int could_be_checkout_paths, | |
| @@ -1383,11 +1355,35 @@ static char *parse_remote_branch(const char *arg, | |||
| 1383 | 1355 | } | |
| 1384 | 1356 | ||
| 1385 | 1357 | if (!remote && num_matches > 1) { | |
| 1386 | - | if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) | |
| 1387 | - | advise_disambiguating_remotes(which_command); | |
| 1388 | - | ||
| 1389 | - | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 1390 | - | arg, num_matches); | |
| 1358 | + | if (advice_enabled(ADVICE_CHECKOUT_AMBIGUOUS_REMOTE_BRANCH_NAME)) { | |
| 1359 | + | const char *cmdname; | |
| 1360 | + | ||
| 1361 | + | switch (which_command) { | |
| 1362 | + | case CHECKOUT_CHECKOUT: | |
| 1363 | + | cmdname = "checkout"; | |
| 1364 | + | break; | |
| 1365 | + | case CHECKOUT_SWITCH: | |
| 1366 | + | cmdname = "switch"; | |
| 1367 | + | break; | |
| 1368 | + | default: | |
| 1369 | + | BUG("command <%d> should not reach parse_remote_branch", | |
| 1370 | + | which_command); | |
| 1371 | + | break; | |
| 1372 | + | } | |
| 1373 | + | ||
| 1374 | + | advise(_("If you meant to check out a remote tracking branch on, e.g. 'origin',\n" | |
| 1375 | + | "you can do so by fully qualifying the name with the --track option:\n" | |
| 1376 | + | "\n" | |
| 1377 | + | " git %s --track origin/<name>\n" | |
| 1378 | + | "\n" | |
| 1379 | + | "If you'd like to always have checkouts of an ambiguous <name> prefer\n" | |
| 1380 | + | "one remote, e.g. the 'origin' remote, consider setting\n" | |
| 1381 | + | "checkout.defaultRemote=origin in your config."), | |
| 1382 | + | cmdname); | |
| 1383 | + | } | |
| 1384 | + | ||
| 1385 | + | die(_("'%s' matched multiple (%d) remote tracking branches"), | |
| 1386 | + | arg, num_matches); | |
| 1391 | 1387 | } | |
| 1392 | 1388 | ||
| 1393 | 1389 | return remote; | |
modified builtin/worktree.c
| @@ -303,11 +303,9 @@ static const char *worktree_basename(const char *path, int *olen) | |||
| 303 | 303 | while (len && is_dir_sep(path[len - 1])) | |
| 304 | 304 | len--; | |
| 305 | 305 | ||
| 306 | - | for (name = path + len - 1; name > path; name--) | |
| 307 | - | if (is_dir_sep(*name)) { | |
| 308 | - | name++; | |
| 309 | - | break; | |
| 310 | - | } | |
| 306 | + | name = path + len; | |
| 307 | + | while (name > path && !is_dir_sep(name[-1])) | |
| 308 | + | name--; | |
| 311 | 309 | ||
| 312 | 310 | *olen = len; | |
| 313 | 311 | return name; | |