Skip to content

git_buf_vprintf fails when parsing repositoryformatversion = 0 on windows #1400

@TNick

Description

@TNick

Originally posted on stackoverflow

git describe returns v0.16.0-2393-gb72f5d4

On a Windows XP, 32-bit machine I've cloned the libgit2 repository using
git clone git://github.com/libgit2/libgit2.git trunk
Then I configured from trunk_build_debug for mingw using:
cmake ../trunk -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS=-g
and build-ed it with: make

With this newly build library I'm trying to create a new repository. So I'm using

    error_code = git_repository_init( &repo, path, 0 );
    if ( error_code != 0 )
    {
        const git_error * error = giterr_last();
       /* the text representation:
        ( error == NULL || error->message == NULL ? 
        "(no description)" : 
        error->message )
        */
    }

On first attempt this fails with the error message:
Git error -1: The global file '.config/git/config' doesn't exist: The system cannot find the file specified.

The .git directory is created and I can use git status to query it. Also, I have git installed in Program Files/git and it works just fine. The file that is mentioned does not exist in user directory, that is true. I'm not happy because I can't check the returned error code (the error may be "real").

So I've created an empty file in D:\Documents and Settings\User Name.config\git\config The .git directory is again created but I get the same -1 error code. This time giterr_last() returns NULL.

Debugging libgit2 I arrived at following failure:

#0  git_buf_vprintf (buf=0x23f3d8, format=0x6cb5226b "%s.%s", ap=0x23efc8 "À\217ß\025\210´ß\025") at $LIBGIT2\trunk\src\buffer.c:197
        args = 0x23efc8 "À\217ß\025\210´ß\025"
        len = -1
        expected_size = 10
#1  0x6cac6a35 in git_buf_printf (buf=0x23f3d8, format=0x6cb5226b "%s.%s") at $LIBGIT2\trunk\src\buffer.c:220
        r = 366985288
        ap = 0x23efc8 "À\217ß\025\210´ß\025"
#2  0x6cad0606 in config_parse (cfg_file=0x15dfcdf8, level=4) at $LIBGIT2\trunk\src\config_file.c:1024
        c = 114
        current_section = 0x15df8fc0 "core"
        var_name = 0x15dfb488 "repositoryformatversion"
        var_value = 0x15dfa510 "0"
        var = 0x15dfbf40
        existing = 0x1c2c42e
        buf = {ptr = 0x15dfc970 "core.repositoryf««««««««îþîþîþîþ", asize = 16, size = 0}
        result = 0
        pos = 0
        idx_i = 3
        cbuf = "[br", '\000' <repeats 43 times>, stripped
        p1 = 0x23efd4 "[br"
        p2 = 0x23efd7 ""
#3  0x6caceec8 in config_open (cfg=0x15dfcdf8, level=4) at $LIBGIT2\trunk\src\config_file.c:201
        res = 0
        b = 0x15dfcdf8
#4  0x6caccf80 in git_config_add_backend (cfg=0x15dfccc8, file=0x15dfcdf8, level=4, force=0) at $LIBGIT2\trunk\src\config.c:265
        internal = 0x513838a4
        result = 366988792
#5  0x6caccbe8 in git_config_add_file_ondisk (cfg=0x15dfccc8, path=0x15dfcda8 "$TEST_PATH/test_new_prj/.git/config", level=4, force=0) at $LIBGIT2\trunk\src\config.c:105
        file = 0x15dfcdf8
        res = 1823827983
#6  0x6cb04f85 in load_config (out=0x15dfaf8c, repo=0x15dfaf88, global_config_path=0x15dfa2a0 "$USER_PATH/.gitconfig", xdg_config_path=0x15dfb2c0 "$USER_PATH/.config/git/config", system_config_path=0x15dfcd30 "$PROGRAM_FILES/Git/etc/gitconfig") at $LIBGIT2\trunk\src\repository.c:467
        error = 0
        config_path = {ptr = 0x15dfcda8 "$TEST_PATH/test_new_prj/.git/config", asize = 56, size = 50}
        cfg = 0x15dfccc8
#7  0x6cb05162 in git_repository_config__weakptr (out=0x23f588, repo=0x15dfaf88) at $LIBGIT2\trunk\src\repository.c:523
        xdg_buf = {ptr = 0x15dfb2c0 "$USER_PATH/.config/git/config", asize = 56, size = 55}
        global_config_path = 0x15dfa2a0 "$USER_PATH/.gitconfig"
        system_config_path = 0x15dfcd30 "$PROGRAM_FILES/Git/etc/gitconfig"
        global_buf = {ptr = 0x15dfa2a0 "$USER_PATH/.gitconfig", asize = 48, size = 47}
        system_buf = {ptr = 0x15dfcd30 "$PROGRAM_FILES/Git/etc/gitconfig", asize = 40, size = 34}
        res = 366981144
        xdg_config_path = 0x15dfb2c0 "$USER_PATH/.config/git/config"
#8  0x6cb044d9 in load_config_data (repo=0x15dfaf88) at $LIBGIT2\trunk\src\repository.c:127
        is_bare = 366977464
        config = 0x3
#9  0x6cb04d1a in git_repository_open_ext (repo_ptr=0x23f714, start_path=0x15dfa240 "$TEST_PATH/test_new_prj/.git/", flags=1, ceiling_dirs=0x0) at $LIBGIT2\trunk\src\repository.c:384
        error = 0
        path = {ptr = 0x6cb60205 "", asize = 0, size = 0}
        parent = {ptr = 0x15dfa300 "$TEST_PATH/test_new_prj/", asize = 40, size = 39}
        repo = 0x15dfaf88
#10 0x6cb04d90 in git_repository_open (repo_out=0x23f714, path=0x15dfa240 "$TEST_PATH/test_new_prj/.git/") at $LIBGIT2\trunk\src\repository.c:398
No locals.
#11 0x6cb068ce in git_repository_init_ext (out=0x23f714, given_repo=0x15dfa178 "$TEST_PATH/test_new_prj", opts=0x23f660) at $LIBGIT2\trunk\src\repository.c:1256
        error = 0
        repo_path = {ptr = 0x15dfa240 "$TEST_PATH/test_new_prj/.git/", asize = 72, size = 44}
        wd_path = {ptr = 0x15dfa200 "$TEST_PATH/test_new_prj/", asize = 40, size = 39}
#12 0x6cb066e1 in git_repository_init (repo_out=0x23f714, path=0x15dfa178 "$TEST_PATH/test_new_prj", is_bare=0) at $LIBGIT2\trunk\src\repository.c:1210
        opts = {version = 1, flags = 196624, mode = 0, workdir_path = 0x0, description = 0x0, template_path = 0x0, initial_head = 0x0, origin_url = 0x0}

So git_buf_vprintf() computes an expected size based on the format (%s.%s)
const size_t expected_size = buf->size + (strlen(format) * 2);

repositoryformatversion = 0 is longer than this, so the call to p_vsnprintf() fails.
Changing the line to prevents the failure.
const size_t expected_size = buf->size + (strlen(format) * 16);
but I don't think is a proper solution (nor do I have other suggestion).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions