* 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.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63854 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2018-07-04 16:10:14 +00:00
Родитель 36f3e5ec85
Коммит b3799b93c2
1 изменённых файлов: 14 добавлений и 8 удалений

Просмотреть файл

@ -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;