aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert2017-06-21 17:18:30 -0700
committerPaul Eggert2017-06-21 17:27:58 -0700
commit35caaf713fb3c3e5e826e52b37e1b389c6bd9cce (patch)
tree958d4ccfe87ae8bfb3b8537ec251a971a2137fa8 /src
parent07a3386fbb4e180f017fab11ea963f4f34493f3c (diff)
downloademacs-35caaf713fb3c3e5e826e52b37e1b389c6bd9cce.tar.gz
emacs-35caaf713fb3c3e5e826e52b37e1b389c6bd9cce.zip
Limit bidi_find_bracket_pairs to MAX_ALLOCA
* src/bidi.c (MAX_BPA_STACK): Now a constant, not a macro. Shrink it to allow for the two struct bidi_it objects in the same frame. (PUSH_BPA_STACK): Avoid integer overflow with enormous bidi cache. (bidi_find_bracket_pairs): Use compile-time check instead of runtime.
Diffstat (limited to 'src')
-rw-r--r--src/bidi.c14
1 files changed, 7 insertions, 7 deletions
diff --git a/src/bidi.c b/src/bidi.c
index dce0bf695f6..e34da778ba0 100644
--- a/src/bidi.c
+++ b/src/bidi.c
@@ -565,9 +565,7 @@ bidi_copy_it (struct bidi_it *to, struct bidi_it *from)
565 RTL characters in the offending line of text. */ 565 RTL characters in the offending line of text. */
566/* Do we need to allow customization of this limit? */ 566/* Do we need to allow customization of this limit? */
567#define BIDI_CACHE_MAX_ELTS_PER_SLOT 50000 567#define BIDI_CACHE_MAX_ELTS_PER_SLOT 50000
568#if BIDI_CACHE_CHUNK >= BIDI_CACHE_MAX_ELTS_PER_SLOT 568verify (BIDI_CACHE_CHUNK < BIDI_CACHE_MAX_ELTS_PER_SLOT);
569# error BIDI_CACHE_CHUNK must be less than BIDI_CACHE_MAX_ELTS_PER_SLOT
570#endif
571static ptrdiff_t bidi_cache_max_elts = BIDI_CACHE_MAX_ELTS_PER_SLOT; 569static ptrdiff_t bidi_cache_max_elts = BIDI_CACHE_MAX_ELTS_PER_SLOT;
572static struct bidi_it *bidi_cache; 570static struct bidi_it *bidi_cache;
573static ptrdiff_t bidi_cache_size = 0; 571static ptrdiff_t bidi_cache_size = 0;
@@ -2468,9 +2466,11 @@ typedef struct bpa_stack_entry {
2468 unsigned flags : 2; 2466 unsigned flags : 2;
2469} bpa_stack_entry; 2467} bpa_stack_entry;
2470 2468
2471/* With MAX_ALLOCA of 16KB, this should allow at least 1K slots in the 2469/* Allow for the two struct bidi_it objects too, since they can be big.
2470 With MAX_ALLOCA of 16 KiB, this should allow at least 900 slots in the
2472 BPA stack, which should be more than enough for actual bidi text. */ 2471 BPA stack, which should be more than enough for actual bidi text. */
2473#define MAX_BPA_STACK ((int)max (MAX_ALLOCA / sizeof (bpa_stack_entry), 1)) 2472enum { MAX_BPA_STACK = max (1, ((MAX_ALLOCA - 2 * sizeof (struct bidi_it))
2473 / sizeof (bpa_stack_entry))) };
2474 2474
2475/* UAX#9 says to match opening brackets with the matching closing 2475/* UAX#9 says to match opening brackets with the matching closing
2476 brackets or their canonical equivalents. As of Unicode 8.0, there 2476 brackets or their canonical equivalents. As of Unicode 8.0, there
@@ -2517,7 +2517,7 @@ typedef struct bpa_stack_entry {
2517#define PUSH_BPA_STACK \ 2517#define PUSH_BPA_STACK \
2518 do { \ 2518 do { \
2519 int ch; \ 2519 int ch; \
2520 if (bpa_sp < MAX_BPA_STACK - 1) \ 2520 if (bpa_sp < MAX_BPA_STACK - 1 && bidi_cache_last_idx <= INT_MAX) \
2521 { \ 2521 { \
2522 bpa_sp++; \ 2522 bpa_sp++; \
2523 ch = CANONICAL_EQU (bidi_it->ch); \ 2523 ch = CANONICAL_EQU (bidi_it->ch); \
@@ -2563,7 +2563,7 @@ bidi_find_bracket_pairs (struct bidi_it *bidi_it)
2563 ptrdiff_t pairing_pos; 2563 ptrdiff_t pairing_pos;
2564 int idx_at_entry = bidi_cache_idx; 2564 int idx_at_entry = bidi_cache_idx;
2565 2565
2566 eassert (MAX_BPA_STACK >= 100); 2566 verify (MAX_BPA_STACK >= 100);
2567 bidi_copy_it (&saved_it, bidi_it); 2567 bidi_copy_it (&saved_it, bidi_it);
2568 /* bidi_cache_iterator_state refuses to cache on backward scans, 2568 /* bidi_cache_iterator_state refuses to cache on backward scans,
2569 and bidi_cache_fetch_state doesn't bring scan_dir from the 2569 and bidi_cache_fetch_state doesn't bring scan_dir from the