From e413fd545b074f164fca706435d2d5826e22dcac Mon Sep 17 00:00:00 2001 From: Olaf Bergmann Date: Fri, 19 Jun 2020 15:44:43 +0200 Subject: [PATCH] coap-server.c: avoid fopen on NULL pointer in read_file_mem As the behavior for calling fopen() on a NULL pointer is undefined in ISO C99, a check is added before passing the file name to fopen(). This fixes CID 1495723. --- examples/coap-server.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/coap-server.c b/examples/coap-server.c index 62a87af3..ade937ef 100644 --- a/examples/coap-server.c +++ b/examples/coap-server.c @@ -735,12 +735,12 @@ verify_cn_callback(const char *cn, } static uint8_t *read_file_mem(const char* file, size_t *length) { - FILE *f = fopen(file, "r"); + FILE *f; uint8_t *buf; struct stat statbuf; *length = 0; - if (!f) + if (!file || !(f = fopen(file, "r"))) return NULL; if (fstat(fileno(f), &statbuf) == -1) {