Skip to content

gh-127081: add critical sections to dbm objects #132749

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 14, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix libc thread safety issues with :mod:`dbm` by performing stateful
operations in critical sections.
102 changes: 73 additions & 29 deletions Modules/_dbmmodule.c
Original file line number Diff line number Diff line change
@@ -68,6 +68,7 @@ typedef struct {

#include "clinic/_dbmmodule.c.h"

/* NOTE: Must be used within a critical section! */
#define check_dbmobject_open(v, err) \
if ((v)->di_dbm == NULL) { \
PyErr_SetString(err, "DBM object has already been closed"); \
@@ -118,12 +119,15 @@ dbm_dealloc(PyObject *self)
static Py_ssize_t
dbm_length(PyObject *self)
{
Py_ssize_t result = -1;
dbmobject *dp = dbmobject_CAST(self);
_dbm_state *state = PyType_GetModuleState(Py_TYPE(dp));
assert(state != NULL);

Py_BEGIN_CRITICAL_SECTION(self);
if (dp->di_dbm == NULL) {
PyErr_SetString(state->dbm_error, "DBM object has already been closed");
return -1;
goto done;
}
if ( dp->di_size < 0 ) {
datum key;
@@ -135,73 +139,95 @@ dbm_length(PyObject *self)
size++;
dp->di_size = size;
}
return dp->di_size;
result = dp->di_size;
done:;
Py_END_CRITICAL_SECTION();
return result;
}

static int
dbm_bool(PyObject *self)
{
int result;
dbmobject *dp = dbmobject_CAST(self);
_dbm_state *state = PyType_GetModuleState(Py_TYPE(dp));
assert(state != NULL);

Py_BEGIN_CRITICAL_SECTION(self);
if (dp->di_dbm == NULL) {
PyErr_SetString(state->dbm_error, "DBM object has already been closed");
return -1;
result = -1;
goto done;
}

if (dp->di_size > 0) {
/* Known non-zero size. */
return 1;
result = 1;
goto done;
}
if (dp->di_size == 0) {
/* Known zero size. */
return 0;
result = 0;
goto done;
}

/* Unknown size. Ensure DBM object has an entry. */
datum key = dbm_firstkey(dp->di_dbm);
if (key.dptr == NULL) {
/* Empty. Cache this fact. */
dp->di_size = 0;
return 0;
result = dp->di_size = 0;
goto done;
}

/* Non-empty. Don't cache the length since we don't know. */
return 1;
result = 1;
done:;
Py_END_CRITICAL_SECTION();
return result;
}

static PyObject *
dbm_subscript(PyObject *self, PyObject *key)
{
datum drec, krec;
Py_ssize_t tmp_size;
PyObject *result = NULL;
dbmobject *dp = dbmobject_CAST(self);
_dbm_state *state = PyType_GetModuleState(Py_TYPE(dp));
assert(state != NULL);
if (!PyArg_Parse(key, "s#", &krec.dptr, &tmp_size)) {
return NULL;
}

krec.dsize = tmp_size;
check_dbmobject_open(dp, state->dbm_error);

Py_BEGIN_CRITICAL_SECTION(self);
/* Can't use the macro here as it returns. */
if (dp->di_dbm == NULL) {
PyErr_SetString(state->dbm_error, "DBM object has already been closed");
goto done;
}
drec = dbm_fetch(dp->di_dbm, krec);
if ( drec.dptr == 0 ) {
PyErr_SetObject(PyExc_KeyError, key);
return NULL;
goto done;
}
if ( dbm_error(dp->di_dbm) ) {
dbm_clearerr(dp->di_dbm);
PyErr_SetString(state->dbm_error, "");
return NULL;
goto done;
}
return PyBytes_FromStringAndSize(drec.dptr, drec.dsize);
result = PyBytes_FromStringAndSize(drec.dptr, drec.dsize);
done:;
Py_END_CRITICAL_SECTION();
return result;
}

static int
dbm_ass_sub(PyObject *self, PyObject *v, PyObject *w)
{
datum krec, drec;
Py_ssize_t tmp_size;
int result = -1;
dbmobject *dp = dbmobject_CAST(self);

if ( !PyArg_Parse(v, "s#", &krec.dptr, &tmp_size) ) {
@@ -212,10 +238,13 @@ dbm_ass_sub(PyObject *self, PyObject *v, PyObject *w)
_dbm_state *state = PyType_GetModuleState(Py_TYPE(dp));
assert(state != NULL);
krec.dsize = tmp_size;

Py_BEGIN_CRITICAL_SECTION(self);
if (dp->di_dbm == NULL) {
PyErr_SetString(state->dbm_error, "DBM object has already been closed");
return -1;
PyErr_SetString(state->dbm_error, "DBM object has already been closed");
goto done;
}

dp->di_size = -1;
if (w == NULL) {
if ( dbm_delete(dp->di_dbm, krec) < 0 ) {
@@ -228,39 +257,44 @@ dbm_ass_sub(PyObject *self, PyObject *v, PyObject *w)
else {
PyErr_SetString(state->dbm_error, "cannot delete item from database");
}
return -1;
goto done;
}
} else {
if ( !PyArg_Parse(w, "s#", &drec.dptr, &tmp_size) ) {
PyErr_SetString(PyExc_TypeError,
"dbm mappings have bytes or string elements only");
return -1;
goto done;
}
drec.dsize = tmp_size;
if ( dbm_store(dp->di_dbm, krec, drec, DBM_REPLACE) < 0 ) {
dbm_clearerr(dp->di_dbm);
PyErr_SetString(state->dbm_error,
"cannot add item to database");
return -1;
goto done;
}
}
if ( dbm_error(dp->di_dbm) ) {
dbm_clearerr(dp->di_dbm);
PyErr_SetString(state->dbm_error, "");
return -1;
goto done;
}
return 0;

result = 0;
done:;
Py_END_CRITICAL_SECTION();
return result;
}

/*[clinic input]
@critical_section
_dbm.dbm.close

Close the database.
[clinic start generated code]*/

static PyObject *
_dbm_dbm_close_impl(dbmobject *self)
/*[clinic end generated code: output=c8dc5b6709600b86 input=046db72377d51be8]*/
/*[clinic end generated code: output=c8dc5b6709600b86 input=4a94f79facbc28ca]*/
{
if (self->di_dbm) {
dbm_close(self->di_dbm);
@@ -270,6 +304,7 @@ _dbm_dbm_close_impl(dbmobject *self)
}

/*[clinic input]
@critical_section
_dbm.dbm.keys

cls: defining_class
@@ -279,7 +314,7 @@ Return a list of all keys in the database.

static PyObject *
_dbm_dbm_keys_impl(dbmobject *self, PyTypeObject *cls)
/*[clinic end generated code: output=f2a593b3038e5996 input=d3706a28fc051097]*/
/*[clinic end generated code: output=f2a593b3038e5996 input=6ddefeadf2a80156]*/
{
PyObject *v, *item;
datum key;
@@ -315,35 +350,42 @@ dbm_contains(PyObject *self, PyObject *arg)
dbmobject *dp = dbmobject_CAST(self);
datum key, val;
Py_ssize_t size;
int result = -1;

_dbm_state *state = PyType_GetModuleState(Py_TYPE(dp));
assert(state != NULL);

Py_BEGIN_CRITICAL_SECTION(self);
if ((dp)->di_dbm == NULL) {
PyErr_SetString(state->dbm_error,
"DBM object has already been closed");
return -1;
goto done;
}
if (PyUnicode_Check(arg)) {
key.dptr = (char *)PyUnicode_AsUTF8AndSize(arg, &size);
key.dsize = size;
if (key.dptr == NULL)
return -1;
goto done;
}
else if (!PyBytes_Check(arg)) {
PyErr_Format(PyExc_TypeError,
"dbm key must be bytes or string, not %.100s",
Py_TYPE(arg)->tp_name);
return -1;
goto done;
}
else {
key.dptr = PyBytes_AS_STRING(arg);
key.dsize = PyBytes_GET_SIZE(arg);
}
val = dbm_fetch(dp->di_dbm, key);
return val.dptr != NULL;
result = val.dptr != NULL;
done:;
Py_END_CRITICAL_SECTION();
return result;
}

/*[clinic input]
@critical_section
_dbm.dbm.get
cls: defining_class
key: str(accept={str, robuffer}, zeroes=True)
@@ -356,7 +398,7 @@ Return the value for key if present, otherwise default.
static PyObject *
_dbm_dbm_get_impl(dbmobject *self, PyTypeObject *cls, const char *key,
Py_ssize_t key_length, PyObject *default_value)
/*[clinic end generated code: output=b4e55f8b6d482bc4 input=66b993b8349fa8c1]*/
/*[clinic end generated code: output=b4e55f8b6d482bc4 input=1d88a22bb5e55202]*/
{
datum dbm_key, val;
_dbm_state *state = PyType_GetModuleState(cls);
@@ -373,6 +415,7 @@ _dbm_dbm_get_impl(dbmobject *self, PyTypeObject *cls, const char *key,
}

/*[clinic input]
@critical_section
_dbm.dbm.setdefault
cls: defining_class
key: str(accept={str, robuffer}, zeroes=True)
@@ -387,7 +430,7 @@ If key is not in the database, it is inserted with default as the value.
static PyObject *
_dbm_dbm_setdefault_impl(dbmobject *self, PyTypeObject *cls, const char *key,
Py_ssize_t key_length, PyObject *default_value)
/*[clinic end generated code: output=9c2f6ea6d0fb576c input=126a3ff15c5f8232]*/
/*[clinic end generated code: output=9c2f6ea6d0fb576c input=c01510ef7571e13b]*/
{
datum dbm_key, val;
Py_ssize_t tmp_size;
@@ -427,6 +470,7 @@ _dbm_dbm_setdefault_impl(dbmobject *self, PyTypeObject *cls, const char *key,
}

/*[clinic input]
@critical_section
_dbm.dbm.clear
cls: defining_class
/
@@ -436,7 +480,7 @@ Remove all items from the database.

static PyObject *
_dbm_dbm_clear_impl(dbmobject *self, PyTypeObject *cls)
/*[clinic end generated code: output=8d126b9e1d01a434 input=43aa6ca1acb7f5f5]*/
/*[clinic end generated code: output=8d126b9e1d01a434 input=a1aa5d99adfb9656]*/
{
_dbm_state *state = PyType_GetModuleState(cls);
assert(state != NULL);
Loading
Oops, something went wrong.
Loading
Oops, something went wrong.