Handle calloc failures

This commit is contained in:
cpq committed 2021-01-26 12:16:58 +00:00
1 parent 7200f079bf
commit 8e52075636
5 files changed
+30 -4

No files matched your search

+14
View File
@@ -801,6 +801,9 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
fclose(fp); fclose(fp);
} else { } else {
struct http_data *d = (struct http_data *) calloc(1, sizeof(*d)); struct http_data *d = (struct http_data *) calloc(1, sizeof(*d));
if (d == NULL) {
mg_error(c, "static HTTP OOM");
} else {
d->fp = fp; d->fp = fp;
d->old_pfn_data = c->pfn_data; d->old_pfn_data = c->pfn_data;
c->pfn = static_cb; c->pfn = static_cb;
@@ -808,6 +811,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
} }
} }
} }
}
#if MG_ARCH == MG_ARCH_ESP32 || MG_ARCH == MG_ARCH_ESP8266 || \ #if MG_ARCH == MG_ARCH_ESP32 || MG_ARCH == MG_ARCH_ESP8266 || \
MG_ARCH == MG_ARCH_FREERTOS MG_ARCH == MG_ARCH_FREERTOS
@@ -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) { int mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls)); struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls));
int rc = 0; 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, LOG(LL_DEBUG, ("%lu Setting TLS, CA: %s, cert: %s, key: %s", c->id,
opts->ca == NULL ? "null" : opts->ca, opts->ca == NULL ? "null" : opts->ca,
opts->cert == NULL ? "null" : opts->cert, 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"; const char *id = "mongoose";
static unsigned char s_initialised = 0; static unsigned char s_initialised = 0;
int rc; int rc;
if (tls == NULL) {
mg_error(c, "TLS OOM");
goto fail;
}
if (!s_initialised) { if (!s_initialised) {
SSL_library_init(); SSL_library_init();
s_initialised++; s_initialised++;
+1 -2
View File
@@ -233,8 +233,7 @@ typedef unsigned short uint16_t;
typedef short int16_t; typedef short int16_t;
typedef unsigned int uint32_t; typedef unsigned int uint32_t;
typedef int int32_t; typedef int int32_t;
typedef int bool; typedef enum { false = 0, true = 1 } bool;
enum { false = 0, true = 1 };
#else #else
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
+1 -2
View File
@@ -12,8 +12,7 @@ typedef unsigned short uint16_t;
typedef short int16_t; typedef short int16_t;
typedef unsigned int uint32_t; typedef unsigned int uint32_t;
typedef int int32_t; typedef int int32_t;
typedef int bool; typedef enum { false = 0, true = 1 } bool;
enum { false = 0, true = 1 };
#else #else
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
+4
View File
@@ -383,6 +383,9 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
fclose(fp); fclose(fp);
} else { } else {
struct http_data *d = (struct http_data *) calloc(1, sizeof(*d)); struct http_data *d = (struct http_data *) calloc(1, sizeof(*d));
if (d == NULL) {
mg_error(c, "static HTTP OOM");
} else {
d->fp = fp; d->fp = fp;
d->old_pfn_data = c->pfn_data; d->old_pfn_data = c->pfn_data;
c->pfn = static_cb; c->pfn = static_cb;
@@ -390,6 +393,7 @@ void mg_http_serve_file(struct mg_connection *c, struct mg_http_message *hm,
} }
} }
} }
}
#if MG_ARCH == MG_ARCH_ESP32 || MG_ARCH == MG_ARCH_ESP8266 || \ #if MG_ARCH == MG_ARCH_ESP32 || MG_ARCH == MG_ARCH_ESP8266 || \
MG_ARCH == MG_ARCH_FREERTOS MG_ARCH == MG_ARCH_FREERTOS
+10
View File
@@ -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) { int mg_tls_init(struct mg_connection *c, struct mg_tls_opts *opts) {
struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls)); struct mg_tls *tls = (struct mg_tls *) calloc(1, sizeof(*tls));
int rc = 0; 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, LOG(LL_DEBUG, ("%lu Setting TLS, CA: %s, cert: %s, key: %s", c->id,
opts->ca == NULL ? "null" : opts->ca, opts->ca == NULL ? "null" : opts->ca,
opts->cert == NULL ? "null" : opts->cert, 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"; const char *id = "mongoose";
static unsigned char s_initialised = 0; static unsigned char s_initialised = 0;
int rc; int rc;
if (tls == NULL) {
mg_error(c, "TLS OOM");
goto fail;
}
if (!s_initialised) { if (!s_initialised) {
SSL_library_init(); SSL_library_init();
s_initialised++; s_initialised++;