Skip to content

Commit f3906a6

Browse files
authored
Merge pull request #138 from ruby/claude/fervent-pare-2991fb
Use rb_len_t for String lengths in cgi/escape
2 parents 3168b8c + a0a40a3 commit f3906a6

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

‎ext/cgi/escape/escape.c‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
#include "ruby.h"
22
#include "ruby/encoding.h"
33

4+
#ifndef HAVE_RB_LEN_T
5+
typedef long rb_len_t;
6+
# define RB_LEN_MAX LONG_MAX
7+
#endif
8+
49
RUBY_EXTERN unsigned long ruby_scan_digits(const char *str, ssize_t len, int base, size_t *retlen, int *overflow);
510
RUBY_EXTERN const char ruby_hexdigits[];
611
RUBY_EXTERN const signed char ruby_digit36_to_number_table[];
@@ -32,11 +37,11 @@ preserve_original_state(VALUE orig, VALUE dest)
3237
rb_enc_associate(dest, rb_enc_get(orig));
3338
}
3439

35-
static inline long
40+
static inline rb_len_t
3641
escaped_length(VALUE str)
3742
{
38-
const long len = RSTRING_LEN(str);
39-
if (len >= LONG_MAX / HTML_ESCAPE_MAX_LEN) {
43+
const rb_len_t len = RSTRING_LEN(str);
44+
if (len >= RB_LEN_MAX / HTML_ESCAPE_MAX_LEN) {
4045
ruby_malloc_size_overflow(len, HTML_ESCAPE_MAX_LEN);
4146
}
4247
return len * HTML_ESCAPE_MAX_LEN;
@@ -83,7 +88,7 @@ optimized_unescape_html(VALUE str)
8388
unsigned long charlimit = (strcasecmp(rb_enc_name(enc), "UTF-8") == 0 ? UNICODE_MAX :
8489
strcasecmp(rb_enc_name(enc), "ISO-8859-1") == 0 ? 256 :
8590
128);
86-
long i, j, len, beg = 0;
91+
rb_len_t i, j, len, beg = 0;
8792
size_t clen, plen;
8893
int overflow;
8994
const char *cstr;
@@ -221,7 +226,7 @@ url_unreserved_char(unsigned char c)
221226
static VALUE
222227
optimized_escape(VALUE str, int plus_escape)
223228
{
224-
long i, len, beg = 0;
229+
rb_len_t i, len, beg = 0;
225230
VALUE dest = 0;
226231
const char *cstr;
227232
char buf[4] = {'%'};
@@ -263,7 +268,7 @@ optimized_escape(VALUE str, int plus_escape)
263268
static VALUE
264269
optimized_unescape(VALUE str, VALUE encoding, int unescape_plus)
265270
{
266-
long i, len, beg = 0;
271+
rb_len_t i, len, beg = 0;
267272
VALUE dest = 0;
268273
const char *cstr;
269274
rb_encoding *enc = rb_to_encoding(encoding);

0 commit comments

Comments
 (0)