aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPaul Eggert2011-07-07 10:42:28 -0700
committerPaul Eggert2011-07-07 10:42:28 -0700
commit5b8ffbdddd1280515a254c360f67626f0c9ab3c8 (patch)
tree5a7d3f152f774078bb50836c82883ae2f947b319 /src
parent903fe15d9deb28a72075c39dfd6003a2ff1af134 (diff)
downloademacs-5b8ffbdddd1280515a254c360f67626f0c9ab3c8.tar.gz
emacs-5b8ffbdddd1280515a254c360f67626f0c9ab3c8.zip
* bidi.c: Integer signedness and overflow fixes.
(bidi_cache_idx, bidi_cache_last_idx, bidi_cache_fetch_state) (bidi_cache_search, bidi_cache_find_level_change) (bidi_cache_iterator_state, bidi_cache_find, bidi_find_other_level_edge) (bidi_dump_cached_states): Don't arbitrarily limit cache indexes to int; use ptrdiff_t instead. (bidi_cache_size): Use ptrdiff_t rather than size_t, as we prefer signed integers. (elsz): Make it a (signed) constant. (bidi_cache_iterator_state): Check for size-calculation overflow.
Diffstat (limited to 'src')
-rw-r--r--src/ChangeLog11
-rw-r--r--src/bidi.c29
2 files changed, 27 insertions, 13 deletions
diff --git a/src/ChangeLog b/src/ChangeLog
index f2c318fa84a..a6cafedb36c 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,16 @@
12011-07-07 Paul Eggert <eggert@cs.ucla.edu> 12011-07-07 Paul Eggert <eggert@cs.ucla.edu>
2 2
3 * bidi.c: Integer signedness and overflow fixes.
4 (bidi_cache_idx, bidi_cache_last_idx, bidi_cache_fetch_state)
5 (bidi_cache_search, bidi_cache_find_level_change)
6 (bidi_cache_iterator_state, bidi_cache_find, bidi_find_other_level_edge)
7 (bidi_dump_cached_states):
8 Don't arbitrarily limit cache indexes to int; use ptrdiff_t instead.
9 (bidi_cache_size): Use ptrdiff_t rather than size_t, as we prefer
10 signed integers.
11 (elsz): Make it a (signed) constant.
12 (bidi_cache_iterator_state): Check for size-calculation overflow.
13
3 * alloc.c: Integer signedness and overflow fixes. 14 * alloc.c: Integer signedness and overflow fixes.
4 Do not impose an arbitrary 32-bit limit on malloc sizes when debugging. 15 Do not impose an arbitrary 32-bit limit on malloc sizes when debugging.
5 (__malloc_size_t): Default to size_t, not to int. 16 (__malloc_size_t): Default to size_t, not to int.
diff --git a/src/bidi.c b/src/bidi.c
index 469afdb3819..ecdcdd93d66 100644
--- a/src/bidi.c
+++ b/src/bidi.c
@@ -263,10 +263,10 @@ bidi_copy_it (struct bidi_it *to, struct bidi_it *from)
263 263
264#define BIDI_CACHE_CHUNK 200 264#define BIDI_CACHE_CHUNK 200
265static struct bidi_it *bidi_cache; 265static struct bidi_it *bidi_cache;
266static size_t bidi_cache_size = 0; 266static ptrdiff_t bidi_cache_size = 0;
267static size_t elsz = sizeof (struct bidi_it); 267enum { elsz = sizeof (struct bidi_it) };
268static int bidi_cache_idx; /* next unused cache slot */ 268static ptrdiff_t bidi_cache_idx; /* next unused cache slot */
269static int bidi_cache_last_idx; /* slot of last cache hit */ 269static ptrdiff_t bidi_cache_last_idx; /* slot of last cache hit */
270 270
271static inline void 271static inline void
272bidi_cache_reset (void) 272bidi_cache_reset (void)
@@ -288,7 +288,7 @@ bidi_cache_shrink (void)
288} 288}
289 289
290static inline void 290static inline void
291bidi_cache_fetch_state (int idx, struct bidi_it *bidi_it) 291bidi_cache_fetch_state (ptrdiff_t idx, struct bidi_it *bidi_it)
292{ 292{
293 int current_scan_dir = bidi_it->scan_dir; 293 int current_scan_dir = bidi_it->scan_dir;
294 294
@@ -304,10 +304,10 @@ bidi_cache_fetch_state (int idx, struct bidi_it *bidi_it)
304 level less or equal to LEVEL. if LEVEL is -1, disregard the 304 level less or equal to LEVEL. if LEVEL is -1, disregard the
305 resolved levels in cached states. DIR, if non-zero, means search 305 resolved levels in cached states. DIR, if non-zero, means search
306 in that direction from the last cache hit. */ 306 in that direction from the last cache hit. */
307static inline int 307static inline ptrdiff_t
308bidi_cache_search (EMACS_INT charpos, int level, int dir) 308bidi_cache_search (EMACS_INT charpos, int level, int dir)
309{ 309{
310 int i, i_start; 310 ptrdiff_t i, i_start;
311 311
312 if (bidi_cache_idx) 312 if (bidi_cache_idx)
313 { 313 {
@@ -366,12 +366,12 @@ bidi_cache_search (EMACS_INT charpos, int level, int dir)
366 C, searching backwards (DIR = -1) for LEVEL = 2 will return the 366 C, searching backwards (DIR = -1) for LEVEL = 2 will return the
367 index of slot B or A, depending whether BEFORE is, respectively, 367 index of slot B or A, depending whether BEFORE is, respectively,
368 non-zero or zero. */ 368 non-zero or zero. */
369static int 369static ptrdiff_t
370bidi_cache_find_level_change (int level, int dir, int before) 370bidi_cache_find_level_change (int level, int dir, int before)
371{ 371{
372 if (bidi_cache_idx) 372 if (bidi_cache_idx)
373 { 373 {
374 int i = dir ? bidi_cache_last_idx : bidi_cache_idx - 1; 374 ptrdiff_t i = dir ? bidi_cache_last_idx : bidi_cache_idx - 1;
375 int incr = before ? 1 : 0; 375 int incr = before ? 1 : 0;
376 376
377 if (!dir) 377 if (!dir)
@@ -407,7 +407,7 @@ bidi_cache_find_level_change (int level, int dir, int before)
407static inline void 407static inline void
408bidi_cache_iterator_state (struct bidi_it *bidi_it, int resolved) 408bidi_cache_iterator_state (struct bidi_it *bidi_it, int resolved)
409{ 409{
410 int idx; 410 ptrdiff_t idx;
411 411
412 /* We should never cache on backward scans. */ 412 /* We should never cache on backward scans. */
413 if (bidi_it->scan_dir == -1) 413 if (bidi_it->scan_dir == -1)
@@ -420,6 +420,9 @@ bidi_cache_iterator_state (struct bidi_it *bidi_it, int resolved)
420 /* Enlarge the cache as needed. */ 420 /* Enlarge the cache as needed. */
421 if (idx >= bidi_cache_size) 421 if (idx >= bidi_cache_size)
422 { 422 {
423 if (min (PTRDIFF_MAX, SIZE_MAX) / elsz - BIDI_CACHE_CHUNK
424 < bidi_cache_size)
425 memory_full (SIZE_MAX);
423 bidi_cache_size += BIDI_CACHE_CHUNK; 426 bidi_cache_size += BIDI_CACHE_CHUNK;
424 bidi_cache = 427 bidi_cache =
425 (struct bidi_it *) xrealloc (bidi_cache, bidi_cache_size * elsz); 428 (struct bidi_it *) xrealloc (bidi_cache, bidi_cache_size * elsz);
@@ -468,7 +471,7 @@ bidi_cache_iterator_state (struct bidi_it *bidi_it, int resolved)
468static inline bidi_type_t 471static inline bidi_type_t
469bidi_cache_find (EMACS_INT charpos, int level, struct bidi_it *bidi_it) 472bidi_cache_find (EMACS_INT charpos, int level, struct bidi_it *bidi_it)
470{ 473{
471 int i = bidi_cache_search (charpos, level, bidi_it->scan_dir); 474 ptrdiff_t i = bidi_cache_search (charpos, level, bidi_it->scan_dir);
472 475
473 if (i >= 0) 476 if (i >= 0)
474 { 477 {
@@ -1756,7 +1759,7 @@ static void
1756bidi_find_other_level_edge (struct bidi_it *bidi_it, int level, int end_flag) 1759bidi_find_other_level_edge (struct bidi_it *bidi_it, int level, int end_flag)
1757{ 1760{
1758 int dir = end_flag ? -bidi_it->scan_dir : bidi_it->scan_dir; 1761 int dir = end_flag ? -bidi_it->scan_dir : bidi_it->scan_dir;
1759 int idx; 1762 ptrdiff_t idx;
1760 1763
1761 /* Try the cache first. */ 1764 /* Try the cache first. */
1762 if ((idx = bidi_cache_find_level_change (level, dir, end_flag)) >= 0) 1765 if ((idx = bidi_cache_find_level_change (level, dir, end_flag)) >= 0)
@@ -1912,7 +1915,7 @@ void bidi_dump_cached_states (void) EXTERNALLY_VISIBLE;
1912void 1915void
1913bidi_dump_cached_states (void) 1916bidi_dump_cached_states (void)
1914{ 1917{
1915 int i; 1918 ptrdiff_t i;
1916 int ndigits = 1; 1919 int ndigits = 1;
1917 1920
1918 if (bidi_cache_idx == 0) 1921 if (bidi_cache_idx == 0)