bpo-35436: Add missing PyErr_NoMemory() calls and other minor bug fix… · python/cpython@602d307

16 files changed

lines changed

Original file line numberDiff line numberDiff line change

@@ -0,0 +1,2 @@

1+

Fix various issues with memory allocation error handling. Patch by Zackery

2+

Spytz.

Original file line numberDiff line numberDiff line change

@@ -728,6 +728,10 @@ subclasscheck_check_registry(_abc_data *impl, PyObject *subclass,

728728

// Weakref callback may remove entry from set.

729729

// So we take snapshot of registry first.

730730

PyObject **copy = PyMem_Malloc(sizeof(PyObject*) * registry_size);

731+

if (copy == NULL) {

732+

PyErr_NoMemory();

733+

return -1;

734+

}

731735

PyObject *key;

732736

Py_ssize_t pos = 0;

733737

Py_hash_t hash;

Original file line numberDiff line numberDiff line change

@@ -305,8 +305,10 @@ _ctypes_alloc_format_string_for_type(char code, int big_endian)

305305

}

306306
307307

result = PyMem_Malloc(3);

308-

if (result == NULL)

308+

if (result == NULL) {

309+

PyErr_NoMemory();

309310

return NULL;

311+

}

310312
311313

result[0] = big_endian ? '>' : '<';

312314

result[1] = pep_code;

@@ -366,8 +368,10 @@ _ctypes_alloc_format_string_with_shape(int ndim, const Py_ssize_t *shape,

366368

if (prefix)

367369

prefix_len += strlen(prefix);

368370

new_prefix = PyMem_Malloc(prefix_len);

369-

if (new_prefix == NULL)

371+

if (new_prefix == NULL) {

372+

PyErr_NoMemory();

370373

return NULL;

374+

}

371375

new_prefix[0] = '\0';

372376

if (prefix)

373377

strcpy(new_prefix, prefix);

@@ -1851,6 +1855,10 @@ static PyObject *CreateSwappedType(PyTypeObject *type, PyObject *args, PyObject

18511855

#else

18521856

suffix = PyUnicode_InternFromString("_be");

18531857

#endif

1858+

if (suffix == NULL) {

1859+

Py_DECREF(swapped_args);

1860+

return NULL;

1861+

}

18541862
18551863

newname = PyUnicode_Concat(name, suffix);

18561864

if (newname == NULL) {

Original file line numberDiff line numberDiff line change

@@ -305,7 +305,6 @@ static CThunkObject* CThunkObject_new(Py_ssize_t nArgs)

305305
306306

p = PyObject_GC_NewVar(CThunkObject, &PyCThunk_Type, nArgs);

307307

if (p == NULL) {

308-

PyErr_NoMemory();

309308

return NULL;

310309

}

311310
Original file line numberDiff line numberDiff line change

@@ -815,11 +815,13 @@ _io__WindowsConsoleIO_readall_impl(winconsoleio *self)

815815

}

816816

bufsize = newsize;

817817
818-

buf = PyMem_Realloc(buf, (bufsize + 1) * sizeof(wchar_t));

819-

if (!buf) {

818+

wchar_t *tmp = PyMem_Realloc(buf,

819+

(bufsize + 1) * sizeof(wchar_t));

820+

if (tmp == NULL) {

820821

PyMem_Free(buf);

821822

return NULL;

822823

}

824+

buf = tmp;

823825

}

824826
825827

subbuf = read_console_w(self->handle, bufsize - len, &n);

Original file line numberDiff line numberDiff line change

@@ -449,8 +449,9 @@ semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)

449449
450450

if (!unlink) {

451451

name_copy = PyMem_Malloc(strlen(name) + 1);

452-

if (name_copy == NULL)

453-

goto failure;

452+

if (name_copy == NULL) {

453+

return PyErr_NoMemory();

454+

}

454455

strcpy(name_copy, name);

455456

}

456457

@@ -473,7 +474,9 @@ semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)

473474

if (handle != SEM_FAILED)

474475

SEM_CLOSE(handle);

475476

PyMem_Free(name_copy);

476-

_PyMp_SetError(NULL, MP_STANDARD_ERROR);

477+

if (!PyErr_Occurred()) {

478+

_PyMp_SetError(NULL, MP_STANDARD_ERROR);

479+

}

477480

return NULL;

478481

}

479482
Original file line numberDiff line numberDiff line change

