aboutsummaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorLibravatar M Stoeckl <code@mstoeckl.com>2019-01-19 10:31:55 -0500
committerLibravatar emersion <contact@emersion.fr>2019-01-19 18:45:25 +0100
commit62260ab56e398aba81fef1a3b6d5f72d6aa6532d (patch)
treea7264e6676381ec9cc5dca6725d88e3257d9d0db /common
parentapply_output_config: remove output_i (diff)
downloadsway-62260ab56e398aba81fef1a3b6d5f72d6aa6532d.tar.gz
sway-62260ab56e398aba81fef1a3b6d5f72d6aa6532d.tar.zst
sway-62260ab56e398aba81fef1a3b6d5f72d6aa6532d.zip
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.c16
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) {
22 size_t line_size = 0; 22 size_t line_size = 0;
23 FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r"); 23 FILE *fp = popen("sway --get-socketpath 2>/dev/null", "r");
24 if (fp) { 24 if (fp) {
25 getline(&line, &line_size, fp); 25 ssize_t nret = getline(&line, &line_size, fp);
26 pclose(fp); 26 pclose(fp);
27 if (line && *line) { 27 if (nret > 0) {
28 // remove trailing newline, if there is one
29 if (line[nret - 1] == '\n') {
30 line[nret - 1] = '\0';
31 }
28 return line; 32 return line;
29 } 33 }
30 } 34 }
@@ -35,9 +39,13 @@ char *get_socketpath(void) {
35 } 39 }
36 fp = popen("i3 --get-socketpath 2>/dev/null", "r"); 40 fp = popen("i3 --get-socketpath 2>/dev/null", "r");
37 if (fp) { 41 if (fp) {
38 getline(&line, &line_size, fp); 42 ssize_t nret = getline(&line, &line_size, fp);
39 pclose(fp); 43 pclose(fp);
40 if (line && *line) { 44 if (nret > 0) {
45 // remove trailing newline, if there is one
46 if (line[nret - 1] == '\n') {
47 line[nret - 1] = '\0';
48 }
41 return line; 49 return line;
42 } 50 }
43 } 51 }