aboutsummaryrefslogtreecommitdiffstats
path: root/src/data.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/data.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/data.c')
-rw-r--r--src/data.c31
1 files changed, 10 insertions, 21 deletions
diff --git a/src/data.c b/src/data.c
index 4db93f5625f..ccec15f430a 100644
--- a/src/data.c
+++ b/src/data.c
@@ -2631,30 +2631,16 @@ arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args)
2631 switch (code) 2631 switch (code)
2632 { 2632 {
2633 case Aadd: 2633 case Aadd:
2634 if (INT_ADD_OVERFLOW (accum, next)) 2634 overflow |= INT_ADD_WRAPV (accum, next, &accum);
2635 {
2636 overflow = 1;
2637 accum &= INTMASK;
2638 }
2639 accum += next;
2640 break; 2635 break;
2641 case Asub: 2636 case Asub:
2642 if (INT_SUBTRACT_OVERFLOW (accum, next)) 2637 if (! argnum)
2643 { 2638 accum = nargs == 1 ? - next : next;
2644 overflow = 1; 2639 else
2645 accum &= INTMASK; 2640 overflow |= INT_SUBTRACT_WRAPV (accum, next, &accum);
2646 }
2647 accum = argnum ? accum - next : nargs == 1 ? - next : next;
2648 break; 2641 break;
2649 case Amult: 2642 case Amult:
2650 if (INT_MULTIPLY_OVERFLOW (accum, next)) 2643 overflow |= INT_MULTIPLY_WRAPV (accum, next, &accum);
2651 {
2652 EMACS_UINT a = accum, b = next, ab = a * b;
2653 overflow = 1;
2654 accum = ab & INTMASK;
2655 }
2656 else
2657 accum *= next;
2658 break; 2644 break;
2659 case Adiv: 2645 case Adiv:
2660 if (! (argnum || nargs == 1)) 2646 if (! (argnum || nargs == 1))
@@ -2663,7 +2649,10 @@ arith_driver (enum arithop code, ptrdiff_t nargs, Lisp_Object *args)
2663 { 2649 {
2664 if (next == 0) 2650 if (next == 0)
2665 xsignal0 (Qarith_error); 2651 xsignal0 (Qarith_error);
2666 accum /= next; 2652 if (INT_DIVIDE_OVERFLOW (accum, next))
2653 overflow = true;
2654 else
2655 accum /= next;
2667 } 2656 }
2668 break; 2657 break;
2669 case Alogand: 2658 case Alogand: