Skip to content

Commit 135ec7c

Browse files
authored
gh-99537: Use Py_SETREF() function in C code (#99657)
Fix potential race condition in code patterns: * Replace "Py_DECREF(var); var = new;" with "Py_SETREF(var, new);" * Replace "Py_XDECREF(var); var = new;" with "Py_XSETREF(var, new);" * Replace "Py_CLEAR(var); var = new;" with "Py_XSETREF(var, new);" Other changes: * Replace "old = var; var = new; Py_DECREF(var)" with "Py_SETREF(var, new);" * Replace "old = var; var = new; Py_XDECREF(var)" with "Py_XSETREF(var, new);" * And remove the "old" variable.
1 parent 3db0a21 commit 135ec7c

19 files changed

+34
-76
lines changed

Objects/bytesobject.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2109,9 +2109,7 @@ bytes_translate_impl(PyBytesObject *self, PyObject *table,
21092109
changed = 1;
21102110
}
21112111
if (!changed && PyBytes_CheckExact(input_obj)) {
2112-
Py_INCREF(input_obj);
2113-
Py_DECREF(result);
2114-
result = input_obj;
2112+
Py_SETREF(result, Py_NewRef(input_obj));
21152113
}
21162114
PyBuffer_Release(&del_table_view);
21172115
PyBuffer_Release(&table_view);

Objects/capsule.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ PyCapsule_Import(const char *name, int no_block)
220220
}
221221
} else {
222222
PyObject *object2 = PyObject_GetAttrString(object, trace);
223-
Py_DECREF(object);
224-
object = object2;
223+
Py_SETREF(object, object2);
225224
}
226225
if (!object) {
227226
goto EXIT;

Objects/fileobject.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ PyFile_GetLine(PyObject *f, int n)
8888
else {
8989
PyObject *v;
9090
v = PyBytes_FromStringAndSize(s, len-1);
91-
Py_DECREF(result);
92-
result = v;
91+
Py_SETREF(result, v);
9392
}
9493
}
9594
}
@@ -104,8 +103,7 @@ PyFile_GetLine(PyObject *f, int n)
104103
else if (PyUnicode_READ_CHAR(result, len-1) == '\n') {
105104
PyObject *v;
106105
v = PyUnicode_Substring(result, 0, len-1);
107-
Py_DECREF(result);
108-
result = v;
106+
Py_SETREF(result, v);
109107
}
110108
}
111109
return result;

Objects/floatobject.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -531,20 +531,17 @@ float_richcompare(PyObject *v, PyObject *w, int op)
531531
temp = _PyLong_Lshift(ww, 1);
532532
if (temp == NULL)
533533
goto Error;
534-
Py_DECREF(ww);
535-
ww = temp;
534+
Py_SETREF(ww, temp);
536535

537536
temp = _PyLong_Lshift(vv, 1);
538537
if (temp == NULL)
539538
goto Error;
540-
Py_DECREF(vv);
541-
vv = temp;
539+
Py_SETREF(vv, temp);
542540

543541
temp = PyNumber_Or(vv, _PyLong_GetOne());
544542
if (temp == NULL)
545543
goto Error;
546-
Py_DECREF(vv);
547-
vv = temp;
544+
Py_SETREF(vv, temp);
548545
}
549546

550547
r = PyObject_RichCompareBool(vv, ww, op);

Objects/genobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,8 +491,7 @@ _gen_throw(PyGenObject *gen, int close_on_genexit,
491491
}
492492
else {
493493
/* Normalize to raise <class>, <instance> */
494-
Py_XDECREF(val);
495-
val = typ;
494+
Py_XSETREF(val, typ);
496495
typ = Py_NewRef(PyExceptionInstance_Class(typ));
497496

498497
if (tb == NULL)

Objects/setobject.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,8 +1270,7 @@ set_intersection_multi(PySetObject *so, PyObject *args)
12701270
Py_DECREF(result);
12711271
return NULL;
12721272
}
1273-
Py_DECREF(result);
1274-
result = newresult;
1273+
Py_SETREF(result, newresult);
12751274
}
12761275
return result;
12771276
}

