Skip to content

Commit 7380097

Browse files
authored
PYTHON-3959 - NULL Initialize PyObjects (#1859)
1 parent af23139 commit 7380097

File tree

2 files changed

+29
-29
lines changed

2 files changed

+29
-29
lines changed

bson/_cbsonmodule.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static PyObject* _test_long_long_to_str(PyObject* self, PyObject* args) {
207207
*
208208
* Returns a new ref */
209209
static PyObject* _error(char* name) {
210-
PyObject* error;
210+
PyObject* error = NULL;
211211
PyObject* errors = PyImport_ImportModule("bson.errors");
212212
if (!errors) {
213213
return NULL;
@@ -279,7 +279,7 @@ static PyObject* datetime_from_millis(long long millis) {
279279
* micros = diff * 1000 111000
280280
* Resulting in datetime(1, 1, 1, 1, 1, 1, 111000) -- the expected result
281281
*/
282-
PyObject* datetime;
282+
PyObject* datetime = NULL;
283283
int diff = (int)(((millis % 1000) + 1000) % 1000);
284284
int microseconds = diff * 1000;
285285
Time64_T seconds = (millis - diff) / 1000;
@@ -294,7 +294,7 @@ static PyObject* datetime_from_millis(long long millis) {
294294
timeinfo.tm_sec,
295295
microseconds);
296296
if(!datetime) {
297-
PyObject *etype, *evalue, *etrace;
297+
PyObject *etype = NULL, *evalue = NULL, *etrace = NULL;
298298

299299
/*
300300
* Calling _error clears the error state, so fetch it first.
@@ -350,8 +350,8 @@ static PyObject* datetime_ms_from_millis(PyObject* self, long long millis){
350350
return NULL;
351351
}
352352

353-
PyObject* dt;
354-
PyObject* ll_millis;
353+
PyObject* dt = NULL;
354+
PyObject* ll_millis = NULL;
355355

356356
if (!(ll_millis = PyLong_FromLongLong(millis))){
357357
return NULL;
@@ -1790,7 +1790,7 @@ static PyObject* _cbson_dict_to_bson(PyObject* self, PyObject* args) {
17901790
PyObject* result;
17911791
unsigned char check_keys;
17921792
unsigned char top_level = 1;
1793-
PyObject* options_obj;
1793+
PyObject* options_obj = NULL;
17941794
codec_options_t options;
17951795
buffer_t buffer;
17961796
PyObject* raw_bson_document_bytes_obj;
@@ -2512,8 +2512,8 @@ static PyObject* get_value(PyObject* self, PyObject* name, const char* buffer,
25122512
* Wrap any non-InvalidBSON errors in InvalidBSON.
25132513
*/
25142514
if (PyErr_Occurred()) {
2515-
PyObject *etype, *evalue, *etrace;
2516-
PyObject *InvalidBSON;
2515+
PyObject *etype = NULL, *evalue = NULL, *etrace = NULL;
2516+
PyObject *InvalidBSON = NULL;
25172517

25182518
/*
25192519
* Calling _error clears the error state, so fetch it first.
@@ -2585,8 +2585,8 @@ static int _element_to_dict(PyObject* self, const char* string,
25852585
if (!*name) {
25862586
/* If NULL is returned then wrap the UnicodeDecodeError
25872587
in an InvalidBSON error */
2588-
PyObject *etype, *evalue, *etrace;
2589-
PyObject *InvalidBSON;
2588+
PyObject *etype = NULL, *evalue = NULL, *etrace = NULL;
2589+
PyObject *InvalidBSON = NULL;
25902590

25912591
PyErr_Fetch(&etype, &evalue, &etrace);
25922592
if (PyErr_GivenExceptionMatches(etype, PyExc_Exception)) {
@@ -2620,7 +2620,7 @@ static PyObject* _cbson_element_to_dict(PyObject* self, PyObject* args) {
26202620
/* TODO: Support buffer protocol */
26212621
char* string;
26222622
PyObject* bson;
2623-
PyObject* options_obj;
2623+
PyObject* options_obj = NULL;
26242624
codec_options_t options;
26252625
unsigned position;
26262626
unsigned max;
@@ -2732,7 +2732,7 @@ static PyObject* _cbson_bson_to_dict(PyObject* self, PyObject* args) {
27322732
int32_t size;
27332733
Py_ssize_t total_size;
27342734
const char* string;
2735-
PyObject* bson;
2735+
PyObject* bson = NULL;
27362736
codec_options_t options;
27372737
PyObject* result = NULL;
27382738
PyObject* options_obj;

pymongo/_cmessagemodule.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct module_state {
4545
*
4646
* Returns a new ref */
4747
static PyObject* _error(char* name) {
48-
PyObject* error;
48+
PyObject* error = NULL;
4949
PyObject* errors = PyImport_ImportModule("pymongo.errors");
5050
if (!errors) {
5151
return NULL;
@@ -75,9 +75,9 @@ static PyObject* _cbson_query_message(PyObject* self, PyObject* args) {
7575
int begin, cur_size, max_size = 0;
7676
int num_to_skip;
7777
int num_to_return;
78-
PyObject* query;
79-
PyObject* field_selector;
80-
PyObject* options_obj;
78+
PyObject* query = NULL;
79+
PyObject* field_selector = NULL;
80+
PyObject* options_obj = NULL;
8181
codec_options_t options;
8282
buffer_t buffer = NULL;
8383
int length_location, message_length;
@@ -221,12 +221,12 @@ static PyObject* _cbson_op_msg(PyObject* self, PyObject* args) {
221221
/* NOTE just using a random number as the request_id */
222222
int request_id = rand();
223223
unsigned int flags;
224-
PyObject* command;
224+
PyObject* command = NULL;
225225
char* identifier = NULL;
226226
Py_ssize_t identifier_length = 0;
227-
PyObject* docs;
228-
PyObject* doc;
229-
PyObject* options_obj;
227+
PyObject* docs = NULL;
228+
PyObject* doc = NULL;
229+
PyObject* options_obj = NULL;
230230
codec_options_t options;
231231
buffer_t buffer = NULL;
232232
int length_location, message_length;
@@ -535,12 +535,12 @@ static PyObject*
535535
_cbson_encode_batched_op_msg(PyObject* self, PyObject* args) {
536536
unsigned char op;
537537
unsigned char ack;
538-
PyObject* command;
539-
PyObject* docs;
538+
PyObject* command = NULL;
539+
PyObject* docs = NULL;
540540
PyObject* ctx = NULL;
541541
PyObject* to_publish = NULL;
542542
PyObject* result = NULL;
543-
PyObject* options_obj;
543+
PyObject* options_obj = NULL;
544544
codec_options_t options;
545545
buffer_t buffer;
546546
struct module_state *state = GETSTATE(self);
@@ -592,12 +592,12 @@ _cbson_batched_op_msg(PyObject* self, PyObject* args) {
592592
unsigned char ack;
593593
int request_id;
594594
int position;
595-
PyObject* command;
596-
PyObject* docs;
595+
PyObject* command = NULL;
596+
PyObject* docs = NULL;
597597
PyObject* ctx = NULL;
598598
PyObject* to_publish = NULL;
599599
PyObject* result = NULL;
600-
PyObject* options_obj;
600+
PyObject* options_obj = NULL;
601601
codec_options_t options;
602602
buffer_t buffer;
603603
struct module_state *state = GETSTATE(self);
@@ -868,12 +868,12 @@ _cbson_encode_batched_write_command(PyObject* self, PyObject* args) {
868868
char *ns = NULL;
869869
unsigned char op;
870870
Py_ssize_t ns_len;
871-
PyObject* command;
872-
PyObject* docs;
871+
PyObject* command = NULL;
872+
PyObject* docs = NULL;
873873
PyObject* ctx = NULL;
874874
PyObject* to_publish = NULL;
875875
PyObject* result = NULL;
876-
PyObject* options_obj;
876+
PyObject* options_obj = NULL;
877877
codec_options_t options;
878878
buffer_t buffer;
879879
struct module_state *state = GETSTATE(self);

0 commit comments

Comments
 (0)