Don't update size until alloc done.
2011-07-29 Paul Eggert <eggert@cs.ucla.edu>
+ * xgselect.c (xg_select): Check for size calculation overflow.
+ Don't update size until alloc done.
+
* xfns.c: Integer and memory overflow fixes.
(x_encode_text, x_set_name_internal, Fx_change_window_property):
Use ptrdiff_t, not int, to count sizes, since they can exceed
do {
if (n_gfds > gfds_size)
{
- while (n_gfds > gfds_size)
- gfds_size *= 2;
+ int gfds_size_max =
+ min (INT_MAX, min (PTRDIFF_MAX, SIZE_MAX) / sizeof *gfds);
+ int size;
+ if (gfds_size_max / 2 < n_gfds)
+ memory_full (SIZE_MAX);
+ size = 2 * n_gfds;
+ gfds_size = 0;
xfree (gfds);
- gfds = xmalloc (sizeof (*gfds) * gfds_size);
+ gfds = xmalloc (sizeof *gfds * size);
+ gfds_size = size;
}
n_gfds = g_main_context_query (context,