From: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
To: git@vger.kernel.org, Junio C Hamano <gitster@pobox.com>,
Jeff King <peff@peff.net>,
Jonathan del Strother <maillist@steelskies.com>
Cc: "Nguyễn Thái Ngọc Duy" <pclouds@gmail.com>
Subject: [PATCH] Fix memory corruption when .gitignore does not end by \n
Date: Wed, 20 Jan 2010 21:09:16 +0700 [thread overview]
Message-ID: <1263996556-9712-1-git-send-email-pclouds@gmail.com> (raw)
In-Reply-To: <fcaeb9bf1001200458v436a8adeq5cfc6753900e6c0c@mail.gmail.com>
Commit b5041c5 (Avoid writing to buffer in add_excludes_from_file_1())
tried not to append '\n' at the end because the next commit
may return a buffer that does not have extra space for that.
Unfortunately it left this assignment in the loop:
buf[i - (i && buf[i-1] == '\r')] = 0;
that can corrupt memory if "buf" is not '\n' terminated. But even if
it does not corrupt memory, the last line would not be
NULL-terminated, leading to errors later inside add_exclude().
This patch fixes it by reverting the faulty commit and make
sure "buf" is always \n terminated.
While at it, free unused memory properly.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
This patch causes a crash for me. Not sure if it does for anybody else.
diff --git a/t/t3001-ls-files-others-exclude.sh b/t/t3001-ls-files-others-exclude.sh
index 6d2f2b6..e7efdb5 100755
--- a/t/t3001-ls-files-others-exclude.sh
+++ b/t/t3001-ls-files-others-exclude.sh
@@ -57,7 +57,7 @@ expect
echo '*.1
/*.3
!*.6' >.gitignore
-echo '*.2
+echo -n '*.2
two/*.4
!*.7
*.8' >one/.gitignore
dir.c | 16 +++++++++++++---
1 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/dir.c b/dir.c
index 1538ad5..67c3af6 100644
--- a/dir.c
+++ b/dir.c
@@ -242,6 +242,14 @@ int add_excludes_from_file_to_list(const char *fname,
if (!check_index ||
(buf = read_skip_worktree_file_from_index(fname, &size)) == NULL)
return -1;
+ if (size == 0) {
+ free(buf);
+ return 0;
+ }
+ if (buf[size-1] != '\n') {
+ buf = xrealloc(buf, size+1);
+ buf[size++] = '\n';
+ }
}
else {
size = xsize_t(st.st_size);
@@ -249,19 +257,21 @@ int add_excludes_from_file_to_list(const char *fname,
close(fd);
return 0;
}
- buf = xmalloc(size);
+ buf = xmalloc(size+1);
if (read_in_full(fd, buf, size) != size) {
+ free(buf);
close(fd);
return -1;
}
+ buf[size++] = '\n';
close(fd);
}
if (buf_p)
*buf_p = buf;
entry = buf;
- for (i = 0; i <= size; i++) {
- if (i == size || buf[i] == '\n') {
+ for (i = 0; i < size; i++) {
+ if (buf[i] == '\n') {
if (entry != buf + i && entry[0] != '#') {
buf[i - (i && buf[i-1] == '\r')] = 0;
add_exclude(entry, base, baselen, which);
--
1.6.6.181.g5ee6
next prev parent reply other threads:[~2010-01-20 14:10 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-01-19 17:59 git-status segmentation fault in master / OS X Jonathan del Strother
2010-01-20 0:41 ` Jeff King
2010-01-20 0:56 ` Junio C Hamano
2010-01-20 10:43 ` Jonathan del Strother
2010-01-20 12:58 ` Nguyen Thai Ngoc Duy
2010-01-20 14:09 ` Nguyễn Thái Ngọc Duy [this message]
2010-01-20 19:51 ` [PATCH] Fix memory corruption when .gitignore does not end by \n Junio C Hamano
2010-01-21 1:38 ` Nguyen Thai Ngoc Duy
2010-01-21 6:08 ` Jonathan del Strother
2010-01-21 6:48 ` Junio C Hamano
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=1263996556-9712-1-git-send-email-pclouds@gmail.com \
--to=pclouds@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=maillist@steelskies.com \
--cc=peff@peff.net \
/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).