ruby-changes:51629
From: nobu <ko1@a...>
Date: Tue, 3 Jul 2018 22:35:02 +0900 (JST)
Subject: [ruby-changes:51629] nobu:r63841 (trunk): signal.c: packed signals
nobu 2018-07-03 22:34:56 +0900 (Tue, 03 Jul 2018) New Revision: 63841 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=63841 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] Modified files: trunk/signal.c Index: signal.c =================================================================== --- signal.c (revision 63840) +++ signal.c (revision 63841) @@ -62,8 +62,11 @@ ruby_atomic_compare_and_swap(rb_atomic_t https://github.com/ruby/ruby/blob/trunk/signal.c#L62 } #endif +#define FOREACH_SIGNAL(sig, offset) \ + for (sig = siglist + (offset); sig < siglist + numberof(siglist); ++sig) +static const int LONGEST_SIGNAME = 7; /* MIGRATE and RETRACT */ static const struct signals { - const char *signm; + char signm[LONGEST_SIGNAME + 1]; int signo; } siglist [] = { {"EXIT", 0}, @@ -192,7 +195,6 @@ static const struct signals { https://github.com/ruby/ruby/blob/trunk/signal.c#L195 #ifdef SIGINFO {"INFO", SIGINFO}, #endif - {NULL, 0} }; static const char signame_prefix[3] = "SIG"; @@ -204,7 +206,7 @@ signm2signo(VALUE *sig_ptr, int negative https://github.com/ruby/ruby/blob/trunk/signal.c#L206 const struct signals *sigs; VALUE vsig = *sig_ptr; const char *nm; - long len; + long len, nmlen; int prefix = 0; if (RB_SYMBOL_P(vsig)) { @@ -258,9 +260,12 @@ signm2signo(VALUE *sig_ptr, int negative https://github.com/ruby/ruby/blob/trunk/signal.c#L260 } 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; } } @@ -272,9 +277,10 @@ signo2signm(int no) https://github.com/ruby/ruby/blob/trunk/signal.c#L277 { const struct signals *sigs; - for (sigs = siglist; sigs->signm; sigs++) + FOREACH_SIGNAL(sigs, 0) { if (sigs->signo == no) return sigs->signm; + } return 0; } @@ -1415,7 +1421,7 @@ sig_list(void) https://github.com/ruby/ruby/blob/trunk/signal.c#L1421 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/