From 3b5e9fb3c92d2533fccf387badf87e75b2ce4c7b Mon Sep 17 00:00:00 2001 From: joshaber Date: Wed, 9 Mar 2016 10:39:49 -0500 Subject: [PATCH 1/3] Bail if there's an error. --- generate/templates/manual/src/convenient_patch.cc | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/generate/templates/manual/src/convenient_patch.cc b/generate/templates/manual/src/convenient_patch.cc index a65649cbd..de69854a9 100644 --- a/generate/templates/manual/src/convenient_patch.cc +++ b/generate/templates/manual/src/convenient_patch.cc @@ -56,8 +56,12 @@ PatchData *createFromRaw(git_patch *raw) { for (unsigned int i = 0; i < patch->numHunks; ++i) { HunkData *hunkData = new HunkData; - const git_diff_hunk *hunk; - git_patch_get_hunk(&hunk, &hunkData->numLines, raw, i); + const git_diff_hunk *hunk = NULL; + int result = git_patch_get_hunk(&hunk, &hunkData->numLines, raw, i); + if (result != 0) { + continue; + } + hunkData->hunk.old_start = hunk->old_start; hunkData->hunk.old_lines = hunk->old_lines; hunkData->hunk.new_start = hunk->new_start; @@ -72,8 +76,11 @@ PatchData *createFromRaw(git_patch *raw) { bool EOFFlag = false; for (unsigned int j = 0; j < hunkData->numLines; ++j) { git_diff_line *storeLine = (git_diff_line *)malloc(sizeof(git_diff_line)); - const git_diff_line *line; - git_patch_get_line_in_hunk(&line, raw, i, j); + const git_diff_line *line = NULL; + int result = git_patch_get_line_in_hunk(&line, raw, i, j); + if (result != 0) { + continue; + } if (j == 0) { // calculate strlen only once for the first line of the first hunk. From 049b5013d1a2fdc917c4ac0087bd2c655e062eec Mon Sep 17 00:00:00 2001 From: joshaber Date: Wed, 9 Mar 2016 10:41:05 -0500 Subject: [PATCH 2/3] Use the content length libgit2 gives us. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Libgit2 documents that the content isn’t NUL-terminated, so we can’t call strlen on it. --- generate/templates/manual/src/convenient_patch.cc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/generate/templates/manual/src/convenient_patch.cc b/generate/templates/manual/src/convenient_patch.cc index de69854a9..a3892d74f 100644 --- a/generate/templates/manual/src/convenient_patch.cc +++ b/generate/templates/manual/src/convenient_patch.cc @@ -83,13 +83,12 @@ PatchData *createFromRaw(git_patch *raw) { } if (j == 0) { - // calculate strlen only once for the first line of the first hunk. - int calculatedContentLength = strlen(line->content); + int calculatedContentLength = line->content_len; if ( calculatedContentLength > noNewlineStringLength && - !strcmp( + !strncmp( &line->content[calculatedContentLength - noNewlineStringLength], - "\n\\ No newline at end of file\n" + "\n\\ No newline at end of file\n", std::min(calculatedContentLength, noNewlineStringLength) )) { EOFFlag = true; } From a98ae5cc8148e32db84478b79ae4b8050e78f2d2 Mon Sep 17 00:00:00 2001 From: joshaber Date: Wed, 9 Mar 2016 10:48:09 -0500 Subject: [PATCH 3/3] *sigh* Windows. --- generate/templates/manual/src/convenient_patch.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/generate/templates/manual/src/convenient_patch.cc b/generate/templates/manual/src/convenient_patch.cc index a3892d74f..8638cf0d7 100644 --- a/generate/templates/manual/src/convenient_patch.cc +++ b/generate/templates/manual/src/convenient_patch.cc @@ -88,7 +88,7 @@ PatchData *createFromRaw(git_patch *raw) { calculatedContentLength > noNewlineStringLength && !strncmp( &line->content[calculatedContentLength - noNewlineStringLength], - "\n\\ No newline at end of file\n", std::min(calculatedContentLength, noNewlineStringLength) + "\n\\ No newline at end of file\n", (std::min)(calculatedContentLength, noNewlineStringLength) )) { EOFFlag = true; }