From 8e520756366ca5739f13dc6ad65fcf269dbbc994 Mon Sep 17 00:00:00 2001 From: cpq Date: Tue, 26 Jan 2021 12:16:58 +0000 Subject: [PATCH] Handle calloc failures --- mongoose.c | 22 ++++++++++++++++++---- mongoose.h | 3 +-- src/arch_win32.h | 3 +-- src/http.c | 12 ++++++++---- src/tls.c | 10 ++++++++++ 5 files changed, 38 insertions(+), 12 deletions(-) diff --git a/mongoose.c b/mongoose.c index a4147757..2a798c27 100644 --- a/mongoose.c +++ b/mongoose.c @@ -801,10 +801,14 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, fclose(fp); } else { struct http_data *d = (struct http_data *) calloc(1, sizeof(*d)); - d->fp = fp; - d->old_pfn_data = c->pfn_data; - c->pfn = static_cb; - c->pfn_data = d; + if (d == NULL) { + mg_error(c, "static HTTP OOM"); + } else { + d->fp = fp; + d->old_pfn_data = c->pfn_data; + c->pfn = static_cb; + c->pfn_data = d; + } } } } @@ -3320,6 +3324,10 @@ static void debug_cb(void *c, int lev, const char *s, int n, const char *s2) { int mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) { struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls)); int rc = 0; + if (tls == NULL) { + mg_error(c, "TLS OOM"); + goto fail; + } LOG(LL_DEBUG, ("%lu Setting TLS, CA: %s, cert: %s, key: %s", c->id, opts->ca == NULL ? "null" : opts->ca, opts->cert == NULL ? "null" : opts->cert, @@ -3458,6 +3466,12 @@ int mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) { const char *id = "mongoose"; static unsigned char s_initialised = 0; int rc; + + if (tls == NULL) { + mg_error(c, "TLS OOM"); + goto fail; + } + if (!s_initialised) { SSL_library_init(); s_initialised++; diff --git a/mongoose.h b/mongoose.h index 41f8b01b..0010a4e7 100644 --- a/mongoose.h +++ b/mongoose.h @@ -233,8 +233,7 @@ typedef unsigned short uint16_t; typedef short int16_t; typedef unsigned int uint32_t; typedef int int32_t; -typedef int bool; -enum { false = 0, true = 1 }; +typedef enum { false = 0, true = 1 } bool; #else #include #include diff --git a/src/arch_win32.h b/src/arch_win32.h index ae0e0e54..648f2cab 100644 --- a/src/arch_win32.h +++ b/src/arch_win32.h @@ -12,8 +12,7 @@ typedef unsigned short uint16_t; typedef short int16_t; typedef unsigned int uint32_t; typedef int int32_t; -typedef int bool; -enum { false = 0, true = 1 }; +typedef enum { false = 0, true = 1 } bool; #else #include #include diff --git a/src/http.c b/src/http.c index c09b4f8f..4851ad10 100644 --- a/src/http.c +++ b/src/http.c @@ -383,10 +383,14 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm, fclose(fp); } else { struct http_data *d = (struct http_data *) calloc(1, sizeof(*d)); - d->fp = fp; - d->old_pfn_data = c->pfn_data; - c->pfn = static_cb; - c->pfn_data = d; + if (d == NULL) { + mg_error(c, "static HTTP OOM"); + } else { + d->fp = fp; + d->old_pfn_data = c->pfn_data; + c->pfn = static_cb; + c->pfn_data = d; + } } } } diff --git a/src/tls.c b/src/tls.c index d8e102ce..a2191d4f 100644 --- a/src/tls.c +++ b/src/tls.c @@ -64,6 +64,10 @@ static void debug_cb(void *c, int lev, const char *s, int n, const char *s2) { int mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) { struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls)); int rc = 0; + if (tls == NULL) { + mg_error(c, "TLS OOM"); + goto fail; + } LOG(LL_DEBUG, ("%lu Setting TLS, CA: %s, cert: %s, key: %s", c->id, opts->ca == NULL ? "null" : opts->ca, opts->cert == NULL ? "null" : opts->cert, @@ -202,6 +206,12 @@ int mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) { const char *id = "mongoose"; static unsigned char s_initialised = 0; int rc; + + if (tls == NULL) { + mg_error(c, "TLS OOM"); + goto fail; + } + if (!s_initialised) { SSL_library_init(); s_initialised++;