Skip to content

Commit 606afed

Browse files
authored
Merge pull request libgit2#6244 from jorio/fix-diff_delta_format_path-crash
Fix crash when regenerating a patch with unquoted spaces in filename
2 parents 2fc0fcb + 71bb92b commit 606afed

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

src/libgit2/diff_print.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,11 @@ static int diff_print_oid_range(
316316
static int diff_delta_format_path(
317317
git_str *out, const char *prefix, const char *filename)
318318
{
319+
if (!filename) {
320+
/* don't prefix "/dev/null" */
321+
return git_str_puts(out, "/dev/null");
322+
}
323+
319324
if (git_str_joinpath(out, prefix, filename) < 0)
320325
return -1;
321326

tests/libgit2/diff/parse.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,32 @@ void test_diff_parse__new_file_with_space(void)
431431
git_diff_free(diff);
432432
}
433433

434+
void test_diff_parse__new_file_with_space_and_regenerate_patch(void)
435+
{
436+
const char *content = PATCH_ORIGINAL_NEW_FILE_WITH_SPACE;
437+
git_diff *diff = NULL;
438+
git_buf buf = GIT_BUF_INIT;
439+
440+
cl_git_pass(git_diff_from_buffer(&diff, content, strlen(content)));
441+
cl_git_pass(git_diff_to_buf(&buf, diff, GIT_DIFF_FORMAT_PATCH));
442+
443+
git_buf_dispose(&buf);
444+
git_diff_free(diff);
445+
}
446+
447+
void test_diff_parse__delete_file_with_space_and_regenerate_patch(void)
448+
{
449+
const char *content = PATCH_DELETE_FILE_WITH_SPACE;
450+
git_diff *diff = NULL;
451+
git_buf buf = GIT_BUF_INIT;
452+
453+
cl_git_pass(git_diff_from_buffer(&diff, content, strlen(content)));
454+
cl_git_pass(git_diff_to_buf(&buf, diff, GIT_DIFF_FORMAT_PATCH));
455+
456+
git_buf_dispose(&buf);
457+
git_diff_free(diff);
458+
}
459+
434460
void test_diff_parse__crlf(void)
435461
{
436462
const char *text = PATCH_CRLF;

tests/libgit2/patch/patch_common.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,15 @@
933933
"@@ -0,0 +1 @@\n" \
934934
"+a\n"
935935

936+
#define PATCH_DELETE_FILE_WITH_SPACE \
937+
"diff --git a/sp ace.txt b/sp ace.txt\n" \
938+
"deleted file mode 100644\n" \
939+
"index 789819226..000000000\n" \
940+
"--- a/sp ace.txt\n" \
941+
"+++ /dev/null\n" \
942+
"@@ -1 +0,0 @@\n" \
943+
"-a\n"
944+
936945
#define PATCH_CRLF \
937946
"diff --git a/test-file b/test-file\r\n" \
938947
"new file mode 100644\r\n" \

0 commit comments

Comments
 (0)