aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert2019-08-11 16:42:38 -0700
committerPaul Eggert2019-08-11 17:10:48 -0700
commit57fc1a5f7c49fbe7288de6ad567c934db2ceaf96 (patch)
tree3bcf2eac0a4d966dbad0729caf57c22da7fa30c0 /src
parentf01365f62c921407acead13bb350816a313a8c42 (diff)
downloademacs-57fc1a5f7c49fbe7288de6ad567c934db2ceaf96.tar.gz
emacs-57fc1a5f7c49fbe7288de6ad567c934db2ceaf96.zip
Prefer signed when testing for signed overflow
* src/alloc.c (free_cons): * src/casefiddle.c (do_casify_multibyte_string): * src/editfns.c (styled_format): * src/image.c (png_load_body): Use signed arguments to INT_MULTIPLY_WRAPV etc. This doesn’t fix any bugs, but GCC emits better code when all args are signed. Also, this removes the need for an if in free_cons (Bug#37006).
Diffstat (limited to 'src')
-rw-r--r--src/alloc.c5
-rw-r--r--src/casefiddle.c3
-rw-r--r--src/editfns.c4
-rw-r--r--src/image.c4
4 files changed, 8 insertions, 8 deletions
diff --git a/src/alloc.c b/src/alloc.c
index d9022ac46c3..8227feadae5 100644
--- a/src/alloc.c
+++ b/src/alloc.c
@@ -2542,9 +2542,8 @@ free_cons (struct Lisp_Cons *ptr)
2542 ptr->u.s.u.chain = cons_free_list; 2542 ptr->u.s.u.chain = cons_free_list;
2543 ptr->u.s.car = dead_object (); 2543 ptr->u.s.car = dead_object ();
2544 cons_free_list = ptr; 2544 cons_free_list = ptr;
2545 if (consing_until_gc <= 0) 2545 int incr = sizeof *ptr;
2546 consing_until_gc += sizeof *ptr; 2546 if (INT_ADD_WRAPV (consing_until_gc, incr, &consing_until_gc))
2547 else if (INT_ADD_WRAPV (consing_until_gc, sizeof *ptr, &consing_until_gc))
2548 consing_until_gc = OBJECT_CT_MAX; 2547 consing_until_gc = OBJECT_CT_MAX;
2549 gcstat.total_free_conses++; 2548 gcstat.total_free_conses++;
2550} 2549}
diff --git a/src/casefiddle.c b/src/casefiddle.c
index ee292dda9b3..6fcb5852141 100644
--- a/src/casefiddle.c
+++ b/src/casefiddle.c
@@ -265,8 +265,9 @@ do_casify_multibyte_string (struct casing_context *ctx, Lisp_Object obj)
265 265
266 ptrdiff_t size = SCHARS (obj), n; 266 ptrdiff_t size = SCHARS (obj), n;
267 USE_SAFE_ALLOCA; 267 USE_SAFE_ALLOCA;
268 ptrdiff_t casing_str_buf_size = sizeof (struct casing_str_buf);
268 if (INT_MULTIPLY_WRAPV (size, MAX_MULTIBYTE_LENGTH, &n) 269 if (INT_MULTIPLY_WRAPV (size, MAX_MULTIBYTE_LENGTH, &n)
269 || INT_ADD_WRAPV (n, sizeof (struct casing_str_buf), &n)) 270 || INT_ADD_WRAPV (n, casing_str_buf_size, &n))
270 n = PTRDIFF_MAX; 271 n = PTRDIFF_MAX;
271 unsigned char *dst = SAFE_ALLOCA (n); 272 unsigned char *dst = SAFE_ALLOCA (n);
272 unsigned char *dst_end = dst + n; 273 unsigned char *dst_end = dst + n;
diff --git a/src/editfns.c b/src/editfns.c
index 1b33f397110..25f80bedb1c 100644
--- a/src/editfns.c
+++ b/src/editfns.c
@@ -3159,8 +3159,8 @@ styled_format (ptrdiff_t nargs, Lisp_Object *args, bool message)
3159 ptrdiff_t nspec_bound = SCHARS (args[0]) >> 1; 3159 ptrdiff_t nspec_bound = SCHARS (args[0]) >> 1;
3160 3160
3161 /* Allocate the info and discarded tables. */ 3161 /* Allocate the info and discarded tables. */
3162 ptrdiff_t info_size, alloca_size; 3162 ptrdiff_t info_size = sizeof *info, alloca_size;
3163 if (INT_MULTIPLY_WRAPV (nspec_bound, sizeof *info, &info_size) 3163 if (INT_MULTIPLY_WRAPV (nspec_bound, info_size, &info_size)
3164 || INT_ADD_WRAPV (formatlen, info_size, &alloca_size) 3164 || INT_ADD_WRAPV (formatlen, info_size, &alloca_size)
3165 || SIZE_MAX < alloca_size) 3165 || SIZE_MAX < alloca_size)
3166 memory_full (SIZE_MAX); 3166 memory_full (SIZE_MAX);
diff --git a/src/image.c b/src/image.c
index 81d8cb4e2b2..a59be0cd8ff 100644
--- a/src/image.c
+++ b/src/image.c
@@ -6463,7 +6463,6 @@ png_load_body (struct frame *f, struct image *img, struct png_load_context *c)
6463 png_uint_32 row_bytes; 6463 png_uint_32 row_bytes;
6464 bool transparent_p; 6464 bool transparent_p;
6465 struct png_memory_storage tbr; /* Data to be read */ 6465 struct png_memory_storage tbr; /* Data to be read */
6466 ptrdiff_t nbytes;
6467 Emacs_Pix_Container ximg, mask_img = NULL; 6466 Emacs_Pix_Container ximg, mask_img = NULL;
6468 6467
6469 /* Find out what file to load. */ 6468 /* Find out what file to load. */
@@ -6660,7 +6659,8 @@ png_load_body (struct frame *f, struct image *img, struct png_load_context *c)
6660 row_bytes = png_get_rowbytes (png_ptr, info_ptr); 6659 row_bytes = png_get_rowbytes (png_ptr, info_ptr);
6661 6660
6662 /* Allocate memory for the image. */ 6661 /* Allocate memory for the image. */
6663 if (INT_MULTIPLY_WRAPV (row_bytes, sizeof *pixels, &nbytes) 6662 ptrdiff_t nbytes = sizeof *pixels;
6663 if (INT_MULTIPLY_WRAPV (row_bytes, nbytes, &nbytes)
6664 || INT_MULTIPLY_WRAPV (nbytes, height, &nbytes)) 6664 || INT_MULTIPLY_WRAPV (nbytes, height, &nbytes))
6665 memory_full (SIZE_MAX); 6665 memory_full (SIZE_MAX);
6666 c->pixels = pixels = xmalloc (nbytes); 6666 c->pixels = pixels = xmalloc (nbytes);