]> git.ipfire.org Git - thirdparty/asterisk.git/commitdiff
test.c: Avoid passing -1 to FD_* family of functions.
authorSean Bright <sean@seanbright.com>
Tue, 28 Feb 2023 00:35:11 +0000 (19:35 -0500)
committerGeorge Joseph <gjoseph@digium.com>
Tue, 28 Feb 2023 18:24:43 +0000 (12:24 -0600)
This avoids buffer overflow errors when running tests that capture
output from child processes.

This also corrects a copypasta in an off-nominal error message.

Change-Id: Ib482847a3515364f14c7e7a0c0a4213851ddb10d

main/test.c

index f06b55718bde42bf5b26357128b4a6b3c04d73b0..9eefbd0b254edc9b98b55e5cc02fd195b56a6a04 100644 (file)
@@ -363,7 +363,7 @@ int ast_test_capture_command(struct ast_test_capture *capture, const char *file,
        }
 
        if (pipe(fd2) == -1) {
-               ast_log(LOG_ERROR, "Couldn't open stdout pipe: %s\n", strerror(errno));
+               ast_log(LOG_ERROR, "Couldn't open stderr pipe: %s\n", strerror(errno));
                goto cleanup;
        }
 
@@ -472,7 +472,10 @@ int ast_test_capture_command(struct ast_test_capture *capture, const char *file,
                         */
                        n = select(nfds, &readfds, &writefds, NULL, NULL);
 
-                       if (FD_ISSET(fd0[1], &writefds)) {
+                       /* A version of FD_ISSET() that is tolerant of -1 file descriptors */
+#define SAFE_FD_ISSET(fd, setptr) ((fd) != -1 && FD_ISSET((fd), setptr))
+
+                       if (SAFE_FD_ISSET(fd0[1], &writefds)) {
                                n = write(fd0[1], data, datalen);
                                if (n > 0) {
                                        data += n;
@@ -485,7 +488,7 @@ int ast_test_capture_command(struct ast_test_capture *capture, const char *file,
                                }
                        }
 
-                       if (FD_ISSET(fd1[0], &readfds)) {
+                       if (SAFE_FD_ISSET(fd1[0], &readfds)) {
                                n = read(fd1[0], buf, sizeof(buf));
                                if (n > 0) {
                                        fwrite(buf, sizeof(char), n, out);
@@ -494,7 +497,7 @@ int ast_test_capture_command(struct ast_test_capture *capture, const char *file,
                                }
                        }
 
-                       if (FD_ISSET(fd2[0], &readfds)) {
+                       if (SAFE_FD_ISSET(fd2[0], &readfds)) {
                                n = read(fd2[0], buf, sizeof(buf));
                                if (n > 0) {
                                        fwrite(buf, sizeof(char), n, err);
@@ -502,6 +505,8 @@ int ast_test_capture_command(struct ast_test_capture *capture, const char *file,
                                        zclose(fd2[0]);
                                }
                        }
+
+#undef SAFE_FD_ISSET
                }
                status = 1;