Skip to content

Commit dc851d9

Browse files
committed
commit: clear user-provided buffers
The functions `git_commit_header_field` and `git_commit_extract_signature` both receive buffers used to hand back the results to the user. While these functions called `git_buf_sanitize` on these buffers, this is not the right thing to do, as it will simply initialize or zero-terminate passed buffers. As we want to overwrite contents, we instead have to call `git_buf_clear` to completely reset them.
1 parent cdb2c2a commit dc851d9

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

include/git2/commit.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,8 @@ GIT_EXTERN(int) git_commit_nth_gen_ancestor(
255255
/**
256256
* Get an arbitrary header field
257257
*
258-
* @param out the buffer to fill
258+
* @param out the buffer to fill; existing content will be
259+
* overwritten
259260
* @param commit the commit to look in
260261
* @param field the header field to return
261262
* @return 0 on succeess, GIT_ENOTFOUND if the field does not exist,
@@ -270,8 +271,10 @@ GIT_EXTERN(int) git_commit_header_field(git_buf *out, const git_commit *commit,
270271
* `GITERR_INVALID`. If the commit does not have a signature, the
271272
* error class will be `GITERR_OBJECT`.
272273
*
273-
* @param signature the signature block
274-
* @param signed_data signed data; this is the commit contents minus the signature block
274+
* @param signature the signature block; existing content will be
275+
* overwritten
276+
* @param signed_data signed data; this is the commit contents minus the signature block;
277+
* existing content will be overwritten
275278
* @param repo the repository in which the commit exists
276279
* @param commit_id the commit from which to extract the data
277280
* @param field the name of the header field containing the signature

src/commit.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ int git_commit_header_field(git_buf *out, const git_commit *commit, const char *
642642
{
643643
const char *eol, *buf = commit->raw_header;
644644

645-
git_buf_sanitize(out);
645+
git_buf_clear(out);
646646

647647
while ((eol = strchr(buf, '\n'))) {
648648
/* We can skip continuations here */
@@ -706,8 +706,8 @@ int git_commit_extract_signature(git_buf *signature, git_buf *signed_data, git_r
706706
const char *h, *eol;
707707
int error;
708708

709-
git_buf_sanitize(signature);
710-
git_buf_sanitize(signed_data);
709+
git_buf_clear(signature);
710+
git_buf_clear(signed_data);
711711

712712
if (!field)
713713
field = "gpgsig";

0 commit comments

Comments
 (0)