@@ -1336,7 +1336,8 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1336
1336
unsigned char tz_aware , unsigned char uuid_subtype ) {
1337
1337
struct module_state * state = GETSTATE (self );
1338
1338
1339
- PyObject * value = NULL ;
1339
+ PyObject * value ;
1340
+ PyObject * error ;
1340
1341
switch (type ) {
1341
1342
case 1 :
1342
1343
{
@@ -1346,6 +1347,9 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1346
1347
}
1347
1348
memcpy (& d , buffer + * position , 8 );
1348
1349
value = PyFloat_FromDouble (d );
1350
+ if (!value ) {
1351
+ return NULL ;
1352
+ }
1349
1353
* position += 8 ;
1350
1354
break ;
1351
1355
}
@@ -1358,6 +1362,9 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1358
1362
}
1359
1363
* position += 4 ;
1360
1364
value = PyUnicode_DecodeUTF8 (buffer + * position , value_length , "strict" );
1365
+ if (!value ) {
1366
+ return NULL ;
1367
+ }
1361
1368
* position += value_length + 1 ;
1362
1369
break ;
1363
1370
}
@@ -1371,10 +1378,10 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1371
1378
}
1372
1379
value = elements_to_dict (self , buffer + * position + 4 ,
1373
1380
size - 5 , as_class , tz_aware , uuid_subtype );
1374
-
1375
1381
if (!value ) {
1376
- goto invalid ;
1382
+ return NULL ;
1377
1383
}
1384
+
1378
1385
/* Decoding for DBRefs */
1379
1386
collection = PyDict_GetItemString (value , "$ref" );
1380
1387
if (collection ) { /* DBRef */
@@ -1410,6 +1417,9 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1410
1417
Py_DECREF (id );
1411
1418
Py_DECREF (collection );
1412
1419
Py_DECREF (database );
1420
+ if (!value ) {
1421
+ return NULL ;
1422
+ }
1413
1423
}
1414
1424
1415
1425
* position += size ;
@@ -1429,7 +1439,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1429
1439
1430
1440
value = PyList_New (0 );
1431
1441
if (!value ) {
1432
- goto invalid ;
1442
+ return NULL ;
1433
1443
}
1434
1444
while (* position < end ) {
1435
1445
PyObject * to_append ;
@@ -1446,7 +1456,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1446
1456
max - (int )key_size , as_class , tz_aware , uuid_subtype );
1447
1457
if (!to_append ) {
1448
1458
Py_DECREF (value );
1449
- goto invalid ;
1459
+ return NULL ;
1450
1460
}
1451
1461
PyList_Append (value , to_append );
1452
1462
Py_DECREF (to_append );
@@ -1485,20 +1495,20 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1485
1495
}
1486
1496
#endif
1487
1497
if (!data ) {
1488
- goto invalid ;
1498
+ return NULL ;
1489
1499
}
1490
1500
if ((subtype == 3 || subtype == 4 ) && state -> UUID ) { // Encode as UUID, not Binary
1491
1501
PyObject * kwargs ;
1492
1502
PyObject * args = PyTuple_New (0 );
1493
1503
if (!args ) {
1494
1504
Py_DECREF (data );
1495
- goto invalid ;
1505
+ return NULL ;
1496
1506
}
1497
1507
kwargs = PyDict_New ();
1498
1508
if (!kwargs ) {
1499
1509
Py_DECREF (data );
1500
1510
Py_DECREF (args );
1501
- goto invalid ;
1511
+ return NULL ;
1502
1512
}
1503
1513
1504
1514
assert (length == 16 ); // UUID should always be 16 bytes
@@ -1532,6 +1542,10 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1532
1542
Py_DECREF (args );
1533
1543
Py_DECREF (kwargs );
1534
1544
Py_DECREF (data );
1545
+ if (!value ) {
1546
+ return NULL ;
1547
+ }
1548
+
1535
1549
* position += length + 5 ;
1536
1550
break ;
1537
1551
@@ -1554,6 +1568,9 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1554
1568
value = PyObject_CallFunctionObjArgs (state -> Binary , data , st , NULL );
1555
1569
Py_DECREF (st );
1556
1570
Py_DECREF (data );
1571
+ if (!value ) {
1572
+ return NULL ;
1573
+ }
1557
1574
* position += length + 5 ;
1558
1575
break ;
1559
1576
}
@@ -1574,6 +1591,9 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1574
1591
#else
1575
1592
value = PyObject_CallFunction (state -> ObjectId , "s#" , buffer + * position , 12 );
1576
1593
#endif
1594
+ if (!value ) {
1595
+ return NULL ;
1596
+ }
1577
1597
* position += 12 ;
1578
1598
break ;
1579
1599
}
@@ -1600,29 +1620,29 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1600
1620
}
1601
1621
1602
1622
if (!naive ) {
1603
- goto invalid ;
1623
+ return NULL ;
1604
1624
}
1605
1625
replace = PyObject_GetAttrString (naive , "replace" );
1606
1626
Py_DECREF (naive );
1607
1627
if (!replace ) {
1608
- goto invalid ;
1628
+ return NULL ;
1609
1629
}
1610
1630
args = PyTuple_New (0 );
1611
1631
if (!args ) {
1612
1632
Py_DECREF (replace );
1613
- goto invalid ;
1633
+ return NULL ;
1614
1634
}
1615
1635
kwargs = PyDict_New ();
1616
1636
if (!kwargs ) {
1617
1637
Py_DECREF (replace );
1618
1638
Py_DECREF (args );
1619
- goto invalid ;
1639
+ return NULL ;
1620
1640
}
1621
1641
if (PyDict_SetItemString (kwargs , "tzinfo" , state -> UTC ) == -1 ) {
1622
1642
Py_DECREF (replace );
1623
1643
Py_DECREF (args );
1624
1644
Py_DECREF (kwargs );
1625
- goto invalid ;
1645
+ return NULL ;
1626
1646
}
1627
1647
value = PyObject_Call (replace , args , kwargs );
1628
1648
Py_DECREF (replace );
@@ -1641,7 +1661,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1641
1661
}
1642
1662
pattern = PyUnicode_DecodeUTF8 (buffer + * position , pattern_length , "strict" );
1643
1663
if (!pattern ) {
1644
- goto invalid ;
1664
+ return NULL ;
1645
1665
}
1646
1666
* position += (int )pattern_length + 1 ;
1647
1667
if ((flags_length = strlen (buffer + * position )) > BSON_MAX_SIZE ) {
@@ -1687,14 +1707,14 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1687
1707
collection = PyUnicode_DecodeUTF8 (buffer + * position ,
1688
1708
coll_length , "strict" );
1689
1709
if (!collection ) {
1690
- goto invalid ;
1710
+ return NULL ;
1691
1711
}
1692
1712
* position += (int )coll_length + 1 ;
1693
1713
1694
1714
id = PyObject_CallFunction (state -> ObjectId , "s#" , buffer + * position , 12 );
1695
1715
if (!id ) {
1696
1716
Py_DECREF (collection );
1697
- goto invalid ;
1717
+ return NULL ;
1698
1718
}
1699
1719
* position += 12 ;
1700
1720
value = PyObject_CallFunctionObjArgs (state -> DBRef , collection , id , NULL );
@@ -1712,7 +1732,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1712
1732
* position += 4 ;
1713
1733
code = PyUnicode_DecodeUTF8 (buffer + * position , value_length , "strict" );
1714
1734
if (!code ) {
1715
- goto invalid ;
1735
+ return NULL ;
1716
1736
}
1717
1737
* position += value_length + 1 ;
1718
1738
value = PyObject_CallFunctionObjArgs (state -> Code , code , NULL , NULL );
@@ -1733,7 +1753,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1733
1753
}
1734
1754
code = PyUnicode_DecodeUTF8 (buffer + * position , code_length , "strict" );
1735
1755
if (!code ) {
1736
- goto invalid ;
1756
+ return NULL ;
1737
1757
}
1738
1758
* position += (int )code_length + 1 ;
1739
1759
@@ -1742,7 +1762,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1742
1762
(PyObject * )& PyDict_Type , tz_aware , uuid_subtype );
1743
1763
if (!scope ) {
1744
1764
Py_DECREF (code );
1745
- goto invalid ;
1765
+ return NULL ;
1746
1766
}
1747
1767
* position += scope_size ;
1748
1768
@@ -1764,7 +1784,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1764
1784
value = PyInt_FromLong (i );
1765
1785
#endif
1766
1786
if (!value ) {
1767
- goto invalid ;
1787
+ return NULL ;
1768
1788
}
1769
1789
* position += 4 ;
1770
1790
break ;
@@ -1779,7 +1799,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1779
1799
memcpy (& time , buffer + * position + 4 , 4 );
1780
1800
value = PyObject_CallFunction (state -> Timestamp , "II" , time , inc );
1781
1801
if (!value ) {
1782
- goto invalid ;
1802
+ return NULL ;
1783
1803
}
1784
1804
* position += 8 ;
1785
1805
break ;
@@ -1793,7 +1813,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1793
1813
memcpy (& ll , buffer + * position , 8 );
1794
1814
value = PyLong_FromLongLong (ll );
1795
1815
if (!value ) {
1796
- goto invalid ;
1816
+ return NULL ;
1797
1817
}
1798
1818
* position += 8 ;
1799
1819
break ;
@@ -1819,44 +1839,14 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1819
1839
return NULL ;
1820
1840
}
1821
1841
}
1822
-
1823
- if (value ) {
1824
- return value ;
1825
- }
1842
+ return value ;
1826
1843
1827
1844
invalid :
1828
1845
1829
- /* Wrap any non-InvalidBSON errors in InvalidBSON. */
1830
- if (PyErr_Occurred ()) {
1831
- /* Calling _error clears the error state, so fetch it first. */
1832
- PyObject * etype , * evalue , * etrace , * InvalidBSON ;
1833
- PyErr_Fetch (& etype , & evalue , & etrace );
1834
- InvalidBSON = _error ("InvalidBSON" );
1835
- if (InvalidBSON ) {
1836
- if (!PyErr_GivenExceptionMatches (etype , InvalidBSON )) {
1837
- /* Raise InvalidBSON(str(e)). */
1838
- PyObject * msg = NULL ;
1839
- Py_DECREF (etype );
1840
- etype = InvalidBSON ;
1841
-
1842
- if (evalue ) {
1843
- msg = PyObject_Str (evalue );
1844
- Py_DECREF (evalue );
1845
- evalue = msg ;
1846
- }
1847
- PyErr_NormalizeException (& etype , & evalue , & etrace );
1848
- }
1849
- }
1850
- /* Steals references to args. */
1851
- PyErr_Restore (etype , evalue , etrace );
1852
- Py_XDECREF (InvalidBSON );
1853
- return NULL ;
1854
- } else {
1855
- PyObject * InvalidBSON = _error ("InvalidBSON" );
1856
- if (InvalidBSON ) {
1857
- PyErr_SetNone (InvalidBSON );
1858
- Py_DECREF (InvalidBSON );
1859
- }
1846
+ error = _error ("InvalidBSON" );
1847
+ if (error ) {
1848
+ PyErr_SetNone (error );
1849
+ Py_DECREF (error );
1860
1850
}
1861
1851
return NULL ;
1862
1852
}
0 commit comments