@@ -227,6 +227,7 @@ static VALUE re2_scanner_rewind(VALUE self) {
227
227
re2_scanner *c;
228
228
Data_Get_Struct (self, re2_scanner, c);
229
229
230
+ delete c->input ;
230
231
c->input = new (std::nothrow) re2::StringPiece (StringValuePtr (c->text ));
231
232
c->eof = false ;
232
233
@@ -785,6 +786,10 @@ static VALUE re2_regexp_initialize(int argc, VALUE *argv, VALUE self) {
785
786
re2_pattern *p;
786
787
787
788
rb_scan_args (argc, argv, " 11" , &pattern, &options);
789
+
790
+ /* Ensure pattern is a string. */
791
+ StringValue (pattern);
792
+
788
793
Data_Get_Struct (self, re2_pattern, p);
789
794
790
795
if (RTEST (options)) {
@@ -1341,6 +1346,9 @@ static VALUE re2_regexp_match_p(const VALUE self, VALUE text) {
1341
1346
* c = RE2::Regexp.new('(\w+)').scan("Foo bar baz")
1342
1347
*/
1343
1348
static VALUE re2_regexp_scan (const VALUE self, VALUE text) {
1349
+ /* Ensure text is a string. */
1350
+ StringValue (text);
1351
+
1344
1352
re2_pattern *p;
1345
1353
re2_scanner *c;
1346
1354
@@ -1382,6 +1390,9 @@ static VALUE re2_regexp_scan(const VALUE self, VALUE text) {
1382
1390
*/
1383
1391
static VALUE re2_Replace (VALUE, VALUE str, VALUE pattern,
1384
1392
VALUE rewrite) {
1393
+ /* Ensure rewrite is a string. */
1394
+ StringValue (rewrite);
1395
+
1385
1396
re2_pattern *p;
1386
1397
1387
1398
/* Take a copy of str so it can be modified in-place by
@@ -1397,6 +1408,9 @@ static VALUE re2_Replace(VALUE, VALUE str, VALUE pattern,
1397
1408
return encoded_str_new (str_as_string.data (), str_as_string.size (),
1398
1409
p->pattern ->options ().encoding ());
1399
1410
} else {
1411
+ /* Ensure pattern is a string. */
1412
+ StringValue (pattern);
1413
+
1400
1414
RE2::Replace (&str_as_string, StringValuePtr (pattern),
1401
1415
StringValuePtr (rewrite));
1402
1416
@@ -1422,6 +1436,9 @@ static VALUE re2_Replace(VALUE, VALUE str, VALUE pattern,
1422
1436
*/
1423
1437
static VALUE re2_GlobalReplace (VALUE, VALUE str, VALUE pattern,
1424
1438
VALUE rewrite) {
1439
+ /* Ensure rewrite is a string. */
1440
+ StringValue (rewrite);
1441
+
1425
1442
/* Take a copy of str so it can be modified in-place by
1426
1443
* RE2::GlobalReplace.
1427
1444
*/
@@ -1436,6 +1453,9 @@ static VALUE re2_GlobalReplace(VALUE, VALUE str, VALUE pattern,
1436
1453
return encoded_str_new (str_as_string.data (), str_as_string.size (),
1437
1454
p->pattern ->options ().encoding ());
1438
1455
} else {
1456
+ /* Ensure pattern is a string. */
1457
+ StringValue (pattern);
1458
+
1439
1459
RE2::GlobalReplace (&str_as_string, StringValuePtr (pattern),
1440
1460
StringValuePtr (rewrite));
1441
1461
@@ -1563,13 +1583,23 @@ static VALUE re2_set_initialize(int argc, VALUE *argv, VALUE self) {
1563
1583
static VALUE re2_set_add (VALUE self, VALUE pattern) {
1564
1584
StringValue (pattern);
1565
1585
re2::StringPiece regex (RSTRING_PTR (pattern), RSTRING_LEN (pattern));
1566
- std::string err;
1567
1586
re2_set *s;
1568
1587
Data_Get_Struct (self, re2_set, s);
1569
1588
1570
- int index = s->set ->Add (regex, &err);
1589
+ /* To prevent the memory of the err string leaking when we call rb_raise,
1590
+ * take a copy of it and let it go out of scope.
1591
+ */
1592
+ char msg[100 ];
1593
+ int index ;
1594
+
1595
+ {
1596
+ std::string err;
1597
+ index = s->set ->Add (regex, &err);
1598
+ strncpy (msg, err.c_str (), sizeof (msg));
1599
+ }
1600
+
1571
1601
if (index < 0 ) {
1572
- rb_raise (rb_eArgError, " str rejected by RE2::Set->Add(): %s" , err. c_str () );
1602
+ rb_raise (rb_eArgError, " str rejected by RE2::Set->Add(): %s" , msg );
1573
1603
}
1574
1604
1575
1605
return INT2FIX (index );
0 commit comments