Skip to content

Commit acce989

Browse files
committed
Properly DECREF all previously fetched symbols on initialization error
1 parent 8f1a894 commit acce989

1 file changed

Lines changed: 121 additions & 40 deletions

File tree

‎python-rapidjson/rapidjson.cpp‎

Lines changed: 121 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ static PyObject* uuid_type = NULL;
3333
-pedantic mode. */
3434

3535
static PyObject* astimezone_name = NULL;
36-
static PyObject* utcoffset_name = NULL;
37-
static PyObject* total_seconds_name = NULL;
38-
static PyObject* timestamp_name = NULL;
3936
static PyObject* hex_name = NULL;
37+
static PyObject* timestamp_name = NULL;
38+
static PyObject* total_seconds_name = NULL;
39+
static PyObject* utcoffset_name = NULL;
4040

41+
static PyObject* minus_inf_string_value = NULL;
4142
static PyObject* nan_string_value = NULL;
4243
static PyObject* plus_inf_string_value = NULL;
43-
static PyObject* minus_inf_string_value = NULL;
4444

4545

4646
struct HandlerContext {
@@ -1435,54 +1435,147 @@ PyInit_rapidjson()
14351435
if (astimezone_name == NULL)
14361436
return NULL;
14371437

1438-
utcoffset_name = PyUnicode_InternFromString("utcoffset");
1439-
if (utcoffset_name == NULL)
1438+
hex_name = PyUnicode_InternFromString("hex");
1439+
if (hex_name == NULL) {
1440+
Py_DECREF(astimezone_name);
1441+
return NULL;
1442+
}
1443+
1444+
timestamp_name = PyUnicode_InternFromString("timestamp");
1445+
if (timestamp_name == NULL) {
1446+
Py_DECREF(astimezone_name);
1447+
Py_DECREF(hex_name);
14401448
return NULL;
1449+
}
14411450

14421451
total_seconds_name = PyUnicode_InternFromString("total_seconds");
1443-
if (total_seconds_name == NULL)
1452+
if (total_seconds_name == NULL) {
1453+
Py_DECREF(astimezone_name);
1454+
Py_DECREF(hex_name);
1455+
Py_DECREF(timestamp_name);
14441456
return NULL;
1457+
}
14451458

1446-
timestamp_name = PyUnicode_InternFromString("timestamp");
1447-
if (timestamp_name == NULL)
1459+
utcoffset_name = PyUnicode_InternFromString("utcoffset");
1460+
if (utcoffset_name == NULL) {
1461+
Py_DECREF(astimezone_name);
1462+
Py_DECREF(hex_name);
1463+
Py_DECREF(timestamp_name);
1464+
Py_DECREF(total_seconds_name);
14481465
return NULL;
1466+
}
14491467

1450-
hex_name = PyUnicode_InternFromString("hex");
1451-
if (hex_name == NULL)
1468+
minus_inf_string_value = PyUnicode_InternFromString("-Infinity");
1469+
if (minus_inf_string_value == NULL) {
1470+
Py_DECREF(astimezone_name);
1471+
Py_DECREF(hex_name);
1472+
Py_DECREF(timestamp_name);
1473+
Py_DECREF(total_seconds_name);
1474+
Py_DECREF(utcoffset_name);
14521475
return NULL;
1476+
}
14531477

14541478
nan_string_value = PyUnicode_InternFromString("nan");
1455-
if (nan_string_value == NULL)
1479+
if (nan_string_value == NULL) {
1480+
Py_DECREF(astimezone_name);
1481+
Py_DECREF(hex_name);
1482+
Py_DECREF(timestamp_name);
1483+
Py_DECREF(total_seconds_name);
1484+
Py_DECREF(utcoffset_name);
1485+
Py_DECREF(minus_inf_string_value);
14561486
return NULL;
1487+
}
14571488

14581489
plus_inf_string_value = PyUnicode_InternFromString("+Infinity");
1459-
if (plus_inf_string_value == NULL)
1460-
return NULL;
1461-
1462-
minus_inf_string_value = PyUnicode_InternFromString("-Infinity");
1463-
if (minus_inf_string_value == NULL)
1490+
if (plus_inf_string_value == NULL) {
1491+
Py_DECREF(astimezone_name);
1492+
Py_DECREF(hex_name);
1493+
Py_DECREF(timestamp_name);
1494+
Py_DECREF(total_seconds_name);
1495+
Py_DECREF(utcoffset_name);
1496+
Py_DECREF(minus_inf_string_value);
1497+
Py_DECREF(nan_string_value);
14641498
return NULL;
1499+
}
14651500

14661501
PyDateTime_IMPORT;
14671502

14681503
PyObject* datetimeModule = PyImport_ImportModule("datetime");
1469-
if (datetimeModule == NULL)
1504+
if (datetimeModule == NULL) {
1505+
Py_DECREF(astimezone_name);
1506+
Py_DECREF(hex_name);
1507+
Py_DECREF(timestamp_name);
1508+
Py_DECREF(total_seconds_name);
1509+
Py_DECREF(utcoffset_name);
1510+
Py_DECREF(minus_inf_string_value);
1511+
Py_DECREF(nan_string_value);
1512+
Py_DECREF(plus_inf_string_value);
1513+
return NULL;
1514+
}
1515+
1516+
PyObject* decimalModule = PyImport_ImportModule("decimal");
1517+
if (decimalModule == NULL) {
1518+
return NULL;
1519+
}
1520+
1521+
decimal_type = PyObject_GetAttrString(decimalModule, "Decimal");
1522+
Py_DECREF(decimalModule);
1523+
1524+
if (decimal_type == NULL) {
1525+
Py_DECREF(astimezone_name);
1526+
Py_DECREF(hex_name);
1527+
Py_DECREF(timestamp_name);
1528+
Py_DECREF(total_seconds_name);
1529+
Py_DECREF(utcoffset_name);
1530+
Py_DECREF(minus_inf_string_value);
1531+
Py_DECREF(nan_string_value);
1532+
Py_DECREF(plus_inf_string_value);
1533+
Py_DECREF(datetimeModule);
14701534
return NULL;
1535+
}
14711536

14721537
timezone_type = PyObject_GetAttrString(datetimeModule, "timezone");
14731538
Py_DECREF(datetimeModule);
14741539

1475-
if (timezone_type == NULL)
1540+
if (timezone_type == NULL) {
1541+
Py_DECREF(astimezone_name);
1542+
Py_DECREF(hex_name);
1543+
Py_DECREF(timestamp_name);
1544+
Py_DECREF(total_seconds_name);
1545+
Py_DECREF(utcoffset_name);
1546+
Py_DECREF(minus_inf_string_value);
1547+
Py_DECREF(nan_string_value);
1548+
Py_DECREF(plus_inf_string_value);
1549+
Py_DECREF(decimal_type);
14761550
return NULL;
1551+
}
14771552

14781553
timezone_utc = PyObject_GetAttrString(timezone_type, "utc");
14791554
if (timezone_utc == NULL) {
1555+
Py_DECREF(astimezone_name);
1556+
Py_DECREF(hex_name);
1557+
Py_DECREF(timestamp_name);
1558+
Py_DECREF(total_seconds_name);
1559+
Py_DECREF(utcoffset_name);
1560+
Py_DECREF(minus_inf_string_value);
1561+
Py_DECREF(nan_string_value);
1562+
Py_DECREF(plus_inf_string_value);
1563+
Py_DECREF(decimal_type);
14801564
Py_DECREF(timezone_type);
14811565
return NULL;
14821566
}
14831567

14841568
PyObject* uuidModule = PyImport_ImportModule("uuid");
14851569
if (uuidModule == NULL) {
1570+
Py_DECREF(astimezone_name);
1571+
Py_DECREF(hex_name);
1572+
Py_DECREF(timestamp_name);
1573+
Py_DECREF(total_seconds_name);
1574+
Py_DECREF(utcoffset_name);
1575+
Py_DECREF(minus_inf_string_value);
1576+
Py_DECREF(nan_string_value);
1577+
Py_DECREF(plus_inf_string_value);
1578+
Py_DECREF(decimal_type);
14861579
Py_DECREF(timezone_type);
14871580
Py_DECREF(timezone_utc);
14881581
return NULL;
@@ -1492,23 +1585,15 @@ PyInit_rapidjson()
14921585
Py_DECREF(uuidModule);
14931586

14941587
if (uuid_type == NULL) {
1495-
Py_DECREF(timezone_type);
1496-
Py_DECREF(timezone_utc);
1497-
return NULL;
1498-
}
1499-
1500-
PyObject* decimalModule = PyImport_ImportModule("decimal");
1501-
if (decimalModule == NULL) {
1502-
Py_DECREF(timezone_type);
1503-
Py_DECREF(timezone_utc);
1504-
Py_DECREF(uuid_type);
1505-
return NULL;
1506-
}
1507-
1508-
decimal_type = PyObject_GetAttrString(decimalModule, "Decimal");
1509-
Py_DECREF(decimalModule);
1510-
1511-
if (decimal_type == NULL) {
1588+
Py_DECREF(astimezone_name);
1589+
Py_DECREF(hex_name);
1590+
Py_DECREF(timestamp_name);
1591+
Py_DECREF(total_seconds_name);
1592+
Py_DECREF(utcoffset_name);
1593+
Py_DECREF(minus_inf_string_value);
1594+
Py_DECREF(nan_string_value);
1595+
Py_DECREF(plus_inf_string_value);
1596+
Py_DECREF(decimal_type);
15121597
Py_DECREF(timezone_type);
15131598
Py_DECREF(timezone_utc);
15141599
Py_DECREF(uuid_type);
@@ -1519,10 +1604,6 @@ PyInit_rapidjson()
15191604

15201605
m = PyModule_Create(&module);
15211606
if (m == NULL) {
1522-
Py_DECREF(timezone_type);
1523-
Py_DECREF(timezone_utc);
1524-
Py_DECREF(decimal_type);
1525-
Py_DECREF(uuid_type);
15261607
return NULL;
15271608
}
15281609

0 commit comments

Comments
 (0)