From 516f388585e5b4d419d964505b969e9003f5c14d Mon Sep 17 00:00:00 2001 From: Andy Green Date: Sat, 23 Apr 2016 07:49:57 +0800 Subject: [PATCH] coverity 160166 readlink doesnt NUL terminate Ah a real bug... well done coverity, that could have been nasty. readlink unusually doesn't NUL terminate the result... take care about it. Signed-off-by: Andy Green --- lib/server.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/server.c b/lib/server.c index 2e1969e..d8096fc 100644 --- a/lib/server.c +++ b/lib/server.c @@ -233,6 +233,7 @@ int lws_http_serve(struct lws *wsi, char *uri, const char *origin) char path[256], sym[256]; unsigned char *p = (unsigned char *)sym + 32 + LWS_PRE, *start = p; unsigned char *end = p + sizeof(sym) - 32 - LWS_PRE; + size_t len; int n, spin = 0; snprintf(path, sizeof(path) - 1, "%s/%s", origin, uri); @@ -248,10 +249,12 @@ int lws_http_serve(struct lws *wsi, char *uri, const char *origin) lwsl_debug(" %s mode %d\n", path, S_IFMT & st.st_mode); #if !defined(WIN32) if ((S_IFMT & st.st_mode) == S_IFLNK) { - if (readlink(path, sym, sizeof(sym))) { + len = readlink(path, sym, sizeof(sym) - 1); + if (len) { lwsl_err("Failed to read link %s\n", path); goto bail; } + sym[len] = '\0'; lwsl_debug("symlink %s -> %s\n", path, sym); snprintf(path, sizeof(path) - 1, "%s", sym); } -- 2.7.4