Skip to content

Commit e9cbda3

Browse files
committed
NULL-check memory allocations
1 parent 89ec5c8 commit e9cbda3

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/cstring.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ static PyTypeObject cstring_type;
3030

3131
static PyObject *_cstring_new(PyTypeObject *type, const char *value, Py_ssize_t len) {
3232
struct cstring *new = CSTRING_ALLOC(type, len + 1);
33+
if(!new)
34+
return NULL;
3335
new->hash = -1;
3436
memcpy(new->value, value, len);
3537
new->value[len] = '\0';
@@ -193,6 +195,8 @@ static PyObject *cstring_concat(PyObject *left, PyObject *right) {
193195
Py_ssize_t size = cstring_len(left) + cstring_len(right) + 1;
194196

195197
struct cstring *new = CSTRING_ALLOC(Py_TYPE(left), size);
198+
if(!new)
199+
return NULL;
196200
memcpy(new->value, CSTRING_VALUE(left), Py_SIZE(left));
197201
memcpy(&new->value[cstring_len(left)], CSTRING_VALUE(right), Py_SIZE(right));
198202
return (PyObject *)new;
@@ -207,6 +211,8 @@ static PyObject *cstring_repeat(PyObject *self, Py_ssize_t count) {
207211
Py_ssize_t size = (cstring_len(self) * count) + 1;
208212

209213
struct cstring *new = CSTRING_ALLOC(Py_TYPE(self), size);
214+
if(!new)
215+
return NULL;
210216
for(Py_ssize_t i = 0; i < size - 1; i += cstring_len(self)) {
211217
memcpy(&new->value[i], CSTRING_VALUE(self), Py_SIZE(self));
212218
}
@@ -254,6 +260,8 @@ static PyObject *_cstring_subscript_slice(PyObject *self, PyObject *slice) {
254260
}
255261

256262
struct cstring *new = CSTRING_ALLOC(Py_TYPE(self), slicelen + 1);
263+
if(!new)
264+
return NULL;
257265
char *src = CSTRING_VALUE_AT(self, start);
258266
for(Py_ssize_t i = 0; i < slicelen; ++i) {
259267
new->value[i] = *src;
@@ -527,6 +535,8 @@ PyObject *cstring_join(PyObject *self, PyObject *arg) {
527535
PyDoc_STRVAR(lower__doc__, "");
528536
PyObject *cstring_lower(PyObject *self, PyObject *args) {
529537
struct cstring *new = CSTRING_ALLOC(Py_TYPE(self), Py_SIZE(self));
538+
if(!new)
539+
return NULL;
530540
const char *s = CSTRING_VALUE(self);
531541
char *d = CSTRING_VALUE(new);
532542

@@ -592,6 +602,8 @@ PyObject *cstring_endswith(PyObject *self, PyObject *args) {
592602
PyDoc_STRVAR(swapcase__doc__, "");
593603
PyObject *cstring_swapcase(PyObject *self, PyObject *args) {
594604
struct cstring *new = CSTRING_ALLOC(Py_TYPE(self), Py_SIZE(self));
605+
if(!new)
606+
return NULL;
595607
const char *s = CSTRING_VALUE(self);
596608
char *d = CSTRING_VALUE(new);
597609

@@ -611,6 +623,8 @@ PyObject *cstring_swapcase(PyObject *self, PyObject *args) {
611623
PyDoc_STRVAR(upper__doc__, "");
612624
PyObject *cstring_upper(PyObject *self, PyObject *args) {
613625
struct cstring *new = CSTRING_ALLOC(Py_TYPE(self), Py_SIZE(self));
626+
if(!new)
627+
return NULL;
614628
const char *s = CSTRING_VALUE(self);
615629
char *d = CSTRING_VALUE(new);
616630

0 commit comments

Comments
 (0)