From 2e78d1083e2ed64ef9e38725deaf691a6ba43e3b Mon Sep 17 00:00:00 2001 From: Sean Bright Date: Mon, 27 Feb 2023 19:35:11 -0500 Subject: [PATCH] test.c: Avoid passing -1 to FD_* family of functions. 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 | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/main/test.c b/main/test.c index f06b55718b..9eefbd0b25 100644 --- a/main/test.c +++ b/main/test.c @@ -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;