Resolve static check warnings in example code#5142
Resolve static check warnings in example code#5142pks-t merged 1 commit intolibgit2:masterfrom scottfurry:StaticChkFixExamples
Conversation
pks-t
left a comment
There was a problem hiding this comment.
Thanks for this contribution! The usage fix is obviously correct, the other one needs some amends though
|
|
||
| if (!o->dir) | ||
| usage("must specify directory to init", NULL); | ||
| usage("must specify directory to init", ""); |
There was a problem hiding this comment.
Yup, passing NULL to fprintf is definitely wrong
examples/merge.c
Outdated
| check_lg2(git_repository_head(&head_ref, repo), "failed to get repo HEAD", NULL); | ||
| if (resolve_refish(&merge_commit, repo, opts->heads[0])) { | ||
| fprintf(stderr, "failed to resolve refish %s", opts->heads[0]); | ||
| git_commit_free(*parents); |
There was a problem hiding this comment.
We'll have to call free(parents) here. We haven't yet populated the parents array, only allocated the array itself
|
Reading on |
examples/merge.c
Outdated
| check_lg2(git_repository_head(&head_ref, repo), "failed to get repo HEAD", NULL); | ||
| if (resolve_refish(&merge_commit, repo, opts->heads[0])) { | ||
| fprintf(stderr, "failed to resolve refish %s", opts->heads[0]); | ||
| free(*parents); |
There was a problem hiding this comment.
You need to free(parents), though, not the the first element of the parents array :) Note the missing *
There was a problem hiding this comment.
I haven't thought this low-level in awhile but I I think I get it now.
There's a reason I took "low hanging fruit" for a fix.
Using cppcheck on libgit2 sources indicated two warnings in example code. merge.c was reported as having a memory leak. Fix applied was to `free()` memory pointed to by `parents`. init.c was reported as having a null pointer dereference on variable arg. Function 'usage' was being called with a null variable. Changed supplied parameter to empty string.
pks-t
left a comment
There was a problem hiding this comment.
Thanks a lot @scottfurry, looks good to me now!
Using cppcheck on libgit2 sources indicated two warnings in example code.
merge.cwas reported as having a memory leak. Fix applied was to apply git_commit_free to value of variableparents.init.cwas reported as having a null pointer dereference on variablearg. Functionusagewas being called with a null variable. Changed supplied parameter to empty string.