siva-msft / libgit2

Other
0 stars 1 forks source link

Potential issue in src/index.c: Unchecked return from initialization function #21

Open monocle-ai opened 4 years ago

monocle-ai commented 4 years ago

What is a Conditionally Uninitialized Variable? The return value of a function that is potentially used to initialize a local variable is not checked. Therefore, reading the local variable may result in undefined behavior.

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


Instance 1 File : src/index.c Enclosing Function : git_index_remove_directory Function : index_find https://github.com/siva-msft/libgit2/blob/53d0ba4625cc355f03d925ec26fc92310dd89fee/src/index.c#L1196 Issue in: pos

Code extract:


    if (!(error = git_buf_sets(&pfx, dir)) &&
        !(error = git_path_to_dir(&pfx)))
        index_find(&pos, index, pfx.ptr, pfx.size, GIT_INDEX_STAGE_ANY, false); <------ HERE

    while (!error) {

How can I fix it? Correct reference usage found in src/index.c at line 2480. https://github.com/siva-msft/libgit2/blob/53d0ba4625cc355f03d925ec26fc92310dd89fee/src/index.c#L2480 Code extract:

        /* skip ignored items that are not already in the index */
        if ((flags & GIT_INDEX_ADD_FORCE) == 0 &&
            git_iterator_current_is_ignored(wditer) &&
            index_find(&existing, index, wd->path, 0, 0, true) < 0) <------ HERE
            continue;

Instance 2 File : src/index.c Enclosing Function : write_entries Function : git_vector_dup https://github.com/siva-msft/libgit2/blob/53d0ba4625cc355f03d925ec26fc92310dd89fee/src/index.c#L2116 Issue in: _casesorted

Code extract:

    /* If index->entries is sorted case-insensitively, then we need
     * to re-sort it case-sensitively before writing */
    if (index->ignore_case) {
        git_vector_dup(&case_sorted, &index->entries, git_index_entry_cmp); <------ HERE
        git_vector_sort(&case_sorted);
        entries = &case_sorted;

How can I fix it? Correct reference usage found in src/index.c at line 2656. https://github.com/siva-msft/libgit2/blob/53d0ba4625cc355f03d925ec26fc92310dd89fee/src/index.c#L2656 Code extract:

    git_atomic_inc(&index->readers);
    git_vector_sort(&index->entries);

    error = git_vector_dup(snap, &index->entries, index->entries._cmp); <------ HERE

    git_mutex_unlock(&index->lock);

Instance 3 File : src/index.c Enclosing Function : write_index Function : git_filebuf_hash https://github.com/siva-msft/libgit2/blob/53d0ba4625cc355f03d925ec26fc92310dd89fee/src/index.c#L2308 Issue in: _hashfinal

Code extract:

        return -1;

    /* get out the hash for all the contents we've appended to the file */
    git_filebuf_hash(&hash_final, file); <------ HERE

    /* write it at the end of the file */

How can I fix it? Correct reference usage found in src/indexer.c at line 1003. https://github.com/siva-msft/libgit2/blob/53d0ba4625cc355f03d925ec26fc92310dd89fee/src/indexer.c#L1003 Code extract:

        goto on_error;

    /* Write out the hash of the idx */
    if (git_filebuf_hash(&trailer_hash, &index_file) < 0) <------ HERE
        goto on_error;