diff options
author | Demi Marie Obenour <demiobenour@gmail.com> | 2023-03-25 15:27:33 -0400 |
---|---|---|
committer | Demi Marie Obenour <demiobenour@gmail.com> | 2023-03-25 15:27:50 -0400 |
commit | 8935793f1f3751a6aa9d78955c7d6236177986de (patch) | |
tree | 3e2f35d7cc9df6a037a95cb49609a305a088b06e | |
parent | 095255531b90f0b442e6ca41fb3752a058562d07 (diff) |
Add tests for unix socket parsing
Signed-off-by: Demi Marie Obenour <demiobenour@gmail.com>
-rw-r--r-- | src/xcb_util.c | 15 | ||||
-rw-r--r-- | tests/check_public.c | 35 |
2 files changed, 45 insertions, 5 deletions
diff --git a/src/xcb_util.c b/src/xcb_util.c index bf93568..5b1f495 100644 --- a/src/xcb_util.c +++ b/src/xcb_util.c @@ -93,20 +93,25 @@ static int _xcb_parse_display_path_to_socket(const char *name, char **host, char struct stat sbuf; char path[PATH_MAX]; size_t len; - int _screen = 0; + int _screen = 0, res; len = strlen(name); if (len >= sizeof(path)) return 0; memcpy(path, name, len + 1); - if (0 != stat(path, &sbuf)) { + res = stat(path, &sbuf); + if (0 != res) { unsigned long lscreen; - char *dot = strrchr(path, '.'), *endptr; - if (errno != ENOENT || !dot || dot[1] < '0' || dot[1] > '9') + char *dot, *endptr; + if (res != -1 || (errno != ENOENT && errno != ENOTDIR)) + return 0; + dot = strrchr(path, '.'); + if (!dot || dot[1] < '1' || dot[1] > '9') return 0; *dot = '\0'; + errno = 0; lscreen = strtoul(dot + 1, &endptr, 10); - if (lscreen > INT_MAX || !endptr || *endptr) + if (lscreen > INT_MAX || !endptr || *endptr || errno) return 0; if (0 != stat(path, &sbuf)) return 0; diff --git a/tests/check_public.c b/tests/check_public.c index aed40c8..3caac1e 100644 --- a/tests/check_public.c +++ b/tests/check_public.c @@ -1,6 +1,10 @@ #include <check.h> +#include <stdio.h> #include <string.h> #include <stdlib.h> +#ifdef __unix__ +#include <unistd.h> +#endif #include "check_suites.h" #include "xcb.h" #include "xcbext.h" @@ -97,6 +101,37 @@ static void parse_display_fail(const char *name) START_TEST(parse_display_unix) { +#ifdef __unix__ + char buf[sizeof "/tmp/xcb-test.XXXXXXX"]; + char buf2[sizeof(buf) + 7]; + int r, v; + memcpy(buf, "/tmp/xcb-test.XXXXXXX", sizeof buf); + v = mkstemp(buf); + ck_assert_msg(v >= 0, "cannot create temporary file"); + parse_display_pass(buf, buf, 0, 0); + r = snprintf(buf2, sizeof buf2, "unix:%s", buf); + if (r < 5 || r >= (int)sizeof buf2) { + ck_assert_msg(0, "snprintf() failed (return value %d)", r); + unlink(buf); + return; + } + parse_display_pass(buf2, buf, 0, 0); + r = snprintf(buf2, sizeof buf2, "unix:%s.1", buf); + if (r < 7 || r >= (int)sizeof buf2) { + ck_assert_msg(0, "snprintf() failed (return value %d)", r); + unlink(buf); + return; + } + parse_display_pass(buf2, buf, 0, 1); + r = snprintf(buf2, sizeof buf2, "%s.1", buf); + if (r < 2 || r >= (int)sizeof buf2) { + ck_assert_msg(0, "snprintf() failed (return value %d)", r); + unlink(buf); + return; + } + parse_display_pass(buf2, buf, 0, 1); + unlink(buf); +#endif parse_display_pass(":0", "", 0, 0); parse_display_pass(":1", "", 1, 0); parse_display_pass(":0.1", "", 0, 1); |