diff --git a/signal.c b/signal.c index c781c38c62..d2231fe5ca 100644 --- a/signal.c +++ b/signal.c @@ -66,8 +66,11 @@ ruby_atomic_compare_and_swap(rb_atomic_t *ptr, rb_atomic_t cmp, # define NSIG (_SIGMAX + 1) /* For QNX */ #endif +#define FOREACH_SIGNAL(sig, offset) \ + for (sig = siglist + (offset); sig < siglist + numberof(siglist); ++sig) +enum { LONGEST_SIGNAME = 7 }; /* MIGRATE and RETRACT */ static const struct signals { - const char *signm; + char signm[LONGEST_SIGNAME + 1]; int signo; } siglist [] = { {"EXIT", 0}, @@ -202,7 +205,6 @@ static const struct signals { #ifdef SIGINFO {"INFO", SIGINFO}, #endif - {NULL, 0} }; static const char signame_prefix[3] = "SIG"; @@ -214,7 +216,7 @@ signm2signo(VALUE *sig_ptr, int negative, int exit, int *prefix_ptr) const struct signals *sigs; VALUE vsig = *sig_ptr; const char *nm; - long len; + long len, nmlen; int prefix = 0; if (RB_SYMBOL_P(vsig)) { @@ -268,9 +270,12 @@ signm2signo(VALUE *sig_ptr, int negative, int exit, int *prefix_ptr) } if (prefix_ptr) *prefix_ptr = prefix; - for (sigs = siglist + !exit; sigs->signm; sigs++) { - if (memcmp(sigs->signm, nm + prefix, len - prefix) == 0 && - sigs->signm[len - prefix] == '\0') { + nmlen = len - prefix; + nm += prefix; + if (nmlen > LONGEST_SIGNAME) goto unsupported; + FOREACH_SIGNAL(sigs, !exit) { + if (memcmp(sigs->signm, nm, nmlen) == 0 && + sigs->signm[nmlen] == '\0') { return negative ? -sigs->signo : sigs->signo; } } @@ -282,9 +287,10 @@ signo2signm(int no) { const struct signals *sigs; - for (sigs = siglist; sigs->signm; sigs++) + FOREACH_SIGNAL(sigs, 0) { if (sigs->signo == no) return sigs->signm; + } return 0; } @@ -1384,7 +1390,7 @@ sig_list(void) VALUE h = rb_hash_new(); const struct signals *sigs; - for (sigs = siglist; sigs->signm; sigs++) { + FOREACH_SIGNAL(sigs, 0) { rb_hash_aset(h, rb_fstring_cstr(sigs->signm), INT2FIX(sigs->signo)); } return h;