@@ -190,9 +190,9 @@ static PyObject* _cbson_insert_message(PyObject* self, PyObject* args) {
190
190
unsigned char continue_on_error ;
191
191
codec_options_t options ;
192
192
PyObject * last_error_args ;
193
- buffer_t buffer ;
193
+ buffer_t buffer = NULL ;
194
194
int length_location , message_length ;
195
- PyObject * result ;
195
+ PyObject * result = NULL ;
196
196
197
197
if (!PyArg_ParseTuple (args , "et#ObbObO&" ,
198
198
"utf-8" ,
@@ -209,9 +209,7 @@ static PyObject* _cbson_insert_message(PyObject* self, PyObject* args) {
209
209
}
210
210
buffer = buffer_new ();
211
211
if (!buffer ) {
212
- destroy_codec_options (& options );
213
- PyMem_Free (collection_name );
214
- return NULL ;
212
+ goto fail ;
215
213
}
216
214
217
215
length_location = init_insert_buffer (buffer ,
@@ -221,10 +219,7 @@ static PyObject* _cbson_insert_message(PyObject* self, PyObject* args) {
221
219
collection_name_length ,
222
220
0 );
223
221
if (length_location == -1 ) {
224
- destroy_codec_options (& options );
225
- PyMem_Free (collection_name );
226
- buffer_free (buffer );
227
- return NULL ;
222
+ goto fail ;
228
223
}
229
224
230
225
iterator = PyObject_GetIter (docs );
@@ -234,21 +229,15 @@ static PyObject* _cbson_insert_message(PyObject* self, PyObject* args) {
234
229
PyErr_SetString (InvalidOperation , "input is not iterable" );
235
230
Py_DECREF (InvalidOperation );
236
231
}
237
- destroy_codec_options (& options );
238
- buffer_free (buffer );
239
- PyMem_Free (collection_name );
240
- return NULL ;
232
+ goto fail ;
241
233
}
242
234
while ((doc = PyIter_Next (iterator )) != NULL ) {
243
235
before = buffer_get_position (buffer );
244
236
if (!write_dict (state -> _cbson , buffer , doc , check_keys ,
245
237
& options , 1 )) {
246
238
Py_DECREF (doc );
247
239
Py_DECREF (iterator );
248
- destroy_codec_options (& options );
249
- buffer_free (buffer );
250
- PyMem_Free (collection_name );
251
- return NULL ;
240
+ goto fail ;
252
241
}
253
242
Py_DECREF (doc );
254
243
cur_size = buffer_get_position (buffer ) - before ;
@@ -257,10 +246,7 @@ static PyObject* _cbson_insert_message(PyObject* self, PyObject* args) {
257
246
Py_DECREF (iterator );
258
247
259
248
if (PyErr_Occurred ()) {
260
- destroy_codec_options (& options );
261
- buffer_free (buffer );
262
- PyMem_Free (collection_name );
263
- return NULL ;
249
+ goto fail ;
264
250
}
265
251
266
252
if (!max_size ) {
@@ -269,10 +255,7 @@ static PyObject* _cbson_insert_message(PyObject* self, PyObject* args) {
269
255
PyErr_SetString (InvalidOperation , "cannot do an empty bulk insert" );
270
256
Py_DECREF (InvalidOperation );
271
257
}
272
- destroy_codec_options (& options );
273
- buffer_free (buffer );
274
- PyMem_Free (collection_name );
275
- return NULL ;
258
+ goto fail ;
276
259
}
277
260
278
261
message_length = buffer_get_position (buffer ) - length_location ;
@@ -282,22 +265,21 @@ static PyObject* _cbson_insert_message(PyObject* self, PyObject* args) {
282
265
if (safe ) {
283
266
if (!add_last_error (self , buffer , request_id , collection_name ,
284
267
collection_name_length , & options , last_error_args )) {
285
- destroy_codec_options (& options );
286
- buffer_free (buffer );
287
- PyMem_Free (collection_name );
288
- return NULL ;
268
+ goto fail ;
289
269
}
290
270
}
291
271
292
- PyMem_Free (collection_name );
293
-
294
272
/* objectify buffer */
295
273
result = Py_BuildValue ("i" BYTES_FORMAT_STRING "i" , request_id ,
296
274
buffer_get_buffer (buffer ),
297
275
(Py_ssize_t )buffer_get_position (buffer ),
298
276
max_size );
277
+ fail :
278
+ PyMem_Free (collection_name );
299
279
destroy_codec_options (& options );
300
- buffer_free (buffer );
280
+ if (buffer ) {
281
+ buffer_free (buffer );
282
+ }
301
283
return result ;
302
284
}
303
285
@@ -318,9 +300,9 @@ static PyObject* _cbson_update_message(PyObject* self, PyObject* args) {
318
300
codec_options_t options ;
319
301
PyObject * last_error_args ;
320
302
int flags ;
321
- buffer_t buffer ;
303
+ buffer_t buffer = NULL ;
322
304
int length_location , message_length ;
323
- PyObject * result ;
305
+ PyObject * result = NULL ;
324
306
325
307
if (!PyArg_ParseTuple (args , "et#bbOObObO&" ,
326
308
"utf-8" ,
@@ -341,17 +323,13 @@ static PyObject* _cbson_update_message(PyObject* self, PyObject* args) {
341
323
}
342
324
buffer = buffer_new ();
343
325
if (!buffer ) {
344
- destroy_codec_options (& options );
345
- PyMem_Free (collection_name );
346
- return NULL ;
326
+ goto fail ;
347
327
}
348
328
349
329
// save space for message length
350
330
length_location = buffer_save_space (buffer , 4 );
351
331
if (length_location == -1 ) {
352
- destroy_codec_options (& options );
353
- PyMem_Free (collection_name );
354
- return NULL ;
332
+ goto fail ;
355
333
}
356
334
if (!buffer_write_int32 (buffer , (int32_t )request_id ) ||
357
335
!buffer_write_bytes (buffer ,
@@ -363,28 +341,19 @@ static PyObject* _cbson_update_message(PyObject* self, PyObject* args) {
363
341
collection_name ,
364
342
collection_name_length + 1 ) ||
365
343
!buffer_write_int32 (buffer , (int32_t )flags )) {
366
- destroy_codec_options (& options );
367
- buffer_free (buffer );
368
- PyMem_Free (collection_name );
369
- return NULL ;
344
+ goto fail ;
370
345
}
371
346
372
347
before = buffer_get_position (buffer );
373
348
if (!write_dict (state -> _cbson , buffer , spec , 0 , & options , 1 )) {
374
- destroy_codec_options (& options );
375
- buffer_free (buffer );
376
- PyMem_Free (collection_name );
377
- return NULL ;
349
+ goto fail ;
378
350
}
379
351
max_size = buffer_get_position (buffer ) - before ;
380
352
381
353
before = buffer_get_position (buffer );
382
354
if (!write_dict (state -> _cbson , buffer , doc , check_keys ,
383
355
& options , 1 )) {
384
- destroy_codec_options (& options );
385
- buffer_free (buffer );
386
- PyMem_Free (collection_name );
387
- return NULL ;
356
+ goto fail ;
388
357
}
389
358
cur_size = buffer_get_position (buffer ) - before ;
390
359
max_size = (cur_size > max_size ) ? cur_size : max_size ;
@@ -396,22 +365,21 @@ static PyObject* _cbson_update_message(PyObject* self, PyObject* args) {
396
365
if (safe ) {
397
366
if (!add_last_error (self , buffer , request_id , collection_name ,
398
367
collection_name_length , & options , last_error_args )) {
399
- destroy_codec_options (& options );
400
- buffer_free (buffer );
401
- PyMem_Free (collection_name );
402
- return NULL ;
368
+ goto fail ;
403
369
}
404
370
}
405
371
406
- PyMem_Free (collection_name );
407
-
408
372
/* objectify buffer */
409
373
result = Py_BuildValue ("i" BYTES_FORMAT_STRING "i" , request_id ,
410
374
buffer_get_buffer (buffer ),
411
375
(Py_ssize_t )buffer_get_position (buffer ),
412
376
max_size );
377
+ fail :
378
+ PyMem_Free (collection_name );
413
379
destroy_codec_options (& options );
414
- buffer_free (buffer );
380
+ if (buffer ) {
381
+ buffer_free (buffer );
382
+ }
415
383
return result ;
416
384
}
417
385
@@ -430,7 +398,7 @@ static PyObject* _cbson_query_message(PyObject* self, PyObject* args) {
430
398
PyObject * query ;
431
399
PyObject * field_selector ;
432
400
codec_options_t options ;
433
- buffer_t buffer ;
401
+ buffer_t buffer = NULL ;
434
402
int length_location , message_length ;
435
403
unsigned char check_keys = 0 ;
436
404
PyObject * result = NULL ;
@@ -448,17 +416,13 @@ static PyObject* _cbson_query_message(PyObject* self, PyObject* args) {
448
416
}
449
417
buffer = buffer_new ();
450
418
if (!buffer ) {
451
- destroy_codec_options (& options );
452
- PyMem_Free (collection_name );
453
- return NULL ;
419
+ goto fail ;
454
420
}
455
421
456
422
// save space for message length
457
423
length_location = buffer_save_space (buffer , 4 );
458
424
if (length_location == -1 ) {
459
- destroy_codec_options (& options );
460
- PyMem_Free (collection_name );
461
- return NULL ;
425
+ goto fail ;
462
426
}
463
427
464
428
/* Pop $clusterTime from dict and write it at the end, avoiding an error
@@ -547,11 +511,12 @@ static PyObject* _cbson_query_message(PyObject* self, PyObject* args) {
547
511
buffer_get_buffer (buffer ),
548
512
(Py_ssize_t )buffer_get_position (buffer ),
549
513
max_size );
550
-
551
514
fail :
552
515
PyMem_Free (collection_name );
553
516
destroy_codec_options (& options );
554
- buffer_free (buffer );
517
+ if (buffer ) {
518
+ buffer_free (buffer );
519
+ }
555
520
Py_XDECREF (cluster_time );
556
521
return result ;
557
522
}
@@ -563,9 +528,9 @@ static PyObject* _cbson_get_more_message(PyObject* self, PyObject* args) {
563
528
Py_ssize_t collection_name_length ;
564
529
int num_to_return ;
565
530
long long cursor_id ;
566
- buffer_t buffer ;
531
+ buffer_t buffer = NULL ;
567
532
int length_location , message_length ;
568
- PyObject * result ;
533
+ PyObject * result = NULL ;
569
534
570
535
if (!PyArg_ParseTuple (args , "et#iL" ,
571
536
"utf-8" ,
@@ -577,15 +542,13 @@ static PyObject* _cbson_get_more_message(PyObject* self, PyObject* args) {
577
542
}
578
543
buffer = buffer_new ();
579
544
if (!buffer ) {
580
- PyMem_Free (collection_name );
581
- return NULL ;
545
+ goto fail ;
582
546
}
583
547
584
548
// save space for message length
585
549
length_location = buffer_save_space (buffer , 4 );
586
550
if (length_location == -1 ) {
587
- PyMem_Free (collection_name );
588
- return NULL ;
551
+ goto fail ;
589
552
}
590
553
if (!buffer_write_int32 (buffer , (int32_t )request_id ) ||
591
554
!buffer_write_bytes (buffer ,
@@ -597,13 +560,9 @@ static PyObject* _cbson_get_more_message(PyObject* self, PyObject* args) {
597
560
collection_name_length + 1 ) ||
598
561
!buffer_write_int32 (buffer , (int32_t )num_to_return ) ||
599
562
!buffer_write_int64 (buffer , (int64_t )cursor_id )) {
600
- buffer_free (buffer );
601
- PyMem_Free (collection_name );
602
- return NULL ;
563
+ goto fail ;
603
564
}
604
565
605
- PyMem_Free (collection_name );
606
-
607
566
message_length = buffer_get_position (buffer ) - length_location ;
608
567
buffer_write_int32_at_position (
609
568
buffer , length_location , (int32_t )message_length );
@@ -612,7 +571,11 @@ static PyObject* _cbson_get_more_message(PyObject* self, PyObject* args) {
612
571
result = Py_BuildValue ("i" BYTES_FORMAT_STRING , request_id ,
613
572
buffer_get_buffer (buffer ),
614
573
(Py_ssize_t )buffer_get_position (buffer ));
615
- buffer_free (buffer );
574
+ fail :
575
+ PyMem_Free (collection_name );
576
+ if (buffer ) {
577
+ buffer_free (buffer );
578
+ }
616
579
return result ;
617
580
}
618
581
@@ -634,7 +597,7 @@ static PyObject* _cbson_op_msg(PyObject* self, PyObject* args) {
634
597
PyObject * doc ;
635
598
unsigned char check_keys = 0 ;
636
599
codec_options_t options ;
637
- buffer_t buffer ;
600
+ buffer_t buffer = NULL ;
638
601
int length_location , message_length ;
639
602
int total_size = 0 ;
640
603
int max_doc_size = 0 ;
@@ -655,54 +618,54 @@ static PyObject* _cbson_op_msg(PyObject* self, PyObject* args) {
655
618
}
656
619
buffer = buffer_new ();
657
620
if (!buffer ) {
658
- goto bufferfail ;
621
+ goto fail ;
659
622
}
660
623
661
624
// save space for message length
662
625
length_location = buffer_save_space (buffer , 4 );
663
626
if (length_location == -1 ) {
664
- goto bufferfail ;
627
+ goto fail ;
665
628
}
666
629
if (!buffer_write_int32 (buffer , (int32_t )request_id ) ||
667
630
!buffer_write_bytes (buffer ,
668
631
"\x00\x00\x00\x00" /* responseTo */
669
632
"\xdd\x07\x00\x00" /* 2013 */ , 8 )) {
670
- goto encodefail ;
633
+ goto fail ;
671
634
}
672
635
673
636
if (!buffer_write_int32 (buffer , (int32_t )flags ) ||
674
637
!buffer_write_bytes (buffer , "\x00" , 1 ) /* Payload type 0 */ ) {
675
- goto encodefail ;
638
+ goto fail ;
676
639
}
677
640
total_size = write_dict (state -> _cbson , buffer , command , 0 ,
678
641
& options , 1 );
679
642
if (!total_size ) {
680
- goto encodefail ;
643
+ goto fail ;
681
644
}
682
645
683
646
if (identifier_length ) {
684
647
int payload_one_length_location , payload_length ;
685
648
/* Payload type 1 */
686
649
if (!buffer_write_bytes (buffer , "\x01" , 1 )) {
687
- goto encodefail ;
650
+ goto fail ;
688
651
}
689
652
/* save space for payload 0 length */
690
653
payload_one_length_location = buffer_save_space (buffer , 4 );
691
654
/* C string identifier */
692
655
if (!buffer_write_bytes_ssize_t (buffer , identifier , identifier_length + 1 )) {
693
- goto encodefail ;
656
+ goto fail ;
694
657
}
695
658
iterator = PyObject_GetIter (docs );
696
659
if (iterator == NULL ) {
697
- goto encodefail ;
660
+ goto fail ;
698
661
}
699
662
while ((doc = PyIter_Next (iterator )) != NULL ) {
700
663
int encoded_doc_size = write_dict (
701
664
state -> _cbson , buffer , doc , check_keys ,
702
665
& options , 1 );
703
666
if (!encoded_doc_size ) {
704
667
Py_CLEAR (doc );
705
- goto encodefail ;
668
+ goto fail ;
706
669
}
707
670
if (encoded_doc_size > max_doc_size ) {
708
671
max_doc_size = encoded_doc_size ;
@@ -726,10 +689,11 @@ static PyObject* _cbson_op_msg(PyObject* self, PyObject* args) {
726
689
(Py_ssize_t )buffer_get_position (buffer ),
727
690
total_size ,
728
691
max_doc_size );
729
- encodefail :
692
+ fail :
730
693
Py_XDECREF (iterator );
731
- buffer_free (buffer );
732
- bufferfail :
694
+ if (buffer ) {
695
+ buffer_free (buffer );
696
+ }
733
697
PyMem_Free (identifier );
734
698
destroy_codec_options (& options );
735
699
return result ;
0 commit comments