git_index_entry* can't read path on ubuntu. · Issue #35 · libgit2/libgit2 · GitHub
Skip to content

git_index_entry* can't read path on ubuntu. #35

Closed

Description

@chobie

im tring to use libgit2 on my ubuntu 10.10.
i found Segmentation fault when try to read git_index_entry->path.

git clone https://github.com/libgit2/libgit2.git
cd libgit2
./waf configure
./waf build-shared
sudo ./waf install-shared

cat > git_index_test.c <<EOF
#include <stdio.h>
#include <git2.h>

int main(int argc, char * args[])
{
    git_index_entry *entry;
    git_index *index;
    git_index_open_bare(&index,".git/index");

    git_index_read(index);
    git_index_entrycount(index);
    int offset = git_index_find(index,"README.md");

    entry = git_index_get(index,offset);

    printf("pointer_address: %d\n", entry->path);
    //Segmentation fault on my machine.
    printf("path: %s\n",entry->path);
    git_index_free(index);

    return 0;
}
EOF

gcc -lgit2 -o git_index_test git_index_test.c

./git_index_test
pointer_address: 9
Segmentation fault

I searched reason. but i couldn't understand.
git_index_entry->path readable in git_index_get(). but git_index_entry->path was wrong address in main.

(but ruggaed work well on my machine.probably i missunderstood something...)

Could you please advice me how to fix this probrem?

$ gcc -v
Using built-in specs.
Target: i686-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.4.4-14ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=i686-linux-gnu --host=i686-linux-gnu --target=i686-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5) 

Activity

  1. vmg commented on Dec 27, 2010

    @vmg
    Member

    Hey chobie, it seems to me that the offset you are getting from git_index_find is actually GIT_ENOTFOUND, because the specified file cannot be found. That's why the lookup with git_index_get is failing and returning a NULL pointer. Can you tell me exactly what's the offset you are getting?

  2. chobie commented on Dec 28, 2010

    @chobie
    MemberAuthor

    Hi tanoku. thanks for your reply.

    git init
    echo "Hello" > README.md
    echo "WORLD" > INSTALL
    git add README.md INSTALL
    git commit -m "initial commit"
    

    i put my code on this directory. and just added show offset value feature.

    printf("offset: %d\n",offset);
    

    compile program

    gcc -lgit2 -O0 -g -o git_index_test git_index_test.c
    

    check git index file.

    git ls-files --stage
    100644 83cf24cdfb4891a36bee93421930dd220766299a 0       INSTALL
    100644 e965047ad7c57865823c7d992b1d046ea66edf78 0       README.md
    

    there is README.md entry on my repository.
    try to execute my program ./git_index_test

    offset:1
    pointer_address: 9
    Segmentation fault
    

    i also try gdb ./git_index_test.

    git_index_entry->path is correct in git_index_get().
    but git_index_entry->path was wrong address in main scope.
    (git_index_entry->path was just strdup() string. so i wondered about that.)

    ummmm. I couldn't find this solution because i don't have enough C skill.

    i tried my code in some environment.

    OS: Result
    OS X(snow leopard): OK
    Ubuntu 10.10(gcc-4.4, i686-linux-gnu): Segmentation Fault
    CentOS5.5(with remi repository, gcc-4.1.2 i386-redhat-linux): Segmentation Fault

    rugged work fine on my ubuntu desktop.
    probably i missunderstod something in my code...

  3. chobie commented on Dec 29, 2010

    @chobie
    MemberAuthor

    I'm sorry to post so many questions.

    finally, i added -D_FILE_OFFSET_BITS=64 option to compile my code. everything works great :)

    Thank you so much for providing great library.
    then i can enjoy New Year's holidays.

  4. vmg commented on Dec 29, 2010

    @vmg
    Member

    Hmmm... I'm opening this bug again. You shouldn't have to force _FILE_OFFSET_BITS to compile the library. I'll look into this, thanks a lot for the warning.

  5. chobie commented on Dec 30, 2010

    @chobie
    MemberAuthor

    oh, i have not to force _FILE_OFFSET_BITS to compile my code right?

    just write down my code what reappear this probrem.

    on 32bit machine

    git clone https://github.com/libgit2/libgit2.git
    cd libgit2
    ./waf configure && ./waf build-shared
    sudo ./waf install-shared
    
    mkdir ../chobie_test && cd ../chobie_test
    
    git init
    echo "Hello" > README.md
    echo "WORLD" > INSTALL
    git add README.md INSTALL
    git commit -m "initial commit"
    
    
    cat > git_index_test.c <<EOF
    #include <stdio.h>
    #include <git2.h>
    
    int main(int argc, char * args[])
    {
        git_index_entry *entry;
        git_index *index;
        git_index_open_bare(&index,".git/index");
    
        git_index_read(index);
        git_index_entrycount(index);
        int offset = git_index_find(index,"README.md");
        if(offset == GIT_ENOTFOUND){
          fprintf(stderr,"README.md offset not found\n");
          return 0;
        }
    
        entry = git_index_get(index,offset);
    
        printf("pointer_address: %d\n", entry->path);
        printf("path: %s\n",entry->path);
        git_index_free(index);
    
        return 0;
    }
    EOF
    
    # something wrong on 32bit machine
    gcc -o git_index_test -lgit2 git_index_test.c
    ./git_index_test
    #pointer_address: 9
    #Segmentation fault
    
    # works good on 32bit machine
    gcc -o git_index_test -lgit2 -D_FILE_OFFSET_BITS=64 git_index_test.c
    ./git_index_test
    #pointer_address: 141451832
    #path: README.md
    

    i found D_FILE_OFFSET_BITS option where rugged generated Makefile on my machine.
    (because rugged work fine on my machine.)

  6. scorpion007-zz commented on Jan 9, 2011

    @scorpion007-zz

    The problem is that index.c compiles by including the internal src/common.h which defines the correct off_t. But user programs don't have access to that header, so they assume off_t is 32 bits.

    This affects windows versions too.

    User applications need access to the headers defining the right sizes to be compatible (the currently private mingw-compat and msvc-compat.h files)

  7. vmg commented on Jan 10, 2011

    @vmg
    Member

    Actually, I've changed the public header to use more generic types (unsigned int instead of off_t), since the file_size entry in the Index is always 32-bits (defined as such in the standard). I think this should fix the problem everywhere.

  8. scorpion007-zz commented on Jan 10, 2011

    @scorpion007-zz

    By standard, you mean the git ABI?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions