Skip to content

Commit f51b038

Browse files
committed
Narrow ALLOCV region for shrunk words
1 parent 06bcfb2 commit f51b038

2 files changed

Lines changed: 20 additions & 8 deletions

File tree

ext/date/date_parse.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,6 @@ VALUE
413413
date_zone_to_diff(VALUE str)
414414
{
415415
VALUE offset = Qnil;
416-
VALUE vbuf = 0;
417416
long l = RSTRING_LEN(str);
418417
const char *s = RSTRING_PTR(str);
419418

@@ -439,16 +438,27 @@ date_zone_to_diff(VALUE str)
439438
l -= w;
440439
dst = 1;
441440
}
441+
442442
{
443+
const char *zn = s;
443444
long sl = shrunk_size(s, l);
444-
if (sl > 0 && sl <= MAX_WORD_LENGTH) {
445+
VALUE vbuf = 0;
446+
const struct zone *z = 0;
447+
448+
if (sl <= 0) {
449+
sl = l;
450+
}
451+
else if (sl <= MAX_WORD_LENGTH) {
445452
char *d = ALLOCV_N(char, vbuf, sl);
446-
l = shrink_space(d, s, l);
447-
s = d;
453+
sl = shrink_space(d, s, l);
454+
zn = d;
448455
}
449-
}
450-
if (l > 0 && l <= MAX_WORD_LENGTH) {
451-
const struct zone *z = zonetab(s, (unsigned int)l);
456+
457+
if (sl > 0 && sl <= MAX_WORD_LENGTH) {
458+
z = zonetab(zn, (unsigned int)sl);
459+
}
460+
ALLOCV_END(vbuf);
461+
452462
if (z) {
453463
int d = z->offset;
454464
if (dst)
@@ -457,6 +467,7 @@ date_zone_to_diff(VALUE str)
457467
goto ok;
458468
}
459469
}
470+
460471
{
461472
char *p;
462473
int sign = 0;
@@ -542,7 +553,6 @@ date_zone_to_diff(VALUE str)
542553
}
543554
RB_GC_GUARD(str);
544555
ok:
545-
ALLOCV_END(vbuf);
546556
return offset;
547557
}
548558

test/date/test_date_parse.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ def test__parse
132132
[['19990523235521.123[-9]',false],[1999,5,23,23,55,21,'-9',-(9*3600),nil], __LINE__],
133133
[['19990523235521.123[+9]',false],[1999,5,23,23,55,21,'+9',+(9*3600),nil], __LINE__],
134134
[['19990523235521.123[9]',false],[1999,5,23,23,55,21,'9',+(9*3600),nil], __LINE__],
135+
[['19990523235521.123[9 ]',false],[1999,5,23,23,55,21,'9 ',+(9*3600),nil], __LINE__],
135136
[['19990523235521.123[-9.50]',false],[1999,5,23,23,55,21,'-9.50',-(9*3600+30*60),nil], __LINE__],
136137
[['19990523235521.123[+9.50]',false],[1999,5,23,23,55,21,'+9.50',+(9*3600+30*60),nil], __LINE__],
137138
[['19990523235521.123[-5:EST]',false],[1999,5,23,23,55,21,'EST',-5*3600,nil], __LINE__],
@@ -140,6 +141,7 @@ def test__parse
140141
[['235521.123',false],[nil,nil,nil,23,55,21,nil,nil,nil], __LINE__],
141142
[['235521.123[-9]',false],[nil,nil,nil,23,55,21,'-9',-9*3600,nil], __LINE__],
142143
[['235521.123[+9]',false],[nil,nil,nil,23,55,21,'+9',+9*3600,nil], __LINE__],
144+
[['235521.123[-9 ]',false],[nil,nil,nil,23,55,21,'-9 ',-9*3600,nil], __LINE__],
143145
[['235521.123[-5:EST]',false],[nil,nil,nil,23,55,21,'EST',-5*3600,nil], __LINE__],
144146
[['235521.123[+9:JST]',false],[nil,nil,nil,23,55,21,'JST',+9*3600,nil], __LINE__],
145147

0 commit comments

Comments
 (0)