ruby-changes:51642
From: nobu <ko1@a...>
Date: Thu, 5 Jul 2018 01:10:19 +0900 (JST)
Subject: [ruby-changes:51642] nobu:r63854 (trunk): signal.c: packed signals
nobu 2018-07-05 01:10:14 +0900 (Thu, 05 Jul 2018) New Revision: 63854 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63854 Log: signal.c: packed signals * signal.c (signals): pack signal names instead of references. * signal.c (signm2signo): also reject too long signal name. [ruby-core:87767] [Bug #14893] reapply r63841 and r63842, which are unrelated to r63758 but had been wrongly reverted by r63852. Modified files: trunk/signal.c Index: signal.c =================================================================== --- signal.c (revision 63853) +++ signal.c (revision 63854) @@ -66,8 +66,11 @@ ruby_atomic_compare_and_swap(rb_atomic_t https://github.com/ruby/ruby/blob/trunk/signal.c#L66 # 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 { https://github.com/ruby/ruby/blob/trunk/signal.c#L205 #ifdef SIGINFO {"INFO", SIGINFO}, #endif - {NULL, 0} }; static const char signame_prefix[3] = "SIG"; @@ -214,7 +216,7 @@ signm2signo(VALUE *sig_ptr, int negative https://github.com/ruby/ruby/blob/trunk/signal.c#L216 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 https://github.com/ruby/ruby/blob/trunk/signal.c#L270 } 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) https://github.com/ruby/ruby/blob/trunk/signal.c#L287 { 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) https://github.com/ruby/ruby/blob/trunk/signal.c#L1390 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; -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/