Skip to content

Commit

Permalink
Fix backup methods in get_socketpath for IPC client
Browse files Browse the repository at this point in the history
Previously, the success of `getline` was tested by checking if the
buffer it allocates is nonempty and has a nonzero first byte. As
 `getline` does not explicitly zero out its memory buffer, this may
fail (e.g., with AddressSanitizer). Instead, we check that at least one
character was returned on standard output.

Also, trailing newlines (if present) are now removed.
  • Loading branch information
mstoeckl authored and emersion committed Jan 19, 2019
1 parent afac6ce commit 62260ab
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions common/ipc-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,13 @@ char *get_socketpath(void) {
size_t line_size = 0;
FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r");
if (fp) {
getline(&line, &line_size, fp);
ssize_t nret = getline(&line, &line_size, fp);
pclose(fp);
if (line && *line) {
if (nret > 0) {
// remove trailing newline, if there is one
if (line[nret - 1] == '\n') {
line[nret - 1] = '\0';
}
return line;
}
}
Expand All @@ -35,9 +39,13 @@ char *get_socketpath(void) {
}
fp = popen("i3 --get-socketpath 2>/dev/null", "r");
if (fp) {
getline(&line, &line_size, fp);
ssize_t nret = getline(&line, &line_size, fp);
pclose(fp);
if (line && *line) {
if (nret > 0) {
// remove trailing newline, if there is one
if (line[nret - 1] == '\n') {
line[nret - 1] = '\0';
}
return line;
}
}
Expand Down

0 comments on commit 62260ab

Please sign in to comment.