diff options
author | M Stoeckl <code@mstoeckl.com> | 2019-01-19 10:31:55 -0500 |
---|---|---|
committer | emersion <contact@emersion.fr> | 2019-01-19 18:45:25 +0100 |
commit | 62260ab56e398aba81fef1a3b6d5f72d6aa6532d (patch) | |
tree | a7264e6676381ec9cc5dca6725d88e3257d9d0db /common | |
parent | afac6ced6e127d83503b39102eb8e0db6a9b1c13 (diff) |
Fix backup methods in get_socketpath for IPC client
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.
Diffstat (limited to 'common')
-rw-r--r-- | common/ipc-client.c | 16 |
1 files changed, 12 insertions, 4 deletions
diff --git a/common/ipc-client.c b/common/ipc-client.c index 1e88e71f..79ed3555 100644 --- a/common/ipc-client.c +++ b/common/ipc-client.c @@ -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; } } @@ -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; } } |