aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorPhilipp Stephani2016-03-02 10:47:27 -0800
committerPaul Eggert2016-03-02 10:48:06 -0800
commitd20d02fef7fd68f03db5abe48548068191dadedf (patch)
treed464114d0e8a3a904313a3b4e77de1c92c6ddd21 /src
parent65f692658e81c940df8b3b315be873840dcef92b (diff)
downloademacs-d20d02fef7fd68f03db5abe48548068191dadedf.tar.gz
emacs-d20d02fef7fd68f03db5abe48548068191dadedf.zip
Use standard checks whenever possible.
This is possible in all functions where we catch signals anyway. * emacs-module.c (module_make_global_ref, module_funcall) (module_copy_string_contents, module_make_string): Use xsignal0 and CHECK macros for argument checks.
Diffstat (limited to 'src')
-rw-r--r--src/emacs-module.c123
1 files changed, 21 insertions, 102 deletions
diff --git a/src/emacs-module.c b/src/emacs-module.c
index 79a077b3cb4..d8f2c1da14e 100644
--- a/src/emacs-module.c
+++ b/src/emacs-module.c
@@ -107,14 +107,12 @@ static enum emacs_funcall_exit module_non_local_exit_check (emacs_env *);
107static void check_main_thread (void); 107static void check_main_thread (void);
108static void finalize_environment (struct emacs_env_private *); 108static void finalize_environment (struct emacs_env_private *);
109static void initialize_environment (emacs_env *, struct emacs_env_private *priv); 109static void initialize_environment (emacs_env *, struct emacs_env_private *priv);
110static void module_args_out_of_range (emacs_env *, Lisp_Object, Lisp_Object);
111static void module_handle_signal (emacs_env *, Lisp_Object); 110static void module_handle_signal (emacs_env *, Lisp_Object);
112static void module_handle_throw (emacs_env *, Lisp_Object); 111static void module_handle_throw (emacs_env *, Lisp_Object);
113static void module_non_local_exit_signal_1 (emacs_env *, Lisp_Object, Lisp_Object); 112static void module_non_local_exit_signal_1 (emacs_env *, Lisp_Object, Lisp_Object);
114static void module_non_local_exit_throw_1 (emacs_env *, Lisp_Object, Lisp_Object); 113static void module_non_local_exit_throw_1 (emacs_env *, Lisp_Object, Lisp_Object);
115static void module_out_of_memory (emacs_env *); 114static void module_out_of_memory (emacs_env *);
116static void module_reset_handlerlist (const int *); 115static void module_reset_handlerlist (const int *);
117static void module_wrong_type (emacs_env *, Lisp_Object, Lisp_Object);
118 116
119/* We used to return NULL when emacs_value was a different type from 117/* We used to return NULL when emacs_value was a different type from
120 Lisp_Object, but nowadays we just use Qnil instead. Although they 118 Lisp_Object, but nowadays we just use Qnil instead. Although they
@@ -269,12 +267,9 @@ module_make_global_ref (emacs_env *env, emacs_value ref)
269 if (i >= 0) 267 if (i >= 0)
270 { 268 {
271 Lisp_Object value = HASH_VALUE (h, i); 269 Lisp_Object value = HASH_VALUE (h, i);
270 verify (EMACS_INT_MAX > MOST_POSITIVE_FIXNUM);
272 EMACS_INT refcount = XFASTINT (value) + 1; 271 EMACS_INT refcount = XFASTINT (value) + 1;
273 if (refcount > MOST_POSITIVE_FIXNUM) 272 if (FIXNUM_OVERFLOW_P (refcount)) xsignal0 (Qoverflow_error);
274 {
275 module_non_local_exit_signal_1 (env, Qoverflow_error, Qnil);
276 return module_nil;
277 }
278 value = make_natnum (refcount); 273 value = make_natnum (refcount);
279 set_hash_value_slot (h, i, value); 274 set_hash_value_slot (h, i, value);
280 } 275 }
@@ -414,6 +409,7 @@ module_funcall (emacs_env *env, emacs_value fun, ptrdiff_t nargs,
414 first arg, because that's what Ffuncall takes. */ 409 first arg, because that's what Ffuncall takes. */
415 Lisp_Object *newargs; 410 Lisp_Object *newargs;
416 USE_SAFE_ALLOCA; 411 USE_SAFE_ALLOCA;
412 if (nargs == PTRDIFF_MAX) xsignal0 (Qoverflow_error);
417 SAFE_ALLOCA_LISP (newargs, nargs + 1); 413 SAFE_ALLOCA_LISP (newargs, nargs + 1);
418 newargs[0] = value_to_lisp (fun); 414 newargs[0] = value_to_lisp (fun);
419 for (ptrdiff_t i = 0; i < nargs; i++) 415 for (ptrdiff_t i = 0; i < nargs; i++)
@@ -460,11 +456,7 @@ module_extract_integer (emacs_env *env, emacs_value n)
460{ 456{
461 MODULE_FUNCTION_BEGIN (0); 457 MODULE_FUNCTION_BEGIN (0);
462 Lisp_Object l = value_to_lisp (n); 458 Lisp_Object l = value_to_lisp (n);
463 if (! INTEGERP (l)) 459 CHECK_NUMBER (l);
464 {
465 module_wrong_type (env, Qintegerp, l);
466 return 0;
467 }
468 return XINT (l); 460 return XINT (l);
469} 461}
470 462
@@ -472,11 +464,7 @@ static emacs_value
472module_make_integer (emacs_env *env, intmax_t n) 464module_make_integer (emacs_env *env, intmax_t n)
473{ 465{
474 MODULE_FUNCTION_BEGIN (module_nil); 466 MODULE_FUNCTION_BEGIN (module_nil);
475 if (! (MOST_NEGATIVE_FIXNUM <= n && n <= MOST_POSITIVE_FIXNUM)) 467 if (FIXNUM_OVERFLOW_P (n)) xsignal0 (Qoverflow_error);
476 {
477 module_non_local_exit_signal_1 (env, Qoverflow_error, Qnil);
478 return module_nil;
479 }
480 return lisp_to_value (make_number (n)); 468 return lisp_to_value (make_number (n));
481} 469}
482 470
@@ -485,11 +473,7 @@ module_extract_float (emacs_env *env, emacs_value f)
485{ 473{
486 MODULE_FUNCTION_BEGIN (0); 474 MODULE_FUNCTION_BEGIN (0);
487 Lisp_Object lisp = value_to_lisp (f); 475 Lisp_Object lisp = value_to_lisp (f);
488 if (! FLOATP (lisp)) 476 CHECK_TYPE (FLOATP (lisp), Qfloatp, lisp);
489 {
490 module_wrong_type (env, Qfloatp, lisp);
491 return 0;
492 }
493 return XFLOAT_DATA (lisp); 477 return XFLOAT_DATA (lisp);
494} 478}
495 479
@@ -506,19 +490,11 @@ module_copy_string_contents (emacs_env *env, emacs_value value, char *buffer,
506{ 490{
507 MODULE_FUNCTION_BEGIN (false); 491 MODULE_FUNCTION_BEGIN (false);
508 Lisp_Object lisp_str = value_to_lisp (value); 492 Lisp_Object lisp_str = value_to_lisp (value);
509 if (! STRINGP (lisp_str)) 493 CHECK_STRING (lisp_str);
510 {
511 module_wrong_type (env, Qstringp, lisp_str);
512 return false;
513 }
514 494
515 Lisp_Object lisp_str_utf8 = ENCODE_UTF_8 (lisp_str); 495 Lisp_Object lisp_str_utf8 = ENCODE_UTF_8 (lisp_str);
516 ptrdiff_t raw_size = SBYTES (lisp_str_utf8); 496 ptrdiff_t raw_size = SBYTES (lisp_str_utf8);
517 if (raw_size == PTRDIFF_MAX) 497 if (raw_size == PTRDIFF_MAX) xsignal0 (Qoverflow_error);
518 {
519 module_non_local_exit_signal_1 (env, Qoverflow_error, Qnil);
520 return false;
521 }
522 ptrdiff_t required_buf_size = raw_size + 1; 498 ptrdiff_t required_buf_size = raw_size + 1;
523 499
524 eassert (length != NULL); 500 eassert (length != NULL);
@@ -534,8 +510,7 @@ module_copy_string_contents (emacs_env *env, emacs_value value, char *buffer,
534 if (*length < required_buf_size) 510 if (*length < required_buf_size)
535 { 511 {
536 *length = required_buf_size; 512 *length = required_buf_size;
537 module_non_local_exit_signal_1 (env, Qargs_out_of_range, Qnil); 513 xsignal0 (Qargs_out_of_range);
538 return false;
539 } 514 }
540 515
541 *length = required_buf_size; 516 *length = required_buf_size;
@@ -548,11 +523,7 @@ static emacs_value
548module_make_string (emacs_env *env, const char *str, ptrdiff_t length) 523module_make_string (emacs_env *env, const char *str, ptrdiff_t length)
549{ 524{
550 MODULE_FUNCTION_BEGIN (module_nil); 525 MODULE_FUNCTION_BEGIN (module_nil);
551 if (length > STRING_BYTES_BOUND) 526 if (length > STRING_BYTES_BOUND) xsignal0 (Qoverflow_error);
552 {
553 module_non_local_exit_signal_1 (env, Qoverflow_error, Qnil);
554 return module_nil;
555 }
556 Lisp_Object lstr = make_unibyte_string (str, length); 527 Lisp_Object lstr = make_unibyte_string (str, length);
557 return lisp_to_value (code_convert_string_norecord (lstr, Qutf_8, false)); 528 return lisp_to_value (code_convert_string_norecord (lstr, Qutf_8, false));
558} 529}
@@ -569,11 +540,7 @@ module_get_user_ptr (emacs_env *env, emacs_value uptr)
569{ 540{
570 MODULE_FUNCTION_BEGIN (NULL); 541 MODULE_FUNCTION_BEGIN (NULL);
571 Lisp_Object lisp = value_to_lisp (uptr); 542 Lisp_Object lisp = value_to_lisp (uptr);
572 if (! USER_PTRP (lisp)) 543 CHECK_TYPE (USER_PTRP (lisp), Quser_ptrp, lisp);
573 {
574 module_wrong_type (env, Quser_ptr, lisp);
575 return NULL;
576 }
577 return XUSER_PTR (lisp)->p; 544 return XUSER_PTR (lisp)->p;
578} 545}
579 546
@@ -582,12 +549,8 @@ module_set_user_ptr (emacs_env *env, emacs_value uptr, void *ptr)
582{ 549{
583 /* FIXME: This function should return bool because it can fail. */ 550 /* FIXME: This function should return bool because it can fail. */
584 MODULE_FUNCTION_BEGIN (); 551 MODULE_FUNCTION_BEGIN ();
585 check_main_thread ();
586 if (module_non_local_exit_check (env) != emacs_funcall_exit_return)
587 return;
588 Lisp_Object lisp = value_to_lisp (uptr); 552 Lisp_Object lisp = value_to_lisp (uptr);
589 if (! USER_PTRP (lisp)) 553 CHECK_TYPE (USER_PTRP (lisp), Quser_ptrp, lisp);
590 module_wrong_type (env, Quser_ptr, lisp);
591 XUSER_PTR (lisp)->p = ptr; 554 XUSER_PTR (lisp)->p = ptr;
592} 555}
593 556
@@ -596,11 +559,7 @@ module_get_user_finalizer (emacs_env *env, emacs_value uptr)
596{ 559{
597 MODULE_FUNCTION_BEGIN (NULL); 560 MODULE_FUNCTION_BEGIN (NULL);
598 Lisp_Object lisp = value_to_lisp (uptr); 561 Lisp_Object lisp = value_to_lisp (uptr);
599 if (! USER_PTRP (lisp)) 562 CHECK_TYPE (USER_PTRP (lisp), Quser_ptrp, lisp);
600 {
601 module_wrong_type (env, Quser_ptr, lisp);
602 return NULL;
603 }
604 return XUSER_PTR (lisp)->finalizer; 563 return XUSER_PTR (lisp)->finalizer;
605} 564}
606 565
@@ -611,8 +570,7 @@ module_set_user_finalizer (emacs_env *env, emacs_value uptr,
611 /* FIXME: This function should return bool because it can fail. */ 570 /* FIXME: This function should return bool because it can fail. */
612 MODULE_FUNCTION_BEGIN (); 571 MODULE_FUNCTION_BEGIN ();
613 Lisp_Object lisp = value_to_lisp (uptr); 572 Lisp_Object lisp = value_to_lisp (uptr);
614 if (! USER_PTRP (lisp)) 573 CHECK_TYPE (USER_PTRP (lisp), Quser_ptrp, lisp);
615 module_wrong_type (env, Quser_ptr, lisp);
616 XUSER_PTR (lisp)->finalizer = fin; 574 XUSER_PTR (lisp)->finalizer = fin;
617} 575}
618 576
@@ -622,19 +580,9 @@ module_vec_set (emacs_env *env, emacs_value vec, ptrdiff_t i, emacs_value val)
622 /* FIXME: This function should return bool because it can fail. */ 580 /* FIXME: This function should return bool because it can fail. */
623 MODULE_FUNCTION_BEGIN (); 581 MODULE_FUNCTION_BEGIN ();
624 Lisp_Object lvec = value_to_lisp (vec); 582 Lisp_Object lvec = value_to_lisp (vec);
625 if (! VECTORP (lvec)) 583 CHECK_VECTOR (lvec);
626 { 584 if (FIXNUM_OVERFLOW_P (i)) xsignal0 (Qoverflow_error);
627 module_wrong_type (env, Qvectorp, lvec); 585 CHECK_RANGED_INTEGER (make_number (i), 0, ASIZE (lvec) - 1);
628 return;
629 }
630 if (! (0 <= i && i < ASIZE (lvec)))
631 {
632 if (MOST_NEGATIVE_FIXNUM <= i && i <= MOST_POSITIVE_FIXNUM)
633 module_args_out_of_range (env, lvec, make_number (i));
634 else
635 module_non_local_exit_signal_1 (env, Qoverflow_error, Qnil);
636 return;
637 }
638 ASET (lvec, i, value_to_lisp (val)); 586 ASET (lvec, i, value_to_lisp (val));
639} 587}
640 588
@@ -643,19 +591,9 @@ module_vec_get (emacs_env *env, emacs_value vec, ptrdiff_t i)
643{ 591{
644 MODULE_FUNCTION_BEGIN (module_nil); 592 MODULE_FUNCTION_BEGIN (module_nil);
645 Lisp_Object lvec = value_to_lisp (vec); 593 Lisp_Object lvec = value_to_lisp (vec);
646 if (! VECTORP (lvec)) 594 CHECK_VECTOR (lvec);
647 { 595 if (FIXNUM_OVERFLOW_P (i)) xsignal0 (Qoverflow_error);
648 module_wrong_type (env, Qvectorp, lvec); 596 CHECK_RANGED_INTEGER (make_number (i), 0, ASIZE (lvec) - 1);
649 return module_nil;
650 }
651 if (! (0 <= i && i < ASIZE (lvec)))
652 {
653 if (MOST_NEGATIVE_FIXNUM <= i && i <= MOST_POSITIVE_FIXNUM)
654 module_args_out_of_range (env, lvec, make_number (i));
655 else
656 module_non_local_exit_signal_1 (env, Qoverflow_error, Qnil);
657 return module_nil;
658 }
659 return lisp_to_value (AREF (lvec, i)); 597 return lisp_to_value (AREF (lvec, i));
660} 598}
661 599
@@ -665,11 +603,7 @@ module_vec_size (emacs_env *env, emacs_value vec)
665 /* FIXME: Return a sentinel value (e.g., -1) on error. */ 603 /* FIXME: Return a sentinel value (e.g., -1) on error. */
666 MODULE_FUNCTION_BEGIN (0); 604 MODULE_FUNCTION_BEGIN (0);
667 Lisp_Object lvec = value_to_lisp (vec); 605 Lisp_Object lvec = value_to_lisp (vec);
668 if (! VECTORP (lvec)) 606 CHECK_VECTOR (lvec);
669 {
670 module_wrong_type (env, Qvectorp, lvec);
671 return 0;
672 }
673 return ASIZE (lvec); 607 return ASIZE (lvec);
674} 608}
675 609
@@ -828,14 +762,6 @@ module_non_local_exit_throw_1 (emacs_env *env, Lisp_Object tag,
828 } 762 }
829} 763}
830 764
831/* Module version of `wrong_type_argument'. */
832static void
833module_wrong_type (emacs_env *env, Lisp_Object predicate, Lisp_Object value)
834{
835 module_non_local_exit_signal_1 (env, Qwrong_type_argument,
836 list2 (predicate, value));
837}
838
839/* Signal an out-of-memory condition to the caller. */ 765/* Signal an out-of-memory condition to the caller. */
840static void 766static void
841module_out_of_memory (emacs_env *env) 767module_out_of_memory (emacs_env *env)
@@ -846,13 +772,6 @@ module_out_of_memory (emacs_env *env)
846 XCDR (Vmemory_signal_data)); 772 XCDR (Vmemory_signal_data));
847} 773}
848 774
849/* Signal arguments are out of range. */
850static void
851module_args_out_of_range (emacs_env *env, Lisp_Object a1, Lisp_Object a2)
852{
853 module_non_local_exit_signal_1 (env, Qargs_out_of_range, list2 (a1, a2));
854}
855
856 775
857/* Value conversion. */ 776/* Value conversion. */
858 777