* xgselect.c (xg_select): Check for size calculation overflow.
[bpt/emacs.git] / src / xgselect.c
1 /* Function for handling the GLib event loop.
2
3 Copyright (C) 2009-2011 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http§://www.gnu.org/licenses/>. */
19
20 #include <config.h>
21
22 #include <setjmp.h>
23 #include "xgselect.h"
24
25 #if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
26
27 #include <glib.h>
28 #include <errno.h>
29 #include <setjmp.h>
30
31 static GPollFD *gfds;
32 static int gfds_size;
33
34 int
35 xg_select (int max_fds, SELECT_TYPE *rfds, SELECT_TYPE *wfds, SELECT_TYPE *efds,
36 EMACS_TIME *timeout)
37 {
38 SELECT_TYPE all_rfds, all_wfds;
39 EMACS_TIME tmo, *tmop = timeout;
40
41 GMainContext *context = g_main_context_default ();
42 int have_wfds = wfds != NULL;
43 int n_gfds = 0, our_tmo = 0, retval = 0, our_fds = 0;
44 int i, nfds, tmo_in_millisec;
45
46 if (rfds) memcpy (&all_rfds, rfds, sizeof (all_rfds));
47 else FD_ZERO (&all_rfds);
48 if (wfds) memcpy (&all_wfds, wfds, sizeof (all_rfds));
49 else FD_ZERO (&all_wfds);
50
51 /* Update event sources in GLib. */
52 g_main_context_pending (context);
53
54 do {
55 if (n_gfds > gfds_size)
56 {
57 int gfds_size_max =
58 min (INT_MAX, min (PTRDIFF_MAX, SIZE_MAX) / sizeof *gfds);
59 int size;
60 if (gfds_size_max / 2 < n_gfds)
61 memory_full (SIZE_MAX);
62 size = 2 * n_gfds;
63 gfds_size = 0;
64 xfree (gfds);
65 gfds = xmalloc (sizeof *gfds * size);
66 gfds_size = size;
67 }
68
69 n_gfds = g_main_context_query (context,
70 G_PRIORITY_LOW,
71 &tmo_in_millisec,
72 gfds,
73 gfds_size);
74 } while (n_gfds > gfds_size);
75
76 for (i = 0; i < n_gfds; ++i)
77 {
78 if (gfds[i].events & G_IO_IN)
79 {
80 FD_SET (gfds[i].fd, &all_rfds);
81 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
82 }
83 if (gfds[i].events & G_IO_OUT)
84 {
85 FD_SET (gfds[i].fd, &all_wfds);
86 if (gfds[i].fd > max_fds) max_fds = gfds[i].fd;
87 have_wfds = 1;
88 }
89 }
90
91 if (tmo_in_millisec >= 0)
92 {
93 EMACS_SET_SECS_USECS (tmo, tmo_in_millisec/1000,
94 1000 * (tmo_in_millisec % 1000));
95 if (!timeout) our_tmo = 1;
96 else
97 {
98 EMACS_TIME difference;
99
100 EMACS_SUB_TIME (difference, tmo, *timeout);
101 if (EMACS_TIME_NEG_P (difference)) our_tmo = 1;
102 }
103
104 if (our_tmo) tmop = &tmo;
105 }
106
107 nfds = select (max_fds+1, &all_rfds, have_wfds ? &all_wfds : NULL,
108 efds, tmop);
109
110 if (nfds < 0)
111 retval = nfds;
112 else if (nfds > 0)
113 {
114 for (i = 0; i < max_fds+1; ++i)
115 {
116 if (FD_ISSET (i, &all_rfds))
117 {
118 if (rfds && FD_ISSET (i, rfds)) ++retval;
119 else ++our_fds;
120 }
121 else if (rfds)
122 FD_CLR (i, rfds);
123
124 if (have_wfds && FD_ISSET (i, &all_wfds))
125 {
126 if (wfds && FD_ISSET (i, wfds)) ++retval;
127 else ++our_fds;
128 }
129 else if (wfds)
130 FD_CLR (i, wfds);
131
132 if (efds && FD_ISSET (i, efds))
133 ++retval;
134 }
135 }
136
137 if (our_fds > 0 || (nfds == 0 && our_tmo))
138 {
139
140 /* If Gtk+ is in use eventually gtk_main_iteration will be called,
141 unless retval is zero. */
142 #ifdef USE_GTK
143 if (retval == 0)
144 #endif
145 while (g_main_context_pending (context))
146 g_main_context_dispatch (context);
147
148 /* To not have to recalculate timeout, return like this. */
149 if (retval == 0)
150 {
151 retval = -1;
152 errno = EINTR;
153 }
154 }
155
156 return retval;
157 }
158 #endif /* USE_GTK || HAVE_GCONF || HAVE_GSETTINGS */
159
160 void
161 xgselect_initialize (void)
162 {
163 #if defined (USE_GTK) || defined (HAVE_GCONF) || defined (HAVE_GSETTINGS)
164 gfds_size = 128;
165 gfds = xmalloc (sizeof (*gfds)*gfds_size);
166 #endif
167 }