-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resolve static check warnings in example code #5142
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this contribution! The usage
fix is obviously correct, the other one needs some amends though
@@ -244,5 +244,5 @@ static void parse_opts(struct opts *o, int argc, char *argv[]) | |||
} | |||
|
|||
if (!o->dir) | |||
usage("must specify directory to init", NULL); | |||
usage("must specify directory to init", ""); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, passing NULL
to fprintf
is definitely wrong
examples/merge.c
Outdated
@@ -220,6 +220,7 @@ static int create_merge_commit(git_repository *repo, git_index *index, merge_opt | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -220,6 +220,7 @@ static int create_merge_commit(git_repository *repo, git_index *index, merge_opt | |||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot @scottfurry, looks good to me now!
Using cppcheck on libgit2 sources indicated two warnings in example code.
merge.c
was reported as having a memory leak. Fix applied was to apply git_commit_free to value of variableparents
.init.c
was reported as having a null pointer dereference on variablearg
. Functionusage
was being called with a null variable. Changed supplied parameter to empty string.