]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: fix TOCTOU in test-json
authorYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 16 Oct 2024 05:35:32 +0000 (14:35 +0900)
committerLuca Boccassi <luca.boccassi@gmail.com>
Wed, 16 Oct 2024 09:06:57 +0000 (10:06 +0100)
Follow-up for 60ae3b86fb52d545b279e3927d2214462385e734.
Fixes CID#1563782.

src/test/test-json.c

index c4df19e08dcd7aba7fd4ce409d2db4ee012a9c58..8dd5746495ee64a5801fd1418408032f1f6a4116 100644 (file)
@@ -1372,8 +1372,8 @@ TEST(fd_info) {
         v = sd_json_variant_unref(v);
         fd = safe_close(fd);
 
-        if (access("/sys/class/net/lo/uevent", F_OK) >= 0) {
-                ASSERT_OK_ERRNO(fd = open("/sys/class/net/lo/uevent", O_CLOEXEC | O_PATH));
+        fd = open("/sys/class/net/lo/uevent", O_CLOEXEC | O_PATH);
+        if (fd >= 0) {
                 ASSERT_OK(json_variant_new_fd_info(&v, fd));
                 ASSERT_OK(sd_json_variant_dump(v, SD_JSON_FORMAT_PRETTY_AUTO | SD_JSON_FORMAT_COLOR_AUTO, NULL, NULL));
                 v = sd_json_variant_unref(v);
@@ -1381,8 +1381,8 @@ TEST(fd_info) {
         }
 
         /* block device */
-        if (access("/dev/sda", F_OK) >= 0) {
-                ASSERT_OK_ERRNO(fd = open("/dev/sda", O_CLOEXEC | O_PATH));
+        fd = open("/dev/sda", O_CLOEXEC | O_PATH);
+        if (fd >= 0) {
                 ASSERT_OK(json_variant_new_fd_info(&v, fd));
                 ASSERT_OK(sd_json_variant_dump(v, SD_JSON_FORMAT_PRETTY_AUTO | SD_JSON_FORMAT_COLOR_AUTO, NULL, NULL));
                 v = sd_json_variant_unref(v);