Skip to content

Commit

Permalink
Fix executable file ownership check
Browse files Browse the repository at this point in the history
When checking ownership for a command in the system PATH, potentially
append a .exe file extension. MSYS itself tolerates omission, but
pacman would be unable to identify the command owner without the
"correct" path.

This is not done if distinct files exist (with/without .exe).
  • Loading branch information
Wolfgang Pupp authored and radioflash committed Feb 29, 2024
1 parent 4903343 commit 993a36f
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/pacman/query.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,12 @@ static int search_path(char **filename, struct stat *bufptr)
path[--plen] = '\0';
}

fullname = malloc(plen + flen + 2);
#ifdef __MSYS__
/* Reserve 4 additional bytes for a potential ".exe" extension */
fullname = malloc(plen + 1 + flen + 4 + 1);
#else
fullname = malloc(plen + 1 + flen + 1);
#endif
if(!fullname) {
free(envpath);
return -1;
Expand All @@ -70,6 +75,26 @@ static int search_path(char **filename, struct stat *bufptr)

if(lstat(fullname, bufptr) == 0) {
free(*filename);
#ifdef __MSYS__
/* Make sure the filename is not supposed to have .exe appended
* MSYS lstat tolerates omission, but the ownership check doesn't */
strcat(fullname, ".exe");
struct stat sbExe;
if (lstat(fullname, &sbExe) == 0) {
if (sbExe.st_ino == bufptr->st_ino
&& sbExe.st_dev == bufptr->st_dev) {
/* file is the same with and without .exe suffix
* => .exe suffix probably required for ownership check. */
*filename = fullname;
*bufptr = sbExe;
free(envpath);
return 0;
}
}
/* undo .exe suffix, because there is no such file or it's
* different from the one without .exe suffix. */
fullname[plen + 1 + flen] = '\0';
#endif
*filename = fullname;
free(envpath);
return 0;
Expand Down

0 comments on commit 993a36f

Please sign in to comment.