PATH_MAX includes the terminating NULL byte

This commit is contained in:
emersion 2018-11-08 12:26:09 +01:00
parent fa255aa994
commit b524c35ccf
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48

View file

@ -46,12 +46,13 @@ err:
return NULL; return NULL;
} }
static bool command_from_pid(char cmd[static PATH_MAX + 1], pid_t pid) { static bool command_from_pid(char cmd[static PATH_MAX], pid_t pid) {
#ifdef __linux__ #ifdef __linux__
char link_path[PATH_MAX]; char link_path[PATH_MAX];
snprintf(link_path, sizeof(link_path), "/proc/%d/exe", pid); snprintf(link_path, sizeof(link_path), "/proc/%d/exe", pid);
ssize_t n = readlink(link_path, cmd, PATH_MAX); // PATH_MAX includes the terminating NULL byte
ssize_t n = readlink(link_path, cmd, PATH_MAX - 1);
if (n < 0) { if (n < 0) {
wlr_log_errno(WLR_ERROR, "Failed to readlink() %s", link_path); wlr_log_errno(WLR_ERROR, "Failed to readlink() %s", link_path);
return false; return false;
@ -59,6 +60,7 @@ static bool command_from_pid(char cmd[static PATH_MAX + 1], pid_t pid) {
cmd[n] = '\0'; cmd[n] = '\0';
return true; return true;
#else #else
// TODO: BSDs
return false; return false;
#endif #endif
} }
@ -78,7 +80,7 @@ static bool global_filter(const struct wl_client *client,
#endif #endif
} }
char cmd[PATH_MAX + 1]; char cmd[PATH_MAX];
if (!command_from_pid(cmd, pid)) { if (!command_from_pid(cmd, pid)) {
wlr_log(WLR_ERROR, "Failed to get command path from PID %d", pid); wlr_log(WLR_ERROR, "Failed to get command path from PID %d", pid);
return false; return false;