From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Jan =?UTF-8?Q?Dj=C3=A4rv?= Newsgroups: gmane.emacs.bugs Subject: bug#11508: 24.1.50; Off-by-one error in xg_select? Date: Sat, 19 May 2012 10:29:32 +0200 Message-ID: <73ED1C31-12C1-4424-9DEC-632A1F0050D4@swipnet.se> References: <4FB64B05.6020709@cornell.edu> <3B902B7C-C59A-4933-A6AA-E23DB052704C@swipnet.se> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 (Apple Message framework v1278) Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: dough.gmane.org 1337416255 27261 80.91.229.3 (19 May 2012 08:30:55 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sat, 19 May 2012 08:30:55 +0000 (UTC) Cc: 11508@debbugs.gnu.org To: Ken Brown Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Sat May 19 10:30:54 2012 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1SVf3n-0000BB-He for geb-bug-gnu-emacs@m.gmane.org; Sat, 19 May 2012 10:30:47 +0200 Original-Received: from localhost ([::1]:36281 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SVf3m-0002Dh-TG for geb-bug-gnu-emacs@m.gmane.org; Sat, 19 May 2012 04:30:46 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:59247) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SVf3j-0002DR-68 for bug-gnu-emacs@gnu.org; Sat, 19 May 2012 04:30:44 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SVf3g-00071y-PI for bug-gnu-emacs@gnu.org; Sat, 19 May 2012 04:30:42 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:52304) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SVf3g-00071n-Lb for bug-gnu-emacs@gnu.org; Sat, 19 May 2012 04:30:40 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.72) (envelope-from ) id 1SVf42-0003l2-7U for bug-gnu-emacs@gnu.org; Sat, 19 May 2012 04:31:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Jan =?UTF-8?Q?Dj=C3=A4rv?= Original-Sender: debbugs-submit-bounces@debbugs.gnu.org Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 19 May 2012 08:31:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 11508 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 11508-submit@debbugs.gnu.org id=B11508.133741620514377 (code B ref 11508); Sat, 19 May 2012 08:31:02 +0000 Original-Received: (at 11508) by debbugs.gnu.org; 19 May 2012 08:30:05 +0000 Original-Received: from localhost ([127.0.0.1]:33617 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1SVf37-0003jp-9J for submit@debbugs.gnu.org; Sat, 19 May 2012 04:30:05 -0400 Original-Received: from mailout.melmac.se ([62.20.26.67]:61133) by debbugs.gnu.org with esmtp (Exim 4.72) (envelope-from ) id 1SVf35-0003iW-Fh for 11508@debbugs.gnu.org; Sat, 19 May 2012 04:30:04 -0400 Original-Received: from mail01.melmac.se (mail01.melmac.se [62.20.26.80]) by mailout.melmac.se (Postfix) with ESMTP id 15F48918F for <11508@debbugs.gnu.org>; Sat, 19 May 2012 10:29:32 +0200 (CEST) Original-Received: (qmail 16801 invoked by uid 89); 19 May 2012 09:25:15 -0000 Original-Received: from h-46-59-42-18.na.cust.bahnhof.se (HELO coolsville.localdomain) (boel.djarv@bdtv.se@46.59.42.18) by mail01.melmac.se with ESMTPA; 19 May 2012 09:25:15 -0000 Original-Received: from [172.20.199.13] (zeplin [172.20.199.13]) by coolsville.localdomain (Postfix) with ESMTPSA id 865447FA06C; Sat, 19 May 2012 10:29:31 +0200 (CEST) In-Reply-To: <3B902B7C-C59A-4933-A6AA-E23DB052704C@swipnet.se> X-Mailer: Apple Mail (2.1278) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.13 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:60208 Archived-At: Hello. Disregard my previous comment, I didn't look carefully enough. The case where max_fds does not get increased in line 78 or 83 is very = rare. Everytime some X connection is active, GLib will have added at = least one fd (for signal handling). Probably So the question is, is it more effective to give select the exact value = (select works fine if nfds has a higher value than strictly neccessary), = or always increment by one in lines 78 and 83? As the normal case is that there will be several file descriptors in = GLib, I'd say that the code does the most efficent thing most of the = time. Jan D. >=20 > I think you misunderstand how select works. Here is a snippet from = the BSD man page: >=20 > "The first nfds descriptors are checked in > each set; i.e., the descriptors from 0 through nfds-1 in the = descriptor > sets are examined. (Example: If you have set two file descriptors = "4" > and "17", nfds should not be "2", but rather "17 + 1" or "18".) " >=20 >=20 > Jan D. >=20 > 18 maj 2012 kl. 15:13 skrev Ken Brown: >=20 >> I apologize in advance for the noise if I'm misunderstanding = something, >> but it seems to me that there is an error in the calculation of the >> first argument in the call to select in xgselect.c:105. (Line numbers >> refer to the current trunk; xgselect.c was last changed in bzr revno >> 108249.) >>=20 >> The parameter "max_fds" in xg_select, in spite of its name, is = initially >> 1 higher than the maximal file descriptor in the fd_sets in the other >> parameters. If max_fds doesn't get increased in line 78 or 83, then >> line 104 does the wrong thing, causing the first argument to select = in >> line 105 to be 1 higher than it should be. >>=20 >> I think the following patch fixes this. It also renames "max_fds" to >> "fds_lim" to more accurately reflect what it represents. >>=20 >> =3D=3D=3D modified file 'src/xgselect.c' >> --- src/xgselect.c 2012-05-16 02:22:53 +0000 >> +++ src/xgselect.c 2012-05-18 12:28:27 +0000 >> @@ -32,7 +32,7 @@ >> static ptrdiff_t gfds_size; >>=20 >> int >> -xg_select (int max_fds, SELECT_TYPE *rfds, SELECT_TYPE *wfds, = SELECT_TYPE *efds, >> +xg_select (int fds_lim, SELECT_TYPE *rfds, SELECT_TYPE *wfds, = SELECT_TYPE *efds, >> EMACS_TIME *timeout) >> { >> SELECT_TYPE all_rfds, all_wfds; >> @@ -41,10 +41,10 @@ >> GMainContext *context; >> int have_wfds =3D wfds !=3D NULL; >> int n_gfds =3D 0, our_tmo =3D 0, retval =3D 0, our_fds =3D 0; >> - int i, nfds, fds_lim, tmo_in_millisec; >> + int i, nfds, tmo_in_millisec; >>=20 >> if (inhibit_window_system || !display_arg) >> - return select (max_fds, rfds, wfds, efds, timeout); >> + return select (fds_lim, rfds, wfds, efds, timeout); >>=20 >> if (rfds) memcpy (&all_rfds, rfds, sizeof (all_rfds)); >> else FD_ZERO (&all_rfds); >> @@ -75,12 +75,12 @@ >> if (gfds[i].events & G_IO_IN) >> { >> FD_SET (gfds[i].fd, &all_rfds); >> - if (gfds[i].fd > max_fds) max_fds =3D gfds[i].fd; >> + if (gfds[i].fd >=3D fds_lim) fds_lim =3D gfds[i].fd + 1; >> } >> if (gfds[i].events & G_IO_OUT) >> { >> FD_SET (gfds[i].fd, &all_wfds); >> - if (gfds[i].fd > max_fds) max_fds =3D gfds[i].fd; >> + if (gfds[i].fd >=3D fds_lim) fds_lim =3D gfds[i].fd + 1; >> have_wfds =3D 1; >> } >> } >> @@ -101,7 +101,6 @@ >> if (our_tmo) tmop =3D &tmo; >> } >>=20 >> - fds_lim =3D max_fds + 1; >> nfds =3D select (fds_lim, &all_rfds, have_wfds ? &all_wfds : NULL, = efds, tmop); >>=20 >> if (nfds < 0) >>=20 >>=20 >>=20 >>=20 >=20