@@ -912,6 +912,11 @@ newPySSLSocket(PySSLContext *sslctx, PySocketSockObject *sock,

912912

PySSL_BEGIN_ALLOW_THREADS

913913

self->ssl = SSL_new(ctx);

914914

PySSL_END_ALLOW_THREADS

915+

if (self->ssl == NULL) {

916+

Py_DECREF(self);

917+

_setSSLError(NULL, 0, __FILE__, __LINE__);

918+

return NULL;

919+

}

915920

SSL_set_app_data(self->ssl, self);

916921

if (sock) {

917922

SSL_set_fd(self->ssl, Py_SAFE_DOWNCAST(sock->sock_fd, SOCKET_T, int));

@@ -1241,6 +1246,10 @@ _get_peer_alt_names (X509 *certificate) {

12411246
12421247

/* get a memory buffer */

12431248

biobuf = BIO_new(BIO_s_mem());

1249+

if (biobuf == NULL) {

1250+

PyErr_SetString(PySSLErrorObject, "failed to allocate BIO");

1251+

return NULL;

1252+

}

12441253
12451254

names = (GENERAL_NAMES *)X509_get_ext_d2i(

12461255

certificate, NID_subject_alt_name, NULL, NULL);

@@ -1593,6 +1602,10 @@ _decode_certificate(X509 *certificate) {

15931602
15941603

/* get a memory buffer */

15951604

biobuf = BIO_new(BIO_s_mem());

1605+

if (biobuf == NULL) {

1606+

PyErr_SetString(PySSLErrorObject, "failed to allocate BIO");

1607+

goto fail0;

1608+

}

15961609
15971610

(void) BIO_reset(biobuf);

15981611

serialNumber = X509_get_serialNumber(certificate);

Original file line numberDiff line numberDiff line change

@@ -6229,8 +6229,7 @@ os_getgroups_impl(PyObject *module)

62296229

} else {

62306230

alt_grouplist = PyMem_New(gid_t, n);

62316231

if (alt_grouplist == NULL) {

6232-

errno = EINVAL;

6233-

return posix_error();

6232+

return PyErr_NoMemory();

62346233

}

62356234

}

62366235

@@ -6255,8 +6254,7 @@ os_getgroups_impl(PyObject *module)

62556254

} else {

62566255

alt_grouplist = PyMem_New(gid_t, n);

62576256

if (alt_grouplist == NULL) {

6258-

errno = EINVAL;

6259-

return posix_error();

6257+

return PyErr_NoMemory();

62606258

}

62616259

n = getgroups(n, alt_grouplist);

62626260

if (n == -1) {

Original file line numberDiff line numberDiff line change

@@ -201,7 +201,7 @@ PyCapsule_Import(const char *name, int no_block)

201201

char *name_dup = (char *)PyMem_MALLOC(name_length);

202202
203203

if (!name_dup) {

204-

return NULL;

204+

return PyErr_NoMemory();

205205

}

206206
207207

memcpy(name_dup, name, name_length);

Original file line numberDiff line numberDiff line change

@@ -576,6 +576,9 @@ read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path,

576576

size_t prefixlen = wcslen(prefix);

577577
578578

wchar_t *buf = (wchar_t*)PyMem_RawMalloc(bufsiz * sizeof(wchar_t));

579+

if (buf == NULL) {

580+

goto error;

581+

}

579582

buf[0] = '\0';

580583
581584

while (!feof(sp_file)) {

@@ -603,17 +606,22 @@ read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path,

603606
604607

DWORD wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, NULL, 0);

605608

wchar_t *wline = (wchar_t*)PyMem_RawMalloc((wn + 1) * sizeof(wchar_t));

609+

if (wline == NULL) {

610+

goto error;

611+

}

606612

wn = MultiByteToWideChar(CP_UTF8, 0, line, -1, wline, wn + 1);

607613

wline[wn] = '\0';

608614
609615

size_t usedsiz = wcslen(buf);

610616

while (usedsiz + wn + prefixlen + 4 > bufsiz) {

611617

bufsiz += MAXPATHLEN;

612-

buf = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) * sizeof(wchar_t));

613-

if (!buf) {

618+

wchar_t *tmp = (wchar_t*)PyMem_RawRealloc(buf, (bufsiz + 1) *

619+

sizeof(wchar_t));

620+

if (tmp == NULL) {

614621

PyMem_RawFree(wline);

615622

goto error;

616623

}

624+

buf = tmp;

617625

}

618626
619627

if (usedsiz) {