On Thu, Mar 10, 2022 at 06:15:48PM +0100, Theo Buehler wrote:
> On Thu, Mar 10, 2022 at 06:03:14PM +0100, Claudio Jeker wrote:
> > On Thu, Mar 10, 2022 at 05:54:21PM +0100, Theo Buehler wrote:
> > > On Thu, Mar 10, 2022 at 05:51:46PM +0100, Claudio Jeker wrote:
> > > > On Thu, Mar 10, 2022 at 05:33:28PM +0100, Martin Vahlensieck wrote:
> > > > > Hi
> > > > > 
> > > > > This pulls up and adjusts the check if i exceeds the bounds of pfds.
> > > > > Before it was technically wrong, as i > NPFDS means that the last
> > > > > write (i == NPFDS) was already out of bounds.
> > > >  
> > > > I see no reason to pull up the check but the if condition should indeed 
> > > > be
> > > > greater or equal. One could consider to change this into an assert() 
> > > > but I
> > > > think I stick with the errx().
> > > 
> > > Agreed. ok for the diff that just changes the checks to >=
> > 
> > Actually I was wrong, the check needs to happen at the start of the loop
> > not at the end else it does not work if the list is exactly the number of
> > elements to fill NPFDS. 
> 
> Ah right, we need to bail out when we would actually go past the limit.
> However, I see no reason to fiddle with the timeout before checking.
> So let's move the check to the beginning.

Here you go:

Index: http.c
===================================================================
RCS file: /cvs/src/usr.sbin/rpki-client/http.c,v
retrieving revision 1.53
diff -u -p -r1.53 http.c
--- http.c      10 Feb 2022 11:10:40 -0000      1.53
+++ http.c      10 Mar 2022 17:28:21 -0000
@@ -1809,6 +1809,9 @@ proc_http(char *bind_addr, int fd)
                timeout = INFTIM;
                now = getmonotime();
                LIST_FOREACH(conn, &active, entry) {
+                       if (i >= NPFDS)
+                               errx(1, "too many connections");
+
                        if (conn->io_time == 0)
                                conn->io_time = now + HTTP_IO_TIMEOUT;
 
@@ -1828,10 +1831,11 @@ proc_http(char *bind_addr, int fd)
                        pfds[i].events = conn->events;
                        conn->pfd = &pfds[i];
                        i++;
-                       if (i > NPFDS)
-                               errx(1, "too many connections");
                }
                LIST_FOREACH(conn, &idle, entry) {
+                       if (i >= NPFDS)
+                               errx(1, "too many connections");
+
                        if (conn->idle_time <= now)
                                timeout = 0;
                        else {
@@ -1844,8 +1848,6 @@ proc_http(char *bind_addr, int fd)
                        pfds[i].events = POLLIN;
                        conn->pfd = &pfds[i];
                        i++;
-                       if (i > NPFDS)
-                               errx(1, "too many connections");
                }
 
                if (poll(pfds, i, timeout) == -1) {

Reply via email to