@@ -1356,6 +1356,24 @@ S_is_ssc_worth_it(const RExC_state_t * pRExC_state, const regnode_ssc * ssc)
1356
1356
return TRUE;
1357
1357
}
1358
1358
1359
+ static void
1360
+ release_RExC_state(pTHX_ void *vstate) {
1361
+ RExC_state_t *pRExC_state = (RExC_state_t *)vstate;
1362
+
1363
+ /* Any or all of these might be NULL.
1364
+
1365
+ There's no point in setting them to NULL after the free, since
1366
+ pRExC_state is about to be released.
1367
+ */
1368
+ SvREFCNT_dec(RExC_rx_sv);
1369
+ Safefree(RExC_open_parens);
1370
+ Safefree(RExC_close_parens);
1371
+ Safefree(RExC_logical_to_parno);
1372
+ Safefree(RExC_parno_to_logical);
1373
+
1374
+ Safefree(pRExC_state);
1375
+ }
1376
+
1359
1377
/*
1360
1378
* Perl_re_op_compile - the perl internal RE engine's function to compile a
1361
1379
* regular expression into internal code.
@@ -1437,8 +1455,6 @@ Perl_re_op_compile(pTHX_ SV ** const patternp, int pat_count,
1437
1455
bool recompile = 0;
1438
1456
bool runtime_code = 0;
1439
1457
scan_data_t data;
1440
- RExC_state_t RExC_state;
1441
- RExC_state_t * const pRExC_state = &RExC_state;
1442
1458
1443
1459
#ifdef TRIE_STUDY_OPT
1444
1460
/* search for "restudy" in this file for a detailed explanation */
@@ -1449,25 +1465,28 @@ Perl_re_op_compile(pTHX_ SV ** const patternp, int pat_count,
1449
1465
1450
1466
PERL_ARGS_ASSERT_RE_OP_COMPILE;
1451
1467
1468
+ DEBUG_r(if (!PL_colorset) reginitcolors());
1469
+
1470
+ RExC_state_t *pRExC_state = NULL;
1452
1471
/* Ensure that all members of the pRExC_state is initialized to 0
1453
1472
* at the start of regex compilation. Historically we have had issues
1454
1473
* with people remembering to zero specific members or zeroing them
1455
1474
* too late, etc. Doing it in one place is saner and avoid oversight
1456
1475
* or error. */
1457
- Zero(pRExC_state,1,RExC_state_t);
1476
+ Newxz(pRExC_state, 1, RExC_state_t);
1477
+
1478
+ SAVEDESTRUCTOR_X(release_RExC_state, pRExC_state);
1479
+
1458
1480
DEBUG_r({
1459
1481
/* and then initialize RExC_mysv1 and RExC_mysv2 early so if
1460
1482
* something calls regprop we don't have issues. These variables
1461
- * not being set up properly motivated the use of Zero () to initalize
1483
+ * not being set up properly motivated the use of Newxz () to initalize
1462
1484
* the pRExC_state structure, as there were codepaths under -Uusedl
1463
1485
* that left these unitialized, and non-null as well. */
1464
1486
RExC_mysv1 = sv_newmortal();
1465
1487
RExC_mysv2 = sv_newmortal();
1466
1488
});
1467
1489
1468
- DEBUG_r(if (!PL_colorset) reginitcolors());
1469
-
1470
-
1471
1490
if (is_bare_re)
1472
1491
*is_bare_re = FALSE;
1473
1492
@@ -1840,6 +1859,7 @@ Perl_re_op_compile(pTHX_ SV ** const patternp, int pat_count,
1840
1859
1841
1860
/* Clean up what we did in this parse */
1842
1861
SvREFCNT_dec_NN(RExC_rx_sv);
1862
+ RExC_rx_sv = NULL;
1843
1863
1844
1864
goto redo_parse;
1845
1865
}
@@ -1915,12 +1935,12 @@ Perl_re_op_compile(pTHX_ SV ** const patternp, int pat_count,
1915
1935
/* search for "restudy" in this file for a detailed explanation */
1916
1936
if (!restudied) {
1917
1937
StructCopy(&zero_scan_data, &data, scan_data_t);
1918
- copyRExC_state = RExC_state ;
1938
+ copyRExC_state = *pRExC_state ;
1919
1939
} else {
1920
1940
U32 seen=RExC_seen;
1921
1941
DEBUG_OPTIMISE_r(Perl_re_printf( aTHX_ "Restudying\n"));
1922
1942
1923
- RExC_state = copyRExC_state;
1943
+ *pRExC_state = copyRExC_state;
1924
1944
if (seen & REG_TOP_LEVEL_BRANCHES_SEEN)
1925
1945
RExC_seen |= REG_TOP_LEVEL_BRANCHES_SEEN;
1926
1946
else
@@ -2439,22 +2459,10 @@ Perl_re_op_compile(pTHX_ SV ** const patternp, int pat_count,
2439
2459
regdump(RExC_rx);
2440
2460
});
2441
2461
2442
- if (RExC_open_parens) {
2443
- Safefree(RExC_open_parens);
2444
- RExC_open_parens = NULL;
2445
- }
2446
- if (RExC_close_parens) {
2447
- Safefree(RExC_close_parens);
2448
- RExC_close_parens = NULL;
2449
- }
2450
- if (RExC_logical_to_parno) {
2451
- Safefree(RExC_logical_to_parno);
2452
- RExC_logical_to_parno = NULL;
2453
- }
2454
- if (RExC_parno_to_logical) {
2455
- Safefree(RExC_parno_to_logical);
2456
- RExC_parno_to_logical = NULL;
2457
- }
2462
+ /* we're returning ownership of the SV to the caller, ensure the cleanup
2463
+ * doesn't release it
2464
+ */
2465
+ RExC_rx_sv = NULL;
2458
2466
2459
2467
#ifdef USE_ITHREADS
2460
2468
/* under ithreads the ?pat? PMf_USED flag on the pmop is simulated
@@ -9344,7 +9352,6 @@ S_output_posix_warnings(pTHX_ RExC_state_t *pRExC_state, AV* posix_warnings)
9344
9352
array is mortal, but is a
9345
9353
fail-safe */
9346
9354
(void) sv_2mortal(msg);
9347
- PREPARE_TO_DIE;
9348
9355
}
9349
9356
Perl_warner(aTHX_ packWARN(WARN_REGEXP), "%s", SvPVX(msg));
9350
9357
SvREFCNT_dec_NN(msg);
0 commit comments