coverity 160166 readlink doesnt NUL terminate
authorAndy Green <andy@warmcat.com>
Fri, 22 Apr 2016 23:49:57 +0000 (07:49 +0800)
committerAndy Green <andy@warmcat.com>
Sat, 23 Apr 2016 00:16:17 +0000 (08:16 +0800)
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 <andy@warmcat.com>
lib/server.c

index 2e1969e..d8096fc 100644 (file)
@@ -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);
                }