Bonjour,
Dans le cadre du développement d'une extension PHP (écrit en C), je me retrouve dans le cas suivant :
Cette fonction, phpgit_working_directory_get_branch, est appelée par une autre fonction
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 const char * phpgit_working_directory_get_branch(git_repository *repo, int format) { int error = 0; const char *branch = NULL; git_reference *head = NULL; /* Je pense que le code suivant n'est pas déterminant du problème, mais comme je suis un noob en C, je le laisse au cas où */ error = git_repository_head(&head, repo); if (error == GIT_EUNBORNBRANCH || error == GIT_ENOTFOUND) { zend_throw_exception("Exception", "Branch not found", 404); } else if (!error) { branch = git_reference_shorthand(head); } else { zend_throw_exception("Exception", "Failed to get current branch", 500); } if (!branch) { if (format == FORMAT_LONG) { zend_throw_exception("Exception", "Not currently on any branch", 0); } else { zend_throw_exception("Exception", "HEAD (no branch)", 1); } } /* et on retourne branch, qui est un pointeur NULL, ou un pointeur "valide" définit par git_reference_shorthand, qui renvoi "const char *" ( https://libgit2.github.com/libgit2/#v0.23.2/group/reference/git_reference_shorthand ) */ return branch; }
En l'état, je prends un warning :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 PHP_METHOD(WorkingDirectory, getBranch) { php_working_directory_t *wd; git_repository *repo; const char *branch; zend_string zbranch; wd = PHP_GIT_WORKING_DIRECTORY_FETCH_FROM(Z_OBJ_P(getThis())); repo = wd->repo; /* La ligne à problème */ branch = phpgit_working_directory_get_branch(repo, FORMAT_SHORT); if(branch != NULL) { zbranch = zend_string_init(branch, strlen(branch), 0); RETURN_STR(zbranch); } else { RETURN_FALSE(); } }Si je cast :warning: assignment makes pointer from integer without a cast
Je prends un autre warning :
Code : Sélectionner tout - Visualiser dans une fenêtre à part branch = (const char *) phpgit_working_directory_get_branch(repo, FORMAT_SHORT);warning: cast to pointer from integer of different size [-Wint-to-pointer-cast]
Du coup j'ai deux questions :
1. Pourquoi j'ai besoin de caster un type en un type exactement identique : const char* en const char* ?
2. Mes recherches sur le 2e warning m'amène à penser que phpgit_working_directory_get_branch me retourne en fait un int (typiquement l'adresse vers laquelle pointe ma variable branch, mais du coup je en comprends pas pourquoi) et que il essai de caster cet int en pointeur à nouveau, et que ça lui pose un problème, et la non plus je ne comprends pas pourquoi car cet int d'adresse aurait la même taille puisqu'il vient d'un pointeur, casté en pointeur, sauf que le warning dit que j'ai tord. Du coup je suis perdu, d'où mon message![]()
Partager