Objects/sliceobject.c

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -448,28 +448,23 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
448448
if (_PyLong_Sign(start) < 0) {
449449
/* start += length */
450450
PyObject *tmp = PyNumber_Add(start, length);
451-
Py_DECREF(start);
452-
start = tmp;
451+
Py_SETREF(start, tmp);
453452
if (start == NULL)
454453
goto error;
455454

456455
cmp_result = PyObject_RichCompareBool(start, lower, Py_LT);
457456
if (cmp_result < 0)
458457
goto error;
459458
if (cmp_result) {
460-
Py_INCREF(lower);
461-
Py_DECREF(start);
462-
start = lower;
459+
Py_SETREF(start, Py_NewRef(lower));
463460
}
464461
}
465462
else {
466463
cmp_result = PyObject_RichCompareBool(start, upper, Py_GT);
467464
if (cmp_result < 0)
468465
goto error;
469466
if (cmp_result) {
470-
Py_INCREF(upper);
471-
Py_DECREF(start);
472-
start = upper;
467+
Py_SETREF(start, Py_NewRef(upper));
473468
}
474469
}
475470
}
@@ -486,28 +481,23 @@ _PySlice_GetLongIndices(PySliceObject *self, PyObject *length,
486481
if (_PyLong_Sign(stop) < 0) {
487482
/* stop += length */
488483
PyObject *tmp = PyNumber_Add(stop, length);
489-
Py_DECREF(stop);
490-
stop = tmp;
484+
Py_SETREF(stop, tmp);
491485
if (stop == NULL)
492486
goto error;
493487

494488
cmp_result = PyObject_RichCompareBool(stop, lower, Py_LT);
495489
if (cmp_result < 0)
496490
goto error;
497491
if (cmp_result) {
498-
Py_INCREF(lower);
499-
Py_DECREF(stop);
500-
stop = lower;
492+
Py_SETREF(stop, Py_NewRef(lower));
501493
}
502494
}
503495
else {
504496
cmp_result = PyObject_RichCompareBool(stop, upper, Py_GT);
505497
if (cmp_result < 0)
506498
goto error;
507499
if (cmp_result) {
508-
Py_INCREF(upper);
509-
Py_DECREF(stop);
510-
stop = upper;
500+
Py_SETREF(stop, Py_NewRef(upper));
511501
}
512502
}
513503
}

Objects/stringlib/unicode_format.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,7 @@ get_field_object(SubString *input, PyObject *args, PyObject *kwargs,
473473
goto error;
474474

475475
/* assign to obj */
476-
Py_DECREF(obj);
477-
obj = tmp;
476+
Py_SETREF(obj, tmp);
478477
}
479478
/* end of iterator, this is the non-error case */
480479
if (ok == 1)
@@ -825,8 +824,7 @@ output_markup(SubString *field_name, SubString *format_spec,
825824
goto done;
826825

827826
/* do the assignment, transferring ownership: fieldobj = tmp */
828-
Py_DECREF(fieldobj);
829-
fieldobj = tmp;
827+
Py_SETREF(fieldobj, tmp);
830828
tmp = NULL;
831829
}
832830

Objects/typeobject.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5968,8 +5968,7 @@ object___dir___impl(PyObject *self)
59685968
else {
59695969
/* Copy __dict__ to avoid mutating it. */
59705970
PyObject *temp = PyDict_Copy(dict);
5971-
Py_DECREF(dict);
5972-
dict = temp;
5971+
Py_SETREF(dict, temp);
59735972
}
59745973

59755974
if (dict == NULL)
@@ -9377,8 +9376,7 @@ super_getattro(PyObject *self, PyObject *name)
93779376
(See SF ID #743627) */
93789377
(su->obj == (PyObject *)starttype) ? NULL : su->obj,
93799378
(PyObject *)starttype);
9380-
Py_DECREF(res);
9381-
res = res2;
9379+
Py_SETREF(res, res2);
93829380
}
93839381

93849382
Py_DECREF(mro);

Objects/unicodeobject.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13572,8 +13572,7 @@ _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type)
1357213572
for (i = 0; i < numdigits; i++)
1357313573
*b1++ = *buf++;
1357413574
*b1 = '\0';
13575-
Py_DECREF(result);
13576-
result = r1;
13575+
Py_SETREF(result, r1);
1357713576
buf = PyBytes_AS_STRING(result);
1357813577
len = numnondigits + prec;
1357913578
}
@@ -13590,8 +13589,7 @@ _PyUnicode_FormatLong(PyObject *val, int alt, int prec, int type)
1359013589
|| buf != PyUnicode_DATA(result)) {
1359113590
PyObject *unicode;
1359213591
unicode = _PyUnicode_FromASCII(buf, len);
13593-
Py_DECREF(result);
13594-
result = unicode;
13592+
Py_SETREF(result, unicode);
1359513593
}
1359613594
else if (len != PyUnicode_GET_LENGTH(result)) {
1359713595
if (PyUnicode_Resize(&result, len) < 0)

0 commit comments

Comments
 (0)