Avoid fortify false positive
authorJosé Bollo <jose.bollo@iot.bzh>
Thu, 25 Jul 2019 16:56:53 +0000 (18:56 +0200)
committerJosé Bollo <jose.bollo@iot.bzh>
Thu, 25 Jul 2019 17:53:03 +0000 (19:53 +0200)
Change-Id: Iceb888ed5cccc46bde9e479a2b1ae9a5a6c4ee53
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
CMakeLists.txt
src/cache.c

index d4ed7c5..80a68aa 100644 (file)
@@ -65,9 +65,10 @@ add_compile_options(-Werror=implicit-function-declaration)
 add_compile_options(-ffunction-sections -fdata-sections)
 add_compile_options(-fPIC)
 add_compile_options(-g)
+add_compile_options(-fstack-protector -D_FORTIFY_SOURCE=2 -O2)
 
-set(CMAKE_C_FLAGS_PROFILING    "-g -O2 -pg -Wp,-U_FORTIFY_SOURCE")
-set(CMAKE_C_FLAGS_DEBUG        "-g -ggdb -Wp,-U_FORTIFY_SOURCE")
+set(CMAKE_C_FLAGS_PROFILING    "-g -O2 -pg")
+set(CMAKE_C_FLAGS_DEBUG        "-g -ggdb --fstack-protector -D_FORTIFY_SOURCE=2")
 set(CMAKE_C_FLAGS_RELEASE      "-g -O2")
 set(CMAKE_C_FLAGS_CCOV         "-g -O2 --coverage")
 
index 0759f1f..5d38b89 100644 (file)
@@ -53,7 +53,7 @@ struct item
        int8_t value;
 
        /** fake ending character */
-       char strings;
+       char strings[];
 };
 typedef struct item item_t;
 
@@ -67,7 +67,7 @@ struct cache
        uint32_t cacheid;
        uint32_t used;
        uint32_t count;
-       uint8_t content[1];
+       uint8_t content[];
 };
 
 static
@@ -199,7 +199,7 @@ search(
                if (item->expire && item->expire < now)
                        drop_at(cache, iter);
                else {
-                       if (match(&item->strings, key))
+                       if (match(item->strings, key))
                                found = item;
                        iter += item->length;
                }
@@ -224,7 +224,7 @@ cache_put(
        item = search(cache, key);
        if (item == NULL) {
                /* create an item */
-               size = (size_t)(&((item_t*)0)->strings)
+               size = sizeof *item
                        + strlen(key->client)
                        + strlen(key->session)
                        + strlen(key->user)
@@ -239,7 +239,7 @@ cache_put(
                        drop_lre(cache);
                item = itemat(cache, cache->used);
                item->length = length;
-               stpcpy(1 + stpcpy(1 + stpcpy(1 + stpcpy(&item->strings, key->client), key->session), key->user), key->permission);
+               stpcpy(1 + stpcpy(1 + stpcpy(1 + stpcpy(item->strings, key->client), key->session), key->user), key->permission);
                cache->used += (uint32_t)size;
        }
        item->expire = expire;
@@ -291,7 +291,7 @@ cache_resize(
                        while (c->used > newsize)
                                drop_lre(c);
 
-               nc = realloc(c, newsize - 1 + sizeof *c);
+               nc = realloc(c, newsize + sizeof *c);
                if (nc == NULL)
                        return -ENOMEM;