siva-msft / libgit2

Other
0 stars 1 forks source link

Potential issue in src/odb.c: Return Value Not Checked from Function Call #65

Open monocle-ai opened 4 years ago

monocle-ai commented 4 years ago

In this codebase, you often check the return value of the implicated function when calling it, but in this instance, it appears that you didn’t. Using a consistent return value checking and/or error handling approach can improve code robustness and readability.

3 instances of this defect were found in the following locations:


Instance 1 File : src/odb.c Enclosing Function : git_odb__error_mismatch Function : git_oid_tostr https://github.com/siva-msft/libgit2/blob/1e987525b3bd86fc29e7ff67d973861efd22faf7/src/odb.c#L1550 Code extract:

{
    char expected_oid[GIT_OID_HEXSZ + 1], actual_oid[GIT_OID_HEXSZ + 1];

    git_oid_tostr(expected_oid, sizeof(expected_oid), expected); <------ HERE
    git_oid_tostr(actual_oid, sizeof(actual_oid), actual);

How can I fix it? Correct reference usage found in src/diff_generate.c at line 1649. https://github.com/siva-msft/libgit2/blob/1e987525b3bd86fc29e7ff67d973861efd22faf7/src/diff_generate.c#L1649 Code extract:


        error = -1;
        git_error_set(GIT_ERROR_INVALID, "commit %s is a merge commit",
            git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit))); <------ HERE
        goto on_error;
    }

Instance 2 File : src/odb.c Enclosing Function : git_odb__error_mismatch Function : git_oid_tostr https://github.com/siva-msft/libgit2/blob/1e987525b3bd86fc29e7ff67d973861efd22faf7/src/odb.c#L1551 Code extract:

    char expected_oid[GIT_OID_HEXSZ + 1], actual_oid[GIT_OID_HEXSZ + 1];

    git_oid_tostr(expected_oid, sizeof(expected_oid), expected);
    git_oid_tostr(actual_oid, sizeof(actual_oid), actual); <------ HERE

    git_error_set(GIT_ERROR_ODB, "object hash mismatch - expected %s but got %s",

How can I fix it? Correct reference usage found in src/diff_generate.c at line 1649. https://github.com/siva-msft/libgit2/blob/1e987525b3bd86fc29e7ff67d973861efd22faf7/src/diff_generate.c#L1649 Code extract:


        error = -1;
        git_error_set(GIT_ERROR_INVALID, "commit %s is a merge commit",
            git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit))); <------ HERE
        goto on_error;
    }

Instance 3 File : src/odb.c Enclosing Function : git_odb__error_notfound Function : git_oid_tostr https://github.com/siva-msft/libgit2/blob/1e987525b3bd86fc29e7ff67d973861efd22faf7/src/odb.c#L1564 Code extract:

{
    if (oid != NULL) {
        char oid_str[GIT_OID_HEXSZ + 1];
        git_oid_tostr(oid_str, oid_len+1, oid); <------ HERE
        git_error_set(GIT_ERROR_ODB, "object not found - %s (%.*s)",
            message, (int) oid_len, oid_str);

How can I fix it? Correct reference usage found in src/diff_generate.c at line 1649. https://github.com/siva-msft/libgit2/blob/1e987525b3bd86fc29e7ff67d973861efd22faf7/src/diff_generate.c#L1649 Code extract:


        error = -1;
        git_error_set(GIT_ERROR_INVALID, "commit %s is a merge commit",
            git_oid_tostr(commit_oidstr, GIT_OID_HEXSZ + 1, git_commit_id(commit))); <------ HERE
        goto on_error;
    }