From 6e1a12739841bc4f7d4e5a2fc8b36d72fc0e8cb2 Mon Sep 17 00:00:00 2001 From: Niki Date: Mon, 24 Jun 2024 15:57:24 +0200 Subject: [PATCH] fix an error in strdup clone --- cstring.c | 4 +++- cutils.c | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cstring.c b/cstring.c index 58481ec..5a4d81a 100644 --- a/cstring.c +++ b/cstring.c @@ -590,7 +590,9 @@ void cstring_change_case(cstring_t *self, int up) { size_t i = 0; while (i < self->length) { - count = mbrtowc(&wide, self->string + i, self->length - i, &state_from); + count = mbrtowc(&wide, + self->string + i, self->length - i, + &state_from); //incomplete (should not happen) if (count == (size_t) -2) { diff --git a/cutils.c b/cutils.c index 4547c4b..87ffa9e 100644 --- a/cutils.c +++ b/cutils.c @@ -38,6 +38,8 @@ size_t strnlen(const char *s, size_t maxlen) { char *strdup(const char *source) { size_t sz = strlen(source); char *new = malloc((sz + 1) * sizeof(char)); + if (!new) + return NULL; strcpy(new, source); return new; } -- 2.27.0