diff options
author | Vicent Marti <tanoku@gmail.com> | 2014-11-21 17:24:55 +0100 |
---|---|---|
committer | Vicent Marti <tanoku@gmail.com> | 2014-11-21 17:24:55 +0100 |
commit | 2e1e0f108f324719537bddd537099798e7d7449a (patch) | |
tree | 94601a331ef7d3ac93e8b25ad5a7e6cf740213c6 /examples | |
parent | 1ba48b7caf8e2de010b8c0038860b90be0692274 (diff) | |
download | libgit2-2e1e0f108f324719537bddd537099798e7d7449a.tar.gz |
blame: Do not assume blob contents are NULL-terminated
Diffstat (limited to 'examples')
-rw-r--r-- | examples/blame.c | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/examples/blame.c b/examples/blame.c index 9d38f25a4..b126c0d33 100644 --- a/examples/blame.c +++ b/examples/blame.c @@ -38,6 +38,7 @@ static void parse_opts(struct opts *o, int argc, char *argv[]); int main(int argc, char *argv[]) { int i, line, break_on_null_hunk; + size_t rawsize; char spec[1024] = {0}; struct opts o = {0}; const char *rawdata; @@ -94,23 +95,24 @@ int main(int argc, char *argv[]) git_object_free(obj); rawdata = git_blob_rawcontent(blob); + rawsize = git_blob_rawsize(blob); /** Produce the output. */ line = 1; i = 0; break_on_null_hunk = 0; - while (i < git_blob_rawsize(blob)) { - const char *eol = strchr(rawdata+i, '\n'); + while (i < rawsize) { + const char *eol = memchr(rawdata + i, '\n', rawsize - i); char oid[10] = {0}; const git_blame_hunk *hunk = git_blame_get_hunk_byline(blame, line); - if (break_on_null_hunk && !hunk) break; + if (break_on_null_hunk && !hunk) + break; if (hunk) { char sig[128] = {0}; break_on_null_hunk = 1; - git_oid_tostr(oid, 10, &hunk->final_commit_id); snprintf(sig, 30, "%s <%s>", hunk->final_signature->name, hunk->final_signature->email); @@ -118,8 +120,8 @@ int main(int argc, char *argv[]) oid, sig, line, - (int)(eol-rawdata-i), - rawdata+i); + (int)(eol - rawdata - i), + rawdata + i); } i = (int)(eol - rawdata + 1); |