diff options
author | Nobuyoshi Nakada <[email protected]> | 2020-11-25 21:35:56 +0900 |
---|---|---|
committer | Nobuyoshi Nakada <[email protected]> | 2020-12-26 01:40:51 +0900 |
commit | 6083fed366f4b4bc180e61bfe7efec7a70393911 (patch) | |
tree | 5eb91e4df7678560f19f4d58e66b95834b9ded0b /string.c | |
parent | 92f9f998fb65c70941ea05ed33a7b9f351f308f7 (diff) |
Use `size_t` for `RSTRING_LEN` in String#count
https://hackerone.com/reports/1042722
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/4001
Diffstat (limited to 'string.c')
-rw-r--r-- | string.c | 11 |
1 files changed, 5 insertions, 6 deletions
@@ -7942,6 +7942,7 @@ rb_str_count(int argc, VALUE *argv, VALUE str) char *s, *send; int i; int ascompat; + size_t n = 0; rb_check_arity(argc, 1, UNLIMITED_ARGUMENTS); @@ -7954,7 +7955,6 @@ rb_str_count(int argc, VALUE *argv, VALUE str) (ptstr = RSTRING_PTR(tstr), ONIGENC_IS_ALLOWED_REVERSE_MATCH(enc, (const unsigned char *)ptstr, (const unsigned char *)ptstr+1)) && !is_broken_string(str)) { - int n = 0; int clen; unsigned char c = rb_enc_codepoint_len(ptstr, ptstr+1, &clen, enc); @@ -7964,7 +7964,7 @@ rb_str_count(int argc, VALUE *argv, VALUE str) while (s < send) { if (*(unsigned char*)s++ == c) n++; } - return INT2NUM(n); + return SIZET2NUM(n); } } @@ -7980,13 +7980,12 @@ rb_str_count(int argc, VALUE *argv, VALUE str) if (!s || RSTRING_LEN(str) == 0) return INT2FIX(0); send = RSTRING_END(str); ascompat = rb_enc_asciicompat(enc); - i = 0; while (s < send) { unsigned int c; if (ascompat && (c = *(unsigned char*)s) < 0x80) { if (table[c]) { - i++; + n++; } s++; } @@ -7994,13 +7993,13 @@ rb_str_count(int argc, VALUE *argv, VALUE str) int clen; c = rb_enc_codepoint_len(s, send, &clen, enc); if (tr_find(c, table, del, nodel)) { - i++; + n++; } s += clen; } } - return INT2NUM(i); + return SIZET2NUM(n); } static VALUE |