Skip to content

Commit

Permalink
Fix crash on MacOS/amd64 due to undefined behaviour of strlcpy.
Browse files Browse the repository at this point in the history
strlcpy has undefined behaviour in case the copied string regions overlap. On platforms with a vector optimized memcpy implementation, overlapping regions may crash the process e.g. with
Child process pid=0 terminated abnormally: Illegal instruction: 4

At this call-site the strlcpy parameters almost certainly overlap. Refactor the code to do the intended functionality in-place.
  • Loading branch information
Keve authored and bapt committed Nov 12, 2024
1 parent 866ffc6 commit a4a647b
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions libpkg/pkg_add.c
Original file line number Diff line number Diff line change
Expand Up @@ -1139,7 +1139,7 @@ pkg_add_check_pkg_archive(struct pkgdb *db, struct pkg *pkg,
const char *arch;
int ret, retcode;
struct pkg_dep *dep = NULL;
char bd[MAXPATHLEN], *basedir = NULL;
char bd[MAXPATHLEN];
char dpath[MAXPATHLEN], *ppath;
const char *ext = NULL;
struct pkg *pkg_inst = NULL;
Expand Down Expand Up @@ -1187,8 +1187,14 @@ pkg_add_check_pkg_archive(struct pkgdb *db, struct pkg *pkg,
fromstdin = STREQ(path, "-");
strlcpy(bd, path, sizeof(bd));
if (!fromstdin) {
basedir = get_dirname(bd);
strlcpy(bd, basedir, sizeof(bd));
/* In-place truncate bd to the directory components. */
char *basedir = strrchr(bd, '/');
if (NULL == basedir) {
bd[0]='.';
bd[1]='\0';
} else {
*basedir = '\0';
}
if ((ext = strrchr(path, '.')) == NULL) {
pkg_emit_error("%s has no extension", path);
return (EPKG_FATAL);
Expand Down

0 comments on commit a4a647b

Please sign in to comment.