diff options
| author | Paul Eggert | 2015-11-21 10:38:19 -0800 |
|---|---|---|
| committer | Paul Eggert | 2015-11-21 10:44:04 -0800 |
| commit | 8afaa1321f8088bfb877fe4b6676e8517adb0bb7 (patch) | |
| tree | 7e865f4b42fc44ba38abf7d0188db0aa05096fbd /src | |
| parent | d696d62fea48096680d6d511a71c4df56d00a51f (diff) | |
| download | emacs-8afaa1321f8088bfb877fe4b6676e8517adb0bb7.tar.gz emacs-8afaa1321f8088bfb877fe4b6676e8517adb0bb7.zip | |
Add a few safety checks when ENABLE_CHECKING
This was motivated by the recent addition of module code,
which added some ENABLE_CHECKING-enabled checks that are
useful elsewhere too.
* src/alloc.c (compact_font_cache_entry):
* src/fns.c (sweep_weak_table):
* src/lread.c (oblookup):
Use gc_asize rather than doing it by hand.
* src/emacs-module.c (module_make_global_ref)
(module_free_global_ref, module_vec_size):
Omit assertions that lisp.h now checks.
* src/lisp.h (XFASTINT, ASIZE): In functional implementations,
check that the result is nonnegative. Use eassume, as this
info can help a bit when optimizing production code.
(XSYMBOL) [!USE_LSB_TAG]: Assert that argument is a symbol,
to be consistent with the USE_LSB_TAG case.
(gc_asize): New function, when ASIZE is needed in the gc.
(gc_aset): Use it.
(HASH_TABLE_P): Move definition up, so that it can be used ...
(XHASH_TABLE): ... here, to assert that the arg is a hash table.
Diffstat (limited to 'src')
| -rw-r--r-- | src/alloc.c | 2 | ||||
| -rw-r--r-- | src/emacs-module.c | 12 | ||||
| -rw-r--r-- | src/fns.c | 9 | ||||
| -rw-r--r-- | src/lisp.h | 33 | ||||
| -rw-r--r-- | src/lread.c | 4 |
5 files changed, 30 insertions, 30 deletions
diff --git a/src/alloc.c b/src/alloc.c index ad3f5235480..99f5cdcdea2 100644 --- a/src/alloc.c +++ b/src/alloc.c | |||
| @@ -5339,7 +5339,7 @@ compact_font_cache_entry (Lisp_Object entry) | |||
| 5339 | && !VECTOR_MARKED_P (XFONT_SPEC (XCAR (obj))) | 5339 | && !VECTOR_MARKED_P (XFONT_SPEC (XCAR (obj))) |
| 5340 | && VECTORP (XCDR (obj))) | 5340 | && VECTORP (XCDR (obj))) |
| 5341 | { | 5341 | { |
| 5342 | ptrdiff_t i, size = ASIZE (XCDR (obj)) & ~ARRAY_MARK_FLAG; | 5342 | ptrdiff_t i, size = gc_asize (XCDR (obj)); |
| 5343 | 5343 | ||
| 5344 | /* If font-spec is not marked, most likely all font-entities | 5344 | /* If font-spec is not marked, most likely all font-entities |
| 5345 | are not marked too. But we must be sure that nothing is | 5345 | are not marked too. But we must be sure that nothing is |
diff --git a/src/emacs-module.c b/src/emacs-module.c index 011cc7be914..c8a0d89492a 100644 --- a/src/emacs-module.c +++ b/src/emacs-module.c | |||
| @@ -257,7 +257,6 @@ module_make_global_ref (emacs_env *env, emacs_value ref) | |||
| 257 | check_main_thread (); | 257 | check_main_thread (); |
| 258 | eassert (module_non_local_exit_check (env) == emacs_funcall_exit_return); | 258 | eassert (module_non_local_exit_check (env) == emacs_funcall_exit_return); |
| 259 | MODULE_HANDLE_SIGNALS; | 259 | MODULE_HANDLE_SIGNALS; |
| 260 | eassert (HASH_TABLE_P (Vmodule_refs_hash)); | ||
| 261 | struct Lisp_Hash_Table *h = XHASH_TABLE (Vmodule_refs_hash); | 260 | struct Lisp_Hash_Table *h = XHASH_TABLE (Vmodule_refs_hash); |
| 262 | Lisp_Object new_obj = value_to_lisp (ref); | 261 | Lisp_Object new_obj = value_to_lisp (ref); |
| 263 | EMACS_UINT hashcode; | 262 | EMACS_UINT hashcode; |
| @@ -266,7 +265,6 @@ module_make_global_ref (emacs_env *env, emacs_value ref) | |||
| 266 | if (i >= 0) | 265 | if (i >= 0) |
| 267 | { | 266 | { |
| 268 | Lisp_Object value = HASH_VALUE (h, i); | 267 | Lisp_Object value = HASH_VALUE (h, i); |
| 269 | eassert (NATNUMP (value)); | ||
| 270 | EMACS_INT refcount = XFASTINT (value) + 1; | 268 | EMACS_INT refcount = XFASTINT (value) + 1; |
| 271 | if (refcount > MOST_POSITIVE_FIXNUM) | 269 | if (refcount > MOST_POSITIVE_FIXNUM) |
| 272 | { | 270 | { |
| @@ -293,7 +291,6 @@ module_free_global_ref (emacs_env *env, emacs_value ref) | |||
| 293 | /* FIXME: Wait a minute. Shouldn't this function report an error if | 291 | /* FIXME: Wait a minute. Shouldn't this function report an error if |
| 294 | the hash lookup fails? */ | 292 | the hash lookup fails? */ |
| 295 | MODULE_HANDLE_SIGNALS_VOID; | 293 | MODULE_HANDLE_SIGNALS_VOID; |
| 296 | eassert (HASH_TABLE_P (Vmodule_refs_hash)); | ||
| 297 | struct Lisp_Hash_Table *h = XHASH_TABLE (Vmodule_refs_hash); | 294 | struct Lisp_Hash_Table *h = XHASH_TABLE (Vmodule_refs_hash); |
| 298 | Lisp_Object obj = value_to_lisp (ref); | 295 | Lisp_Object obj = value_to_lisp (ref); |
| 299 | EMACS_UINT hashcode; | 296 | EMACS_UINT hashcode; |
| @@ -302,7 +299,6 @@ module_free_global_ref (emacs_env *env, emacs_value ref) | |||
| 302 | if (i >= 0) | 299 | if (i >= 0) |
| 303 | { | 300 | { |
| 304 | Lisp_Object value = HASH_VALUE (h, i); | 301 | Lisp_Object value = HASH_VALUE (h, i); |
| 305 | eassert (NATNUMP (value)); | ||
| 306 | EMACS_INT refcount = XFASTINT (value) - 1; | 302 | EMACS_INT refcount = XFASTINT (value) - 1; |
| 307 | if (refcount > 0) | 303 | if (refcount > 0) |
| 308 | { | 304 | { |
| @@ -310,10 +306,7 @@ module_free_global_ref (emacs_env *env, emacs_value ref) | |||
| 310 | set_hash_value_slot (h, i, value); | 306 | set_hash_value_slot (h, i, value); |
| 311 | } | 307 | } |
| 312 | else | 308 | else |
| 313 | { | 309 | hash_remove_from_table (h, value); |
| 314 | eassert (refcount == 0); | ||
| 315 | hash_remove_from_table (h, value); | ||
| 316 | } | ||
| 317 | } | 310 | } |
| 318 | } | 311 | } |
| 319 | 312 | ||
| @@ -670,7 +663,6 @@ module_vec_size (emacs_env *env, emacs_value vec) | |||
| 670 | module_wrong_type (env, Qvectorp, lvec); | 663 | module_wrong_type (env, Qvectorp, lvec); |
| 671 | return 0; | 664 | return 0; |
| 672 | } | 665 | } |
| 673 | eassert (ASIZE (lvec) >= 0); | ||
| 674 | return ASIZE (lvec); | 666 | return ASIZE (lvec); |
| 675 | } | 667 | } |
| 676 | 668 | ||
| @@ -894,7 +886,7 @@ finalize_storage (struct emacs_value_storage *storage) | |||
| 894 | } | 886 | } |
| 895 | 887 | ||
| 896 | /* Allocate a new value from STORAGE and stores OBJ in it. Return | 888 | /* Allocate a new value from STORAGE and stores OBJ in it. Return |
| 897 | NULL if allocations fails and use ENV for non local exit reporting. */ | 889 | NULL if allocation fails and use ENV for non local exit reporting. */ |
| 898 | static emacs_value | 890 | static emacs_value |
| 899 | allocate_emacs_value (emacs_env *env, struct emacs_value_storage *storage, | 891 | allocate_emacs_value (emacs_env *env, struct emacs_value_storage *storage, |
| 900 | Lisp_Object obj) | 892 | Lisp_Object obj) |
| @@ -4079,13 +4079,10 @@ hash_clear (struct Lisp_Hash_Table *h) | |||
| 4079 | static bool | 4079 | static bool |
| 4080 | sweep_weak_table (struct Lisp_Hash_Table *h, bool remove_entries_p) | 4080 | sweep_weak_table (struct Lisp_Hash_Table *h, bool remove_entries_p) |
| 4081 | { | 4081 | { |
| 4082 | ptrdiff_t bucket, n; | 4082 | ptrdiff_t n = gc_asize (h->index); |
| 4083 | bool marked; | 4083 | bool marked = false; |
| 4084 | |||
| 4085 | n = ASIZE (h->index) & ~ARRAY_MARK_FLAG; | ||
| 4086 | marked = 0; | ||
| 4087 | 4084 | ||
| 4088 | for (bucket = 0; bucket < n; ++bucket) | 4085 | for (ptrdiff_t bucket = 0; bucket < n; ++bucket) |
| 4089 | { | 4086 | { |
| 4090 | Lisp_Object idx, next, prev; | 4087 | Lisp_Object idx, next, prev; |
| 4091 | 4088 | ||
diff --git a/src/lisp.h b/src/lisp.h index 71dca7201d0..9af13a85557 100644 --- a/src/lisp.h +++ b/src/lisp.h | |||
| @@ -847,7 +847,9 @@ INLINE EMACS_INT | |||
| 847 | INLINE EMACS_INT | 847 | INLINE EMACS_INT |
| 848 | (XFASTINT) (Lisp_Object a) | 848 | (XFASTINT) (Lisp_Object a) |
| 849 | { | 849 | { |
| 850 | return lisp_h_XFASTINT (a); | 850 | EMACS_INT n = lisp_h_XFASTINT (a); |
| 851 | eassume (0 <= n); | ||
| 852 | return n; | ||
| 851 | } | 853 | } |
| 852 | 854 | ||
| 853 | INLINE struct Lisp_Symbol * | 855 | INLINE struct Lisp_Symbol * |
| @@ -915,7 +917,7 @@ XFASTINT (Lisp_Object a) | |||
| 915 | { | 917 | { |
| 916 | EMACS_INT int0 = Lisp_Int0; | 918 | EMACS_INT int0 = Lisp_Int0; |
| 917 | EMACS_INT n = USE_LSB_TAG ? XINT (a) : XLI (a) - (int0 << VALBITS); | 919 | EMACS_INT n = USE_LSB_TAG ? XINT (a) : XLI (a) - (int0 << VALBITS); |
| 918 | eassert (0 <= n); | 920 | eassume (0 <= n); |
| 919 | return n; | 921 | return n; |
| 920 | } | 922 | } |
| 921 | 923 | ||
| @@ -923,6 +925,7 @@ XFASTINT (Lisp_Object a) | |||
| 923 | INLINE struct Lisp_Symbol * | 925 | INLINE struct Lisp_Symbol * |
| 924 | XSYMBOL (Lisp_Object a) | 926 | XSYMBOL (Lisp_Object a) |
| 925 | { | 927 | { |
| 928 | eassert (SYMBOLP (a)); | ||
| 926 | uintptr_t i = (uintptr_t) XUNTAG (a, Lisp_Symbol); | 929 | uintptr_t i = (uintptr_t) XUNTAG (a, Lisp_Symbol); |
| 927 | void *p = (char *) lispsym + i; | 930 | void *p = (char *) lispsym + i; |
| 928 | return p; | 931 | return p; |
| @@ -1536,7 +1539,16 @@ aref_addr (Lisp_Object array, ptrdiff_t idx) | |||
| 1536 | INLINE ptrdiff_t | 1539 | INLINE ptrdiff_t |
| 1537 | ASIZE (Lisp_Object array) | 1540 | ASIZE (Lisp_Object array) |
| 1538 | { | 1541 | { |
| 1539 | return XVECTOR (array)->header.size; | 1542 | ptrdiff_t size = XVECTOR (array)->header.size; |
| 1543 | eassume (0 <= size); | ||
| 1544 | return size; | ||
| 1545 | } | ||
| 1546 | |||
| 1547 | INLINE ptrdiff_t | ||
| 1548 | gc_asize (Lisp_Object array) | ||
| 1549 | { | ||
| 1550 | /* Like ASIZE, but also can be used in the garbage collector. */ | ||
| 1551 | return XVECTOR (array)->header.size & ~ARRAY_MARK_FLAG; | ||
| 1540 | } | 1552 | } |
| 1541 | 1553 | ||
| 1542 | INLINE void | 1554 | INLINE void |
| @@ -1551,7 +1563,7 @@ gc_aset (Lisp_Object array, ptrdiff_t idx, Lisp_Object val) | |||
| 1551 | { | 1563 | { |
| 1552 | /* Like ASET, but also can be used in the garbage collector: | 1564 | /* Like ASET, but also can be used in the garbage collector: |
| 1553 | sweep_weak_table calls set_hash_key etc. while the table is marked. */ | 1565 | sweep_weak_table calls set_hash_key etc. while the table is marked. */ |
| 1554 | eassert (0 <= idx && idx < (ASIZE (array) & ~ARRAY_MARK_FLAG)); | 1566 | eassert (0 <= idx && idx < gc_asize (array)); |
| 1555 | XVECTOR (array)->contents[idx] = val; | 1567 | XVECTOR (array)->contents[idx] = val; |
| 1556 | } | 1568 | } |
| 1557 | 1569 | ||
| @@ -1933,21 +1945,22 @@ struct Lisp_Hash_Table | |||
| 1933 | }; | 1945 | }; |
| 1934 | 1946 | ||
| 1935 | 1947 | ||
| 1948 | INLINE bool | ||
| 1949 | HASH_TABLE_P (Lisp_Object a) | ||
| 1950 | { | ||
| 1951 | return PSEUDOVECTORP (a, PVEC_HASH_TABLE); | ||
| 1952 | } | ||
| 1953 | |||
| 1936 | INLINE struct Lisp_Hash_Table * | 1954 | INLINE struct Lisp_Hash_Table * |
| 1937 | XHASH_TABLE (Lisp_Object a) | 1955 | XHASH_TABLE (Lisp_Object a) |
| 1938 | { | 1956 | { |
| 1957 | eassert (HASH_TABLE_P (a)); | ||
| 1939 | return XUNTAG (a, Lisp_Vectorlike); | 1958 | return XUNTAG (a, Lisp_Vectorlike); |
| 1940 | } | 1959 | } |
| 1941 | 1960 | ||
| 1942 | #define XSET_HASH_TABLE(VAR, PTR) \ | 1961 | #define XSET_HASH_TABLE(VAR, PTR) \ |
| 1943 | (XSETPSEUDOVECTOR (VAR, PTR, PVEC_HASH_TABLE)) | 1962 | (XSETPSEUDOVECTOR (VAR, PTR, PVEC_HASH_TABLE)) |
| 1944 | 1963 | ||
| 1945 | INLINE bool | ||
| 1946 | HASH_TABLE_P (Lisp_Object a) | ||
| 1947 | { | ||
| 1948 | return PSEUDOVECTORP (a, PVEC_HASH_TABLE); | ||
| 1949 | } | ||
| 1950 | |||
| 1951 | /* Value is the key part of entry IDX in hash table H. */ | 1964 | /* Value is the key part of entry IDX in hash table H. */ |
| 1952 | INLINE Lisp_Object | 1965 | INLINE Lisp_Object |
| 1953 | HASH_KEY (struct Lisp_Hash_Table *h, ptrdiff_t idx) | 1966 | HASH_KEY (struct Lisp_Hash_Table *h, ptrdiff_t idx) |
diff --git a/src/lread.c b/src/lread.c index a62a42ad1a9..7f0f1d1f6a8 100644 --- a/src/lread.c +++ b/src/lread.c | |||
| @@ -3941,10 +3941,8 @@ oblookup (Lisp_Object obarray, register const char *ptr, ptrdiff_t size, ptrdiff | |||
| 3941 | Lisp_Object bucket, tem; | 3941 | Lisp_Object bucket, tem; |
| 3942 | 3942 | ||
| 3943 | obarray = check_obarray (obarray); | 3943 | obarray = check_obarray (obarray); |
| 3944 | obsize = ASIZE (obarray); | ||
| 3945 | |||
| 3946 | /* This is sometimes needed in the middle of GC. */ | 3944 | /* This is sometimes needed in the middle of GC. */ |
| 3947 | obsize &= ~ARRAY_MARK_FLAG; | 3945 | obsize = gc_asize (obarray); |
| 3948 | hash = hash_string (ptr, size_byte) % obsize; | 3946 | hash = hash_string (ptr, size_byte) % obsize; |
| 3949 | bucket = AREF (obarray, hash); | 3947 | bucket = AREF (obarray, hash); |
| 3950 | oblookup_last_bucket_number = hash; | 3948 | oblookup_last_bucket_number = hash; |