aboutsummaryrefslogtreecommitdiffstats
path: root/src/image.c
diff options
context:
space:
mode:
authorPaul Eggert2015-11-08 22:47:01 -0800
committerPaul Eggert2015-11-08 22:48:28 -0800
commit1087305574fd61256d66eb0c995f8bb74bd91afe (patch)
tree9f0052e41a56c785575727931ff4abb8e7dfa7e0 /src/image.c
parentbcca6a2a028d05af3cb5b31a5a2c997f3f1f1d31 (diff)
downloademacs-1087305574fd61256d66eb0c995f8bb74bd91afe.tar.gz
emacs-1087305574fd61256d66eb0c995f8bb74bd91afe.zip
Use INT_ADD_WRAPV etc. to check integer overflow
* src/alloc.c (xnmalloc, xnrealloc, xpalloc, Fmake_string): * src/buffer.c (record_overlay_string, overlay_strings): * src/casefiddle.c (casify_object): * src/ccl.c (Fccl_execute_on_string): * src/character.c (char_width, c_string_width, lisp_string_width) (count_size_as_multibyte, string_escape_byte8): * src/coding.c (coding_alloc_by_realloc, produce_chars): * src/data.c (arith_driver): * src/dispnew.c (realloc_glyph_pool, init_display): * src/editfns.c (styled_format): * src/fns.c (Ffillarray): * src/ftfont.c (ftfont_shape_by_flt): * src/gnutls.c (gnutls_hex_string): * src/gtkutil.c (get_utf8_string): * src/image.c (x_to_xcolors, x_detect_edges, png_load_body): * src/keymap.c (Fkey_description): * src/lisp.h (SAFE_ALLOCA_LISP): * src/term.c (encode_terminal_code): * src/tparam.c (tparam1): * src/xselect.c (x_property_data_to_lisp): * src/xsmfns.c (smc_save_yourself_CB): * src/xterm.c (x_term_init): When checking for integer overflow, prefer INT_MULTIPLY_WRAPV to more-complicated code involving division and/or INT_MULTIPLY_OVERFLOW, and similarly for INT_ADD_WRAPV and subtraction and/or INT_ADD_OVERFLOW. * src/casefiddle.c (casify_object): Simplify multibyte size check. * src/character.c: Remove some obsolete ‘#ifdef emacs’s. * src/data.c (arith_driver): Also check for division overflow, as that’s now possible given that the accumulator can now contain any Emacs integer. * src/lisp.h (lisp_word_count): Remove; no longer used.
Diffstat (limited to 'src/image.c')
-rw-r--r--src/image.c20
1 files changed, 13 insertions, 7 deletions
diff --git a/src/image.c b/src/image.c
index 928eb5cfa37..41687eb885c 100644
--- a/src/image.c
+++ b/src/image.c
@@ -4662,13 +4662,16 @@ x_to_xcolors (struct frame *f, struct image *img, bool rgb_p)
4662 int x, y; 4662 int x, y;
4663 XColor *colors, *p; 4663 XColor *colors, *p;
4664 XImagePtr_or_DC ximg; 4664 XImagePtr_or_DC ximg;
4665 ptrdiff_t nbytes;
4665#ifdef HAVE_NTGUI 4666#ifdef HAVE_NTGUI
4666 HGDIOBJ prev; 4667 HGDIOBJ prev;
4667#endif /* HAVE_NTGUI */ 4668#endif /* HAVE_NTGUI */
4668 4669
4669 if (img->height > min (PTRDIFF_MAX, SIZE_MAX) / sizeof *colors / img->width) 4670 if (INT_MULTIPLY_WRAPV (sizeof *colors, img->width, &nbytes)
4671 || INT_MULTIPLY_WRAPV (img->height, nbytes, &nbytes)
4672 || SIZE_MAX < nbytes)
4670 memory_full (SIZE_MAX); 4673 memory_full (SIZE_MAX);
4671 colors = xmalloc (sizeof *colors * img->width * img->height); 4674 colors = xmalloc (nbytes);
4672 4675
4673 /* Get the X image or create a memory device context for IMG. */ 4676 /* Get the X image or create a memory device context for IMG. */
4674 ximg = image_get_x_image_or_dc (f, img, 0, &prev); 4677 ximg = image_get_x_image_or_dc (f, img, 0, &prev);
@@ -4801,15 +4804,17 @@ x_detect_edges (struct frame *f, struct image *img, int *matrix, int color_adjus
4801 XColor *colors = x_to_xcolors (f, img, 1); 4804 XColor *colors = x_to_xcolors (f, img, 1);
4802 XColor *new, *p; 4805 XColor *new, *p;
4803 int x, y, i, sum; 4806 int x, y, i, sum;
4807 ptrdiff_t nbytes;
4804 4808
4805 for (i = sum = 0; i < 9; ++i) 4809 for (i = sum = 0; i < 9; ++i)
4806 sum += eabs (matrix[i]); 4810 sum += eabs (matrix[i]);
4807 4811
4808#define COLOR(A, X, Y) ((A) + (Y) * img->width + (X)) 4812#define COLOR(A, X, Y) ((A) + (Y) * img->width + (X))
4809 4813
4810 if (img->height > min (PTRDIFF_MAX, SIZE_MAX) / sizeof *new / img->width) 4814 if (INT_MULTIPLY_WRAPV (sizeof *new, img->width, &nbytes)
4815 || INT_MULTIPLY_WRAPV (img->height, nbytes, &nbytes))
4811 memory_full (SIZE_MAX); 4816 memory_full (SIZE_MAX);
4812 new = xmalloc (sizeof *new * img->width * img->height); 4817 new = xmalloc (nbytes);
4813 4818
4814 for (y = 0; y < img->height; ++y) 4819 for (y = 0; y < img->height; ++y)
4815 { 4820 {
@@ -5898,6 +5903,7 @@ png_load_body (struct frame *f, struct image *img, struct png_load_context *c)
5898 png_uint_32 row_bytes; 5903 png_uint_32 row_bytes;
5899 bool transparent_p; 5904 bool transparent_p;
5900 struct png_memory_storage tbr; /* Data to be read */ 5905 struct png_memory_storage tbr; /* Data to be read */
5906 ptrdiff_t nbytes;
5901 5907
5902#ifdef USE_CAIRO 5908#ifdef USE_CAIRO
5903 unsigned char *data = 0; 5909 unsigned char *data = 0;
@@ -6102,10 +6108,10 @@ png_load_body (struct frame *f, struct image *img, struct png_load_context *c)
6102 row_bytes = png_get_rowbytes (png_ptr, info_ptr); 6108 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
6103 6109
6104 /* Allocate memory for the image. */ 6110 /* Allocate memory for the image. */
6105 if (height > min (PTRDIFF_MAX, SIZE_MAX) / sizeof *rows 6111 if (INT_MULTIPLY_WRAPV (row_bytes, sizeof *pixels, &nbytes)
6106 || row_bytes > min (PTRDIFF_MAX, SIZE_MAX) / sizeof *pixels / height) 6112 || INT_MULTIPLY_WRAPV (nbytes, height, &nbytes))
6107 memory_full (SIZE_MAX); 6113 memory_full (SIZE_MAX);
6108 c->pixels = pixels = xmalloc (sizeof *pixels * row_bytes * height); 6114 c->pixels = pixels = xmalloc (nbytes);
6109 c->rows = rows = xmalloc (height * sizeof *rows); 6115 c->rows = rows = xmalloc (height * sizeof *rows);
6110 for (i = 0; i < height; ++i) 6116 for (i = 0; i < height; ++i)
6111 rows[i] = pixels + i * row_bytes; 6117 rows[i] = pixels + i * row_bytes;