From: "Sebastian Thiel via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: "Torsten Bögershausen" <tboegi@web.de>,
"Sebastian Thiel" <sebastian.thiel@icloud.com>,
"Sebastian Thiel" <sebastian.thiel@icloud.com>
Subject: [PATCH v2] fix `git mv existing-dir non-existing-dir` in some environments.
Date: Wed, 09 Aug 2023 07:47:41 +0000 [thread overview]
Message-ID: <pull.1561.v2.git.1691567261701.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.1561.git.1691506431114.gitgitgadget@gmail.com>
From: Sebastian Thiel <sebastian.thiel@icloud.com>
When moving a directory onto another with `git mv` various checks are
performed. One of of these validates that the destination is not existing.
When calling `lstat` on the destination path and it fails as the path
doesn't exist, some environments seem to overwrite the passed in
`stat` memory nonetheless (I observed this issue on debian 12 of x86_64,
running on OrbStack on ARM, emulated with Rosetta).
This would affect the code that followed as it would still acccess a now
modified `st` structure, which now seems to contain uninitialized memory.
`S_ISDIR(st_dir_mode)` would then typically return false causing the code
to run into a bad case.
The fix avoids overwriting the existing `st` structure, providing an
alternative that exists only for that purpose.
Note that this patch minimizes complexity instead of stack-frame size.
Signed-off-by: Sebastian Thiel <sebastian.thiel@icloud.com>
---
[PATCH] mv: handle lstat() failure correctly
When moving a directory onto another with git mv various checks are
performed. One of of these validates that the destination is not
existing.
When calling lstat on the destination path and it fails as the path
doesn't exist, some environments seem to overwrite the passed in stat
memory nonetheless (I observed this issue on debian 12 of x86_64,
running on OrbStack on ARM, emulated with Rosetta).
This would affect the code that followed as it would still acccess a now
modified st structure, which now seems to contain uninitialized memory.
S_ISDIR(st_dir_mode) would then typically return false causing the code
to run into a bad case.
The fix avoids overwriting the existing st structure, providing an
alternative that exists only for that purpose.
Note that this patch minimizes complexity instead of stack-frame size.
======================================================================
It's worth pointing out that the test demonstrates this case only if one
happens to execute it in one of the environments that happen to have an
lstat that writes into stat even on error. Thus it already worked for me
on MacOS, even without the patch applied, which matches my observation
that a certain script works there but doesn't work on the VM.
Even though the patch now minimizes size, I can imagine one might
instead want to rather copy st.st_mode to protect only the relevant
field from being affected by potential rewrites of st later on.
Changes since v1:
* replaced previous title with recommendation by Junio C Hermano
* improved formatting of commit message and renamed gix to git. Let's
call that a typo
* apply Junio C Hermano's suggestions to test-case
* I refrained from changing the error message as this would mean all
translations need adjustment (and I don't know how this is tracked
then)
I also want to apologise for the possibly terrible formatting and the
repetition - it feels strange but is what gitgadget seems to suggests.
Further, it's my honour to submit a patch to git and interact with the
maintainers, it's like meeting my idols!
Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1561%2FByron%2Ffix-mv-v2
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1561/Byron/fix-mv-v2
Pull-Request: https://github.com/gitgitgadget/git/pull/1561
Range-diff vs v1:
1: ad0e6754e2d ! 1: 8908fd228fe fix `git mv existing-dir non-existing-dir`*
@@ Metadata
Author: Sebastian Thiel <sebastian.thiel@icloud.com>
## Commit message ##
- fix `git mv existing-dir non-existing-dir`*
+ fix `git mv existing-dir non-existing-dir` in some environments.
- *in some environments.
+ When moving a directory onto another with `git mv` various checks are
+ performed. One of of these validates that the destination is not existing.
- When moving a directory onto another with `gix mv`
- various checks are performed. One of of these
- validates that the destination is not an existing
- file.
+ When calling `lstat` on the destination path and it fails as the path
+ doesn't exist, some environments seem to overwrite the passed in
+ `stat` memory nonetheless (I observed this issue on debian 12 of x86_64,
+ running on OrbStack on ARM, emulated with Rosetta).
- When calling `lstat` on the destination path and
- it fails as the path doesn't exist, some
- environments seem to overwrite the passed in
- `stat` memory nonetheless.
- (I observed this issue on debian 12 of x86_64,
- running on OrbStack on ARM, emulated with Rosetta)
+ This would affect the code that followed as it would still acccess a now
+ modified `st` structure, which now seems to contain uninitialized memory.
+ `S_ISDIR(st_dir_mode)` would then typically return false causing the code
+ to run into a bad case.
- This would affect the code that followed as it
- would still acccess a now
- modified `st` structure, which now seems to
- contain uninitialized memory.
- `S_ISDIR(st_dir_mode)` would then typically
- return false causing the code to run into a bad
- case.
+ The fix avoids overwriting the existing `st` structure, providing an
+ alternative that exists only for that purpose.
- The fix avoids overwriting the existing `st`
- structure, providing an alternative that exists
- only for that purpose.
-
- Note that this patch minimizes complexity instead of stack-size.
+ Note that this patch minimizes complexity instead of stack-frame size.
Signed-off-by: Sebastian Thiel <sebastian.thiel@icloud.com>
@@ t/t7001-mv.sh: test_expect_success 'do not move directory over existing director
'
+test_expect_success 'rename directory to non-existing directory' '
-+ mkdir dir-a && touch dir-a/f &&
++ mkdir dir-a &&
++ >dir-a/f &&
+ git add dir-a &&
+ git mv dir-a non-existing-dir
+'
builtin/mv.c | 4 ++--
t/t7001-mv.sh | 7 +++++++
2 files changed, 9 insertions(+), 2 deletions(-)
diff --git a/builtin/mv.c b/builtin/mv.c
index fa84fcb20d8..05e7156034e 100644
--- a/builtin/mv.c
+++ b/builtin/mv.c
@@ -184,7 +184,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
int src_dir_nr = 0, src_dir_alloc = 0;
struct strbuf a_src_dir = STRBUF_INIT;
enum update_mode *modes, dst_mode = 0;
- struct stat st;
+ struct stat st, dest_st;
struct string_list src_for_dst = STRING_LIST_INIT_NODUP;
struct lock_file lock_file = LOCK_INIT;
struct cache_entry *ce;
@@ -304,7 +304,7 @@ int cmd_mv(int argc, const char **argv, const char *prefix)
goto act_on_entry;
}
if (S_ISDIR(st.st_mode)
- && lstat(dst, &st) == 0) {
+ && lstat(dst, &dest_st) == 0) {
bad = _("cannot move directory over file");
goto act_on_entry;
}
diff --git a/t/t7001-mv.sh b/t/t7001-mv.sh
index 898a9205328..f136ea76f7f 100755
--- a/t/t7001-mv.sh
+++ b/t/t7001-mv.sh
@@ -174,6 +174,13 @@ test_expect_success 'do not move directory over existing directory' '
test_must_fail git mv path2 path0
'
+test_expect_success 'rename directory to non-existing directory' '
+ mkdir dir-a &&
+ >dir-a/f &&
+ git add dir-a &&
+ git mv dir-a non-existing-dir
+'
+
test_expect_success 'move into "."' '
git mv path1/path2/ .
'
base-commit: 1b0a5129563ebe720330fdc8f5c6843d27641137
--
gitgitgadget
prev parent reply other threads:[~2023-08-09 7:47 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-08 14:53 [PATCH] fix `git mv existing-dir non-existing-dir`* Sebastian Thiel via GitGitGadget
2023-08-08 17:36 ` Junio C Hamano
2023-08-08 18:40 ` Torsten Bögershausen
2023-08-08 21:53 ` Junio C Hamano
2023-08-12 1:14 ` [PATCH] mv: fix error for moving directory to another Junio C Hamano
2023-08-08 19:00 ` [PATCH] fix `git mv existing-dir non-existing-dir`* Junio C Hamano
2023-08-09 7:47 ` Sebastian Thiel via GitGitGadget [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=pull.1561.v2.git.1691567261701.gitgitgadget@gmail.com \
--to=gitgitgadget@gmail.com \
--cc=git@vger.kernel.org \
--cc=sebastian.thiel@icloud.com \
--cc=tboegi@web.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).