diff options
Diffstat (limited to 'src/dispnew.c')
| -rw-r--r-- | src/dispnew.c | 110 |
1 files changed, 72 insertions, 38 deletions
diff --git a/src/dispnew.c b/src/dispnew.c index b2f416701c3..4cc101d98bf 100644 --- a/src/dispnew.c +++ b/src/dispnew.c | |||
| @@ -499,7 +499,10 @@ adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y | |||
| 499 | /* Enlarge MATRIX->rows if necessary. New rows are cleared. */ | 499 | /* Enlarge MATRIX->rows if necessary. New rows are cleared. */ |
| 500 | if (matrix->rows_allocated < dim.height) | 500 | if (matrix->rows_allocated < dim.height) |
| 501 | { | 501 | { |
| 502 | ptrdiff_t size = dim.height * sizeof (struct glyph_row); | 502 | ptrdiff_t size; |
| 503 | if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct glyph_row) < dim.height) | ||
| 504 | memory_full (SIZE_MAX); | ||
| 505 | size = dim.height * sizeof (struct glyph_row); | ||
| 503 | new_rows = dim.height - matrix->rows_allocated; | 506 | new_rows = dim.height - matrix->rows_allocated; |
| 504 | matrix->rows = (struct glyph_row *) xrealloc (matrix->rows, size); | 507 | matrix->rows = (struct glyph_row *) xrealloc (matrix->rows, size); |
| 505 | memset (matrix->rows + matrix->rows_allocated, 0, | 508 | memset (matrix->rows + matrix->rows_allocated, 0, |
| @@ -573,6 +576,9 @@ adjust_glyph_matrix (struct window *w, struct glyph_matrix *matrix, int x, int y | |||
| 573 | struct glyph_row *row = matrix->rows; | 576 | struct glyph_row *row = matrix->rows; |
| 574 | struct glyph_row *end = row + matrix->rows_allocated; | 577 | struct glyph_row *end = row + matrix->rows_allocated; |
| 575 | 578 | ||
| 579 | if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct glyph) < dim.width) | ||
| 580 | memory_full (SIZE_MAX); | ||
| 581 | |||
| 576 | while (row < end) | 582 | while (row < end) |
| 577 | { | 583 | { |
| 578 | row->glyphs[LEFT_MARGIN_AREA] | 584 | row->glyphs[LEFT_MARGIN_AREA] |
| @@ -1217,7 +1223,7 @@ line_draw_cost (struct glyph_matrix *matrix, int vpos) | |||
| 1217 | struct glyph *end = beg + row->used[TEXT_AREA]; | 1223 | struct glyph *end = beg + row->used[TEXT_AREA]; |
| 1218 | int len; | 1224 | int len; |
| 1219 | Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE; | 1225 | Lisp_Object *glyph_table_base = GLYPH_TABLE_BASE; |
| 1220 | int glyph_table_len = GLYPH_TABLE_LENGTH; | 1226 | ptrdiff_t glyph_table_len = GLYPH_TABLE_LENGTH; |
| 1221 | 1227 | ||
| 1222 | /* Ignore trailing and leading spaces if we can. */ | 1228 | /* Ignore trailing and leading spaces if we can. */ |
| 1223 | if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */ | 1229 | if (!FRAME_MUST_WRITE_SPACES (SELECTED_FRAME ())) /* XXX Is SELECTED_FRAME OK here? */ |
| @@ -1391,31 +1397,26 @@ free_glyph_pool (struct glyph_pool *pool) | |||
| 1391 | static int | 1397 | static int |
| 1392 | realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim) | 1398 | realloc_glyph_pool (struct glyph_pool *pool, struct dim matrix_dim) |
| 1393 | { | 1399 | { |
| 1394 | int needed; | 1400 | ptrdiff_t needed; |
| 1395 | int changed_p; | 1401 | int changed_p; |
| 1396 | 1402 | ||
| 1397 | changed_p = (pool->glyphs == 0 | 1403 | changed_p = (pool->glyphs == 0 |
| 1398 | || matrix_dim.height != pool->nrows | 1404 | || matrix_dim.height != pool->nrows |
| 1399 | || matrix_dim.width != pool->ncolumns); | 1405 | || matrix_dim.width != pool->ncolumns); |
| 1400 | 1406 | ||
| 1407 | if (min (PTRDIFF_MAX, SIZE_MAX) / sizeof (struct glyph) / matrix_dim.width | ||
| 1408 | < matrix_dim.height) | ||
| 1409 | memory_full (SIZE_MAX); | ||
| 1410 | |||
| 1401 | /* Enlarge the glyph pool. */ | 1411 | /* Enlarge the glyph pool. */ |
| 1402 | needed = matrix_dim.width * matrix_dim.height; | 1412 | needed = matrix_dim.width; |
| 1413 | needed *= matrix_dim.height; | ||
| 1403 | if (needed > pool->nglyphs) | 1414 | if (needed > pool->nglyphs) |
| 1404 | { | 1415 | { |
| 1405 | ptrdiff_t size = needed * sizeof (struct glyph); | 1416 | ptrdiff_t size = needed * sizeof (struct glyph); |
| 1406 | 1417 | pool->glyphs = (struct glyph *) xrealloc (pool->glyphs, size); | |
| 1407 | if (pool->glyphs) | 1418 | memset (pool->glyphs + pool->nglyphs, 0, |
| 1408 | { | 1419 | size - pool->nglyphs * sizeof (struct glyph)); |
| 1409 | pool->glyphs = (struct glyph *) xrealloc (pool->glyphs, size); | ||
| 1410 | memset (pool->glyphs + pool->nglyphs, 0, | ||
| 1411 | size - pool->nglyphs * sizeof (struct glyph)); | ||
| 1412 | } | ||
| 1413 | else | ||
| 1414 | { | ||
| 1415 | pool->glyphs = (struct glyph *) xmalloc (size); | ||
| 1416 | memset (pool->glyphs, 0, size); | ||
| 1417 | } | ||
| 1418 | |||
| 1419 | pool->nglyphs = needed; | 1420 | pool->nglyphs = needed; |
| 1420 | } | 1421 | } |
| 1421 | 1422 | ||
| @@ -4166,7 +4167,7 @@ struct row_entry | |||
| 4166 | int new_line_number; | 4167 | int new_line_number; |
| 4167 | 4168 | ||
| 4168 | /* Bucket index of this row_entry in the hash table row_table. */ | 4169 | /* Bucket index of this row_entry in the hash table row_table. */ |
| 4169 | int bucket; | 4170 | ptrdiff_t bucket; |
| 4170 | 4171 | ||
| 4171 | /* The row described by this entry. */ | 4172 | /* The row described by this entry. */ |
| 4172 | struct glyph_row *row; | 4173 | struct glyph_row *row; |
| @@ -4180,18 +4181,18 @@ struct row_entry | |||
| 4180 | that we need a larger one. */ | 4181 | that we need a larger one. */ |
| 4181 | 4182 | ||
| 4182 | static struct row_entry *row_entry_pool; | 4183 | static struct row_entry *row_entry_pool; |
| 4183 | static int row_entry_pool_size; | 4184 | static ptrdiff_t row_entry_pool_size; |
| 4184 | 4185 | ||
| 4185 | /* Index of next free entry in row_entry_pool. */ | 4186 | /* Index of next free entry in row_entry_pool. */ |
| 4186 | 4187 | ||
| 4187 | static int row_entry_idx; | 4188 | static ptrdiff_t row_entry_idx; |
| 4188 | 4189 | ||
| 4189 | /* The hash table used during scrolling, and the table's size. This | 4190 | /* The hash table used during scrolling, and the table's size. This |
| 4190 | table is used to quickly identify equal rows in the desired and | 4191 | table is used to quickly identify equal rows in the desired and |
| 4191 | current matrix. */ | 4192 | current matrix. */ |
| 4192 | 4193 | ||
| 4193 | static struct row_entry **row_table; | 4194 | static struct row_entry **row_table; |
| 4194 | static int row_table_size; | 4195 | static ptrdiff_t row_table_size; |
| 4195 | 4196 | ||
| 4196 | /* Vectors of pointers to row_entry structures belonging to the | 4197 | /* Vectors of pointers to row_entry structures belonging to the |
| 4197 | current and desired matrix, and the size of the vectors. */ | 4198 | current and desired matrix, and the size of the vectors. */ |
| @@ -4214,7 +4215,7 @@ static inline struct row_entry * | |||
| 4214 | add_row_entry (struct glyph_row *row) | 4215 | add_row_entry (struct glyph_row *row) |
| 4215 | { | 4216 | { |
| 4216 | struct row_entry *entry; | 4217 | struct row_entry *entry; |
| 4217 | int i = row->hash % row_table_size; | 4218 | ptrdiff_t i = row->hash % row_table_size; |
| 4218 | 4219 | ||
| 4219 | entry = row_table[i]; | 4220 | entry = row_table[i]; |
| 4220 | while (entry && !row_equal_p (entry->row, row, 1)) | 4221 | while (entry && !row_equal_p (entry->row, row, 1)) |
| @@ -4267,9 +4268,10 @@ scrolling_window (struct window *w, int header_line_p) | |||
| 4267 | struct glyph_matrix *desired_matrix = w->desired_matrix; | 4268 | struct glyph_matrix *desired_matrix = w->desired_matrix; |
| 4268 | struct glyph_matrix *current_matrix = w->current_matrix; | 4269 | struct glyph_matrix *current_matrix = w->current_matrix; |
| 4269 | int yb = window_text_bottom_y (w); | 4270 | int yb = window_text_bottom_y (w); |
| 4270 | int i, j, first_old, first_new, last_old, last_new; | 4271 | ptrdiff_t i; |
| 4271 | int nruns, n, run_idx; | 4272 | int j, first_old, first_new, last_old, last_new; |
| 4272 | ptrdiff_t nbytes; | 4273 | int nruns, run_idx; |
| 4274 | ptrdiff_t n, nbytes; | ||
| 4273 | struct row_entry *entry; | 4275 | struct row_entry *entry; |
| 4274 | struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w))); | 4276 | struct redisplay_interface *rif = FRAME_RIF (XFRAME (WINDOW_FRAME (w))); |
| 4275 | 4277 | ||
| @@ -4354,45 +4356,77 @@ scrolling_window (struct window *w, int header_line_p) | |||
| 4354 | if (last_new == first_new) | 4356 | if (last_new == first_new) |
| 4355 | return 0; | 4357 | return 0; |
| 4356 | 4358 | ||
| 4359 | /* Check for integer overflow in xrealloc size calculation. | ||
| 4360 | |||
| 4361 | If next_almost_prime checks (N) for divisibility by 2..10, then | ||
| 4362 | it can return at most N + 10, e.g., next_almost_prime (1) == 11. | ||
| 4363 | So, set next_almost_prime_increment_max to 10. | ||
| 4364 | |||
| 4365 | It's just a coincidence that next_almost_prime_increment_max == | ||
| 4366 | NEXT_ALMOST_PRIME_LIMIT - 1. If NEXT_ALMOST_PRIME_LIMIT were | ||
| 4367 | 13, then next_almost_prime_increment_max would be 14, e.g., | ||
| 4368 | because next_almost_prime (113) would be 127. */ | ||
| 4369 | { | ||
| 4370 | verify (NEXT_ALMOST_PRIME_LIMIT == 11); | ||
| 4371 | enum { next_almost_prime_increment_max = 10 }; | ||
| 4372 | ptrdiff_t alloc_max = min (PTRDIFF_MAX, SIZE_MAX); | ||
| 4373 | ptrdiff_t row_table_max = | ||
| 4374 | ((alloc_max - next_almost_prime_increment_max) | ||
| 4375 | / (3 * sizeof *row_table)); | ||
| 4376 | ptrdiff_t row_entry_pool_max = alloc_max / sizeof *row_entry_pool; | ||
| 4377 | int n_max = min (INT_MAX, min (row_table_max, row_entry_pool_max)); | ||
| 4378 | ptrdiff_t old_lines_max = alloc_max / sizeof *old_lines; | ||
| 4379 | int current_nrows_max = min (n_max - desired_matrix->nrows, old_lines_max); | ||
| 4380 | int desired_nrows_max = | ||
| 4381 | min (INT_MAX, | ||
| 4382 | alloc_max / max (sizeof *new_lines, | ||
| 4383 | max (sizeof *runs, sizeof *run_pool))); | ||
| 4384 | if (current_nrows_max < current_matrix->nrows | ||
| 4385 | || desired_nrows_max < desired_matrix->nrows) | ||
| 4386 | memory_full (SIZE_MAX); | ||
| 4387 | } | ||
| 4388 | |||
| 4357 | /* Reallocate vectors, tables etc. if necessary. */ | 4389 | /* Reallocate vectors, tables etc. if necessary. */ |
| 4358 | 4390 | ||
| 4359 | if (current_matrix->nrows > old_lines_size) | 4391 | if (current_matrix->nrows > old_lines_size) |
| 4360 | { | 4392 | { |
| 4361 | old_lines_size = current_matrix->nrows; | 4393 | nbytes = current_matrix->nrows * sizeof *old_lines; |
| 4362 | nbytes = old_lines_size * sizeof *old_lines; | ||
| 4363 | old_lines = (struct row_entry **) xrealloc (old_lines, nbytes); | 4394 | old_lines = (struct row_entry **) xrealloc (old_lines, nbytes); |
| 4395 | old_lines_size = current_matrix->nrows; | ||
| 4364 | } | 4396 | } |
| 4365 | 4397 | ||
| 4366 | if (desired_matrix->nrows > new_lines_size) | 4398 | if (desired_matrix->nrows > new_lines_size) |
| 4367 | { | 4399 | { |
| 4368 | new_lines_size = desired_matrix->nrows; | 4400 | nbytes = desired_matrix->nrows * sizeof *new_lines; |
| 4369 | nbytes = new_lines_size * sizeof *new_lines; | ||
| 4370 | new_lines = (struct row_entry **) xrealloc (new_lines, nbytes); | 4401 | new_lines = (struct row_entry **) xrealloc (new_lines, nbytes); |
| 4402 | new_lines_size = desired_matrix->nrows; | ||
| 4371 | } | 4403 | } |
| 4372 | 4404 | ||
| 4373 | n = desired_matrix->nrows + current_matrix->nrows; | 4405 | n = desired_matrix->nrows; |
| 4374 | if (3 * n > row_table_size) | 4406 | n += current_matrix->nrows; |
| 4407 | if (row_table_size / 3 < n) | ||
| 4375 | { | 4408 | { |
| 4376 | row_table_size = next_almost_prime (3 * n); | 4409 | ptrdiff_t size = next_almost_prime (3 * n); |
| 4377 | nbytes = row_table_size * sizeof *row_table; | 4410 | nbytes = size * sizeof *row_table; |
| 4378 | row_table = (struct row_entry **) xrealloc (row_table, nbytes); | 4411 | row_table = (struct row_entry **) xrealloc (row_table, nbytes); |
| 4412 | row_table_size = size; | ||
| 4379 | memset (row_table, 0, nbytes); | 4413 | memset (row_table, 0, nbytes); |
| 4380 | } | 4414 | } |
| 4381 | 4415 | ||
| 4382 | if (n > row_entry_pool_size) | 4416 | if (n > row_entry_pool_size) |
| 4383 | { | 4417 | { |
| 4384 | row_entry_pool_size = n; | 4418 | nbytes = n * sizeof *row_entry_pool; |
| 4385 | nbytes = row_entry_pool_size * sizeof *row_entry_pool; | ||
| 4386 | row_entry_pool = (struct row_entry *) xrealloc (row_entry_pool, nbytes); | 4419 | row_entry_pool = (struct row_entry *) xrealloc (row_entry_pool, nbytes); |
| 4420 | row_entry_pool_size = n; | ||
| 4387 | } | 4421 | } |
| 4388 | 4422 | ||
| 4389 | if (desired_matrix->nrows > runs_size) | 4423 | if (desired_matrix->nrows > runs_size) |
| 4390 | { | 4424 | { |
| 4391 | runs_size = desired_matrix->nrows; | 4425 | nbytes = desired_matrix->nrows * sizeof *runs; |
| 4392 | nbytes = runs_size * sizeof *runs; | ||
| 4393 | runs = (struct run **) xrealloc (runs, nbytes); | 4426 | runs = (struct run **) xrealloc (runs, nbytes); |
| 4394 | nbytes = runs_size * sizeof *run_pool; | 4427 | nbytes = desired_matrix->nrows * sizeof *run_pool; |
| 4395 | run_pool = (struct run *) xrealloc (run_pool, nbytes); | 4428 | run_pool = (struct run *) xrealloc (run_pool, nbytes); |
| 4429 | runs_size = desired_matrix->nrows; | ||
| 4396 | } | 4430 | } |
| 4397 | 4431 | ||
| 4398 | nruns = run_idx = 0; | 4432 | nruns = run_idx = 0; |