Skip to content

"SIGSEGV: Illegal storage access" error when I try to use libgit2's library. #3795

@wicast

Description

@wicast

I have installed libgit2 library on my linux PC.
And the following code will get "SIGSEGV: Illegal storage access" error whenever is in debug mode or release mode.

type
  git_commit=ptr pointer
const
  GIT_OID_RAWSZ* = 20
type
  git_oid* = object
    id*: array[GIT_OID_RAWSZ, cuchar]

proc git_commit_lookup*(commit:git_commit; repo: pointer; id: pointer):cint {.  importc: "git_commit_lookup",dynlib:"libgit2.so",cdecl.}

proc git_libgit2_init():cint {.importc,dynlib:"libgit2.so".}
# proc git_libgit2_init():cint {.importc:"git_libgit2_init",header:"<git2.h>".}
proc git_repository_init(ou:pointer,path:cstring,is_bare:bool):cint{.importc,dynlib:"libgit2.so".}
# proc git_repository_init(ou:pointer,path:cstring,is_bare:bool):cint{.importc:"git_repository_init",header:"<git2.h>".}

proc git_libgit2_version*(major: ptr cint; minor: ptr cint; rev: ptr cint) {.  importc: "git_libgit2_version",dynlib:"libgit2.so".}

proc git_repository_open(ou:pointer,path:cstring):cint {. importc: "git_repository_open",dynlib:"libgit2.so" .}


proc git_oid_fromstr*(ou:ptr git_oid,str:cstring):cint {.importc:"git_oid_fromstr",dynlib:"libgit2.so".}

var
  repo:pointer
  major,minor,rev:cint


git_libgit2_version(major.addr,minor.addr,rev.addr)
echo major
echo minor
echo rev

var err= git_repository_open(repo,"./tmp")
if err!=0:
  quit(1)

var
  id:git_oid
  sha:cstring="1da5570cc4153a7adfd01caaec666c6ae3a611d5"

err=git_oid_fromstr(id.addr,sha)
if err!=0:
  quit(1)

echo repr(id)

var
  commit:pointer

discard git_commit_lookup(commit.addr,repo,id.addr)

The problem is occured in last line of git_commit_lookup procedure.

The origin signature of git_commit_lookup is

git_commit_lookup(git_commit **commit, git_repository *repo, const git_oid *id);

And git_commit struct is defined in commit.h as:
typedef struct git_commit git_commit;
c2nim will skip this.

I tried C to call the libgit2,everything is ok.

#include <stdlib.h>
#include <stdio.h>
#include "git2.h"



int main(){
    /* void *repo = NULL; */
    git_repository *repo = NULL;
    int er=git_repository_open(&repo, "./tmp/");
    if(er!=0){
        exit(1);
    }
    git_oid oid;

    int error = git_oid_fromstr(&oid,"1da5570cc4153a7adfd01caaec666c6ae3a611d5");
    if(error!=0){
        exit(1);
    }

    /* git_commit *commit; */
    void *commit;
    int err = git_commit_lookup(&commit, repo, &oid);

    const char *message = git_commit_message(commit);
    printf("%s",message);
}

I need some help to get it work.

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