Skip to content

Commit a00221e

Browse files
authored
gh-116738: Make _csv module thread-safe (#118344)
1 parent 08f6bf7 commit a00221e

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

Modules/_csv.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module instead.
1414
#endif
1515

1616
#include "Python.h"
17+
#include "pycore_pyatomic_ft_wrappers.h"
1718

1819
#include <stddef.h> // offsetof()
1920
#include <stdbool.h>
@@ -34,7 +35,7 @@ typedef struct {
3435
PyTypeObject *dialect_type;
3536
PyTypeObject *reader_type;
3637
PyTypeObject *writer_type;
37-
long field_limit; /* max parsed field size */
38+
Py_ssize_t field_limit; /* max parsed field size */
3839
PyObject *str_write;
3940
} _csvstate;
4041

@@ -706,10 +707,11 @@ parse_grow_buff(ReaderObj *self)
706707
static int
707708
parse_add_char(ReaderObj *self, _csvstate *module_state, Py_UCS4 c)
708709
{
709-
if (self->field_len >= module_state->field_limit) {
710+
Py_ssize_t field_limit = FT_ATOMIC_LOAD_SSIZE_RELAXED(module_state->field_limit);
711+
if (self->field_len >= field_limit) {
710712
PyErr_Format(module_state->error_obj,
711-
"field larger than field limit (%ld)",
712-
module_state->field_limit);
713+
"field larger than field limit (%zd)",
714+
field_limit);
713715
return -1;
714716
}
715717
if (self->field_len == self->field_size && !parse_grow_buff(self))
@@ -1659,20 +1661,20 @@ _csv_field_size_limit_impl(PyObject *module, PyObject *new_limit)
16591661
/*[clinic end generated code: output=f2799ecd908e250b input=cec70e9226406435]*/
16601662
{
16611663
_csvstate *module_state = get_csv_state(module);
1662-
long old_limit = module_state->field_limit;
1664+
Py_ssize_t old_limit = FT_ATOMIC_LOAD_SSIZE_RELAXED(module_state->field_limit);
16631665
if (new_limit != NULL) {
16641666
if (!PyLong_CheckExact(new_limit)) {
16651667
PyErr_Format(PyExc_TypeError,
16661668
"limit must be an integer");
16671669
return NULL;
16681670
}
1669-
module_state->field_limit = PyLong_AsLong(new_limit);
1670-
if (module_state->field_limit == -1 && PyErr_Occurred()) {
1671-
module_state->field_limit = old_limit;
1671+
Py_ssize_t new_limit_value = PyLong_AsSsize_t(new_limit);
1672+
if (new_limit_value == -1 && PyErr_Occurred()) {
16721673
return NULL;
16731674
}
1675+
FT_ATOMIC_STORE_SSIZE_RELAXED(module_state->field_limit, new_limit_value);
16741676
}
1675-
return PyLong_FromLong(old_limit);
1677+
return PyLong_FromSsize_t(old_limit);
16761678
}
16771679

16781680
static PyType_Slot error_slots[] = {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy