Merge pull request #6244 from jorio/fix-diff_delta_format_path-crash Fix crash when regenerating a patch with unquoted spaces in filename
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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
diff --git a/src/libgit2/diff_print.c b/src/libgit2/diff_print.c
index 03d25b0..6c5a2cd 100644
--- a/src/libgit2/diff_print.c
+++ b/src/libgit2/diff_print.c
@@ -316,6 +316,11 @@ static int diff_print_oid_range(
static int diff_delta_format_path(
git_str *out, const char *prefix, const char *filename)
{
+ if (!filename) {
+ /* don't prefix "/dev/null" */
+ return git_str_puts(out, "/dev/null");
+ }
+
if (git_str_joinpath(out, prefix, filename) < 0)
return -1;
diff --git a/tests/libgit2/diff/parse.c b/tests/libgit2/diff/parse.c
index d3a0c8d..9c3f798 100644
--- a/tests/libgit2/diff/parse.c
+++ b/tests/libgit2/diff/parse.c
@@ -431,6 +431,32 @@ void test_diff_parse__new_file_with_space(void)
git_diff_free(diff);
}
+void test_diff_parse__new_file_with_space_and_regenerate_patch(void)
+{
+ const char *content = PATCH_ORIGINAL_NEW_FILE_WITH_SPACE;
+ git_diff *diff = NULL;
+ git_buf buf = GIT_BUF_INIT;
+
+ cl_git_pass(git_diff_from_buffer(&diff, content, strlen(content)));
+ cl_git_pass(git_diff_to_buf(&buf, diff, GIT_DIFF_FORMAT_PATCH));
+
+ git_buf_dispose(&buf);
+ git_diff_free(diff);
+}
+
+void test_diff_parse__delete_file_with_space_and_regenerate_patch(void)
+{
+ const char *content = PATCH_DELETE_FILE_WITH_SPACE;
+ git_diff *diff = NULL;
+ git_buf buf = GIT_BUF_INIT;
+
+ cl_git_pass(git_diff_from_buffer(&diff, content, strlen(content)));
+ cl_git_pass(git_diff_to_buf(&buf, diff, GIT_DIFF_FORMAT_PATCH));
+
+ git_buf_dispose(&buf);
+ git_diff_free(diff);
+}
+
void test_diff_parse__crlf(void)
{
const char *text = PATCH_CRLF;
diff --git a/tests/libgit2/patch/patch_common.h b/tests/libgit2/patch/patch_common.h
index 1e03889..7e2cb6a 100644
--- a/tests/libgit2/patch/patch_common.h
+++ b/tests/libgit2/patch/patch_common.h
@@ -933,6 +933,15 @@
"@@ -0,0 +1 @@\n" \
"+a\n"
+#define PATCH_DELETE_FILE_WITH_SPACE \
+ "diff --git a/sp ace.txt b/sp ace.txt\n" \
+ "deleted file mode 100644\n" \
+ "index 789819226..000000000\n" \
+ "--- a/sp ace.txt\n" \
+ "+++ /dev/null\n" \
+ "@@ -1 +0,0 @@\n" \
+ "-a\n"
+
#define PATCH_CRLF \
"diff --git a/test-file b/test-file\r\n" \
"new file mode 100644\r\n" \