<p>George Joseph <strong>submitted</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/19933">View Change</a></p><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span></span><br></pre><div style="white-space:pre-wrap">Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved; Approved for Submit

</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">test.c: Avoid passing -1 to FD_* family of functions.<br><br>This avoids buffer overflow errors when running tests that capture<br>output from child processes.<br><br>This also corrects a copypasta in an off-nominal error message.<br><br>Change-Id: Ib482847a3515364f14c7e7a0c0a4213851ddb10d<br>---<br>M main/test.c<br>1 file changed, 23 insertions(+), 4 deletions(-)<br><br></pre>
<pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/main/test.c b/main/test.c</span><br><span>index f06b557..9eefbd0 100644</span><br><span>--- a/main/test.c</span><br><span>+++ b/main/test.c</span><br><span>@@ -363,7 +363,7 @@</span><br><span>       }</span><br><span> </span><br><span>        if (pipe(fd2) == -1) {</span><br><span style="color: hsl(0, 100%, 40%);">-          ast_log(LOG_ERROR, "Couldn't open stdout pipe: %s\n", strerror(errno));</span><br><span style="color: hsl(120, 100%, 40%);">+         ast_log(LOG_ERROR, "Couldn't open stderr pipe: %s\n", strerror(errno));</span><br><span>                goto cleanup;</span><br><span>        }</span><br><span> </span><br><span>@@ -472,7 +472,10 @@</span><br><span>                          */</span><br><span>                  n = select(nfds, &readfds, &writefds, NULL, NULL);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-                  if (FD_ISSET(fd0[1], &writefds)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                        /* A version of FD_ISSET() that is tolerant of -1 file descriptors */</span><br><span style="color: hsl(120, 100%, 40%);">+#define SAFE_FD_ISSET(fd, setptr) ((fd) != -1 && FD_ISSET((fd), setptr))</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+                     if (SAFE_FD_ISSET(fd0[1], &writefds)) {</span><br><span>                          n = write(fd0[1], data, datalen);</span><br><span>                            if (n > 0) {</span><br><span>                                      data += n;</span><br><span>@@ -485,7 +488,7 @@</span><br><span>                             }</span><br><span>                    }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-                   if (FD_ISSET(fd1[0], &readfds)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                 if (SAFE_FD_ISSET(fd1[0], &readfds)) {</span><br><span>                           n = read(fd1[0], buf, sizeof(buf));</span><br><span>                          if (n > 0) {</span><br><span>                                      fwrite(buf, sizeof(char), n, out);</span><br><span>@@ -494,7 +497,7 @@</span><br><span>                             }</span><br><span>                    }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-                   if (FD_ISSET(fd2[0], &readfds)) {</span><br><span style="color: hsl(120, 100%, 40%);">+                 if (SAFE_FD_ISSET(fd2[0], &readfds)) {</span><br><span>                           n = read(fd2[0], buf, sizeof(buf));</span><br><span>                          if (n > 0) {</span><br><span>                                      fwrite(buf, sizeof(char), n, err);</span><br><span>@@ -502,6 +505,8 @@</span><br><span>                                     zclose(fd2[0]);</span><br><span>                              }</span><br><span>                    }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#undef SAFE_FD_ISSET</span><br><span>            }</span><br><span>            status = 1;</span><br><span> </span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/19933">change 19933</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/19933"/><meta itemprop="name" content="View Change"/></div></div>

<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: 18 </div>
<div style="display:none"> Gerrit-Change-Id: Ib482847a3515364f14c7e7a0c0a4213851ddb10d </div>
<div style="display:none"> Gerrit-Change-Number: 19933 </div>
<div style="display:none"> Gerrit-PatchSet: 5 </div>
<div style="display:none"> Gerrit-Owner: Sean Bright <sean@seanbright.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Joshua Colp <jcolp@sangoma.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>