diff options
author | José Bollo <jose.bollo@iot.bzh> | 2016-03-16 15:47:37 +0100 |
---|---|---|
committer | José Bollo <jose.bollo@iot.bzh> | 2016-03-16 15:47:37 +0100 |
commit | 2f5957984835ffc7d4f03bb0094a93c575e3eb17 (patch) | |
tree | ad686e9afdb661f0f456c422618c0d28a576ea63 | |
parent | 2a94a1d9c8b6c9bd4a83b870dee1d3072120cb77 (diff) |
Add warning detection and improve
Change-Id: Id4fc86b86d55aac578d84b4d2357c1a2e600c58b
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
-rw-r--r-- | CMakeLists.txt | 18 | ||||
-rw-r--r-- | plugins/afm-main-plugin/afm-main-plugin.c | 5 | ||||
-rw-r--r-- | plugins/samples/ClientCtx.c | 2 | ||||
-rw-r--r-- | plugins/samples/HelloWorld.c | 2 | ||||
-rw-r--r-- | plugins/session/token-api.c | 2 | ||||
-rw-r--r-- | src/helper-api.c | 13 | ||||
-rw-r--r-- | src/http-svc.c | 28 | ||||
-rw-r--r-- | src/main.c | 14 | ||||
-rw-r--r-- | src/rest-api.c | 23 | ||||
-rw-r--r-- | src/session.c | 14 |
10 files changed, 58 insertions, 63 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt index 6049f875..a29646b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,24 @@ INCLUDE(CheckIncludeFiles) INCLUDE(CheckLibraryExists) INCLUDE(GNUInstallDirs) +########################################################################### + +add_compile_options(-Wall -Wextra -Wconversion) +add_compile_options(-Wno-unused-parameter) # frankly not using a parameter does it care? +add_compile_options(-Werror=maybe-uninitialized) +#add_compile_options(-Werror=implicit-function-declaration) +add_compile_options(-ffunction-sections -fdata-sections) +add_compile_options(-Wl,--gc-sections) +add_compile_options(-fPIC) + +set(CMAKE_C_FLAGS_PROFILING "-g -O0 -pg -Wp,-U_FORTIFY_SOURCE") +set(CMAKE_C_FLAGS_DEBUG "-g -O0 -ggdb -Wp,-U_FORTIFY_SOURCE") +set(CMAKE_C_FLAGS_RELEASE "-g -O2") +set(CMAKE_C_FLAGS_CCOV "-g -O2 --coverage") + +########################################################################### + + CHECK_INCLUDE_FILES(magic.h HAVE_MAGIC_H) CHECK_LIBRARY_EXISTS(magic magic_load "" HAVE_LIBMAGIC_SO) IF(HAVE_MAGIC_H) diff --git a/plugins/afm-main-plugin/afm-main-plugin.c b/plugins/afm-main-plugin/afm-main-plugin.c index 1fcc554b..2aa36763 100644 --- a/plugins/afm-main-plugin/afm-main-plugin.c +++ b/plugins/afm-main-plugin/afm-main-plugin.c @@ -69,11 +69,6 @@ static struct json_object *embed(AFB_request *request, const char *tag, struct j return result; } -static struct json_object *call(AFB_request *request, AFB_PostItem *item, const char *tag, struct json_object *(*fun)(AFB_request*,AFB_PostItem*)) -{ - return embed(request, tag, fun(request, item)); -} - static struct json_object *call_void(AFB_request *request, AFB_PostItem *item) { struct json_object *obj = jbus_call_sj_sync(jbus, request->api, "true"); diff --git a/plugins/samples/ClientCtx.c b/plugins/samples/ClientCtx.c index b4ac0e13..b2479f50 100644 --- a/plugins/samples/ClientCtx.c +++ b/plugins/samples/ClientCtx.c @@ -110,7 +110,7 @@ STATIC json_object* myClose (AFB_request *request) { } STATIC void freeCtxCB (MyClientContextT *ctx, MyPluginHandleT *handle, char *uuid) { - fprintf (stderr, "FreeCtxCB uuid=[%s] Plugin=[%s] count=[%d]", uuid, handle->anythingYouWant, ctx->count); + fprintf (stderr, "FreeCtxCB uuid=[%s] Plugin=[%s] count=[%d]", uuid, (char*)handle->anythingYouWant, ctx->count); free (ctx); // Note: handle should be free it is a static resource attached to plugin and not to session diff --git a/plugins/samples/HelloWorld.c b/plugins/samples/HelloWorld.c index d9106920..05bec957 100644 --- a/plugins/samples/HelloWorld.c +++ b/plugins/samples/HelloWorld.c @@ -20,7 +20,7 @@ #include "local-def.h" STATIC json_object* pingSample (AFB_request *request) { - static pingcount = 0; + static int pingcount = 0; json_object *response; char query [512]; int len; diff --git a/plugins/session/token-api.c b/plugins/session/token-api.c index 25d0b993..7073dac0 100644 --- a/plugins/session/token-api.c +++ b/plugins/session/token-api.c @@ -79,7 +79,7 @@ STATIC json_object* clientContextReset (AFB_request *request) { } // Close and Free context STATIC json_object* clientGetPing (AFB_request *request) { - static count=0; + static int count=0; json_object *jresp; jresp = json_object_new_object(); diff --git a/src/helper-api.c b/src/helper-api.c index 54093e40..b3763505 100644 --- a/src/helper-api.c +++ b/src/helper-api.c @@ -19,6 +19,8 @@ #include "../include/local-def.h" #include <dirent.h> +#include <sys/stat.h> +#include <sys/types.h> // handle to hold queryAll values @@ -30,7 +32,7 @@ typedef struct { // Sample Generic Ping Debug API PUBLIC json_object* getPingTest(AFB_request *request) { - static pingcount = 0; + static int pingcount = 0; json_object *response; char query [256]; char session[256]; @@ -62,6 +64,7 @@ STATIC int getQueryCB (void*handle, enum MHD_ValueKind kind, const char *key, co queryHandleT *query = (queryHandleT*)handle; query->idx += snprintf (&query->msg[query->idx],query->len," %s: \'%s\',", key, value); + return MHD_YES; /* continue to iterate */ } // Helper to retrieve argument from connection @@ -110,7 +113,7 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* AFB_PostHandle *postHandle = getPostHandle(request); AFB_PostCtx *postFileCtx; char filepath[512]; - int len; + ssize_t len; // This is called after PostForm and then after DonePostForm if (item == NULL) { @@ -134,7 +137,7 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* free (postFileCtx); return (jresp); } - +#if defined(PLEASE_FIX_ME_THE_ERROR_IS_postFileCtx_NOT_INITIALIZED) // Make sure it's a valid PostForm request if (!request->post && request->post->type != AFB_POST_FORM) { postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"This is not a valid PostForm request\n"); @@ -152,7 +155,7 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Buffer size NULL key=%s]\n", item->key); goto ExitOnError; } - +#endif // Extract Application Context from posthandle [NULL == 1st iteration] postFileCtx = (AFB_PostCtx*) postHandle->ctx; @@ -200,7 +203,7 @@ PUBLIC json_object* getPostFile (AFB_request *request, AFB_PostItem *item, char* // Check we successfully wrote full buffer len = write (postFileCtx->fd, item->data, item->len); - if (item->len != len) { + if ((ssize_t)item->len != len) { postFileCtx->jresp= jsonNewMessage(AFB_FAIL,"Fail to write file [%s] at [%s] error=\n", item->filename, strerror(errno)); goto ExitOnError; } diff --git a/src/http-svc.c b/src/http-svc.c index acc12a88..71e0a08f 100644 --- a/src/http-svc.c +++ b/src/http-svc.c @@ -29,6 +29,7 @@ POST https://www.gnu.org/software/libmicrohttpd/manual/html_node/microhttpd_002dpost.html#microhttpd_002dpost */ +#define _GNU_SOURCE #include <microhttpd.h> @@ -37,15 +38,9 @@ #include "../include/local-def.h" // let's compute fixed URL length only once -static apiUrlLen=0; -static baseUrlLen=0; -static rootUrlLen=0; - -// proto missing from GCC -char *strcasestr(const char *haystack, const char *needle); - -static int rqtcount = 0; // dummy request rqtcount to make each message be different -static int postcount = 0; +static size_t apiUrlLen=0; +static size_t baseUrlLen=0; +static size_t rootUrlLen=0; // try to open libmagic to handle mime types static AFB_error initLibMagic (AFB_session *session) { @@ -79,18 +74,17 @@ static void endRequest (void *cls, struct MHD_Connection *connection, void **con // Create check etag value -STATIC void computeEtag(char *etag, int maxlen, struct stat *sbuf) { - int time; +STATIC void computeEtag(char *etag, size_t maxlen, struct stat *sbuf) { + long time; time = sbuf->st_mtim.tv_sec; - snprintf(etag, maxlen, "%d", time); + snprintf(etag, maxlen, "%ld", time); } STATIC int servFile (struct MHD_Connection *connection, AFB_session *session, const char *url, AFB_staticfile *staticfile) { const char *etagCache, *mimetype; char etagValue[15]; - struct MHD_Response *response; + struct MHD_Response *response = NULL; struct stat sbuf; - int ret; if (fstat (staticfile->fd, &sbuf) != 0) { fprintf(stderr, "Fail to stat file: [%s] error:%s\n", staticfile->path, strerror(errno)); @@ -168,8 +162,6 @@ abortRequest: // this function return either Index.htlm or a redirect to /#!route to make angular happy STATIC int redirectHTML5(struct MHD_Connection *connection, AFB_session *session, const char* url) { - int fd; - int ret; struct MHD_Response *response; AFB_staticfile staticfile; @@ -187,7 +179,7 @@ STATIC int redirectHTML5(struct MHD_Connection *connection, AFB_session *session // minimal httpd file server for static HTML,JS,CSS,etc... STATIC int requestFile(struct MHD_Connection *connection, AFB_session *session, const char* url) { - int fd, ret, idx; + int ret, idx; AFB_staticfile staticfile; char *requestdir, *requesturl; @@ -286,7 +278,7 @@ PUBLIC AFB_error httpdStart(AFB_session *session) { | MHD_USE_TCP_FASTOPEN | MHD_USE_DEBUG , - session->config->httpdPort, // port + (uint16_t)session->config->httpdPort, // port &newClient, NULL, // Tcp Accept call back + extra attribute &newRequest, session, // Http Request Call back + extra attribute MHD_OPTION_NOTIFY_COMPLETED, &endRequest, NULL, @@ -117,7 +117,7 @@ static AFB_options cliOptions [] = { {SET_MODE ,1,"mode" , "set the mode: either local, remote or global"}, {SET_READYFD ,1,"readyfd" , "set the #fd to signal when ready"}, - {0, 0, 0} + {0, 0, NULL, NULL} }; static AFB_aliasdir aliasdir[MAX_ALIAS]; @@ -290,7 +290,7 @@ int main(int argc, char *argv[]) { // build GNU getopt info from cliOptions nbcmd = sizeof (cliOptions) / sizeof (AFB_options); - gnuOptions = malloc (sizeof (ggcOption) * nbcmd); + gnuOptions = malloc (sizeof (ggcOption) * (unsigned)nbcmd); for (ind=0; ind < nbcmd;ind++) { gnuOptions [ind].name = cliOptions[ind].name; gnuOptions [ind].has_arg = cliOptions[ind].has_arg; @@ -354,7 +354,7 @@ int main(int argc, char *argv[]) { aliascount++; } } else { - fprintf(stderr, "Too many aliases [max:%s] %s ignored\n", optarg, MAX_ALIAS-1); + fprintf(stderr, "Too many aliases [max:%d] %s ignored\n", MAX_ALIAS, optarg); } break; @@ -401,7 +401,7 @@ int main(int argc, char *argv[]) { case SET_USERID: if (optarg == 0) goto needValueForOption; - if (!sscanf (optarg, "%s", &cliconfig.setuid)) goto notAnInteger; + cliconfig.setuid = optarg; break; case SET_FAKE_MOD: @@ -662,14 +662,10 @@ errSessiondir: fprintf (stderr,"\nERR:AFB-daemon cannot read/write session dir\n\n"); exit (-1); -errSoundCard: - fprintf (stderr,"\nERR:AFB-daemon fail to probe sound cards\n\n"); - exit (-1); - exitInitLoop: // try to unlink pid file if any if (session->background && session->config->pidfile != NULL) unlink (session->config->pidfile); exit (-1); -}; /* END AFB-daemon() */ +} /* END AFB-daemon() */ diff --git a/src/rest-api.c b/src/rest-api.c index b83e8932..756dc6f1 100644 --- a/src/rest-api.c +++ b/src/rest-api.c @@ -111,7 +111,7 @@ STATIC AFB_error callPluginApi(AFB_request *request, int plugidx, void *context) } } // Trigger a timer to protect from unacceptable long time execution - alarm (request->config->apiTimeout); + alarm ((unsigned)request->config->apiTimeout); } // Out of SessionNone every call get a client context session @@ -127,7 +127,7 @@ STATIC AFB_error callPluginApi(AFB_request *request, int plugidx, void *context) goto ExitOnDone; }; - if (verbose) fprintf(stderr, "Plugin=[%s] Api=[%s] Middleware=[%d] Client=[0x%x] Uuid=[%s] Token=[%s]\n" + if (verbose) fprintf(stderr, "Plugin=[%s] Api=[%s] Middleware=[%d] Client=[%p] Uuid=[%s] Token=[%s]\n" , request->prefix, request->api, plugin->apis[idx].session, clientCtx, clientCtx->uuid, clientCtx->token); switch(plugin->apis[idx].session) { @@ -285,7 +285,7 @@ STATIC int doPostIterate (void *cls, enum MHD_ValueKind kind, const char *key, AFB_PostRequest postRequest; if (verbose) - fprintf (stderr, "postHandle key=%s filename=%s len=%d mime=%s\n", key, filename, size, mimetype); + fprintf (stderr, "postHandle key=%s filename=%s len=%zu mime=%s\n", key, filename, size, mimetype); // Create and Item value for Plugin API item.kind = kind; @@ -322,7 +322,6 @@ STATIC void freeRequest (AFB_request *request) { STATIC AFB_request *createRequest (struct MHD_Connection *connection, AFB_session *session, const char* url) { AFB_request *request; - int idx; // Start with a clean request request = calloc (1, sizeof (AFB_request)); @@ -374,7 +373,7 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co AFB_PostRequest postRequest; int ret; - // fprintf (stderr, "doRestAPI method=%s posthandle=0x%x\n", method, con_cls); + // fprintf (stderr, "doRestAPI method=%s posthandle=%p\n", method, con_cls); // if post data may come in multiple calls if (0 == strcmp(method, MHD_HTTP_METHOD_POST)) { @@ -401,7 +400,7 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co // Form post is handle through a PostProcessor and call API once per form key if (strcasestr(encoding, FORM_CONTENT) != NULL) { - if (verbose) fprintf(stderr, "Create doPostIterate[uid=%d posthandle=0x%x]\n", postHandle->uid, postHandle); + if (verbose) fprintf(stderr, "Create doPostIterate[uid=%d posthandle=%p]\n", postHandle->uid, postHandle); request = createRequest (connection, session, url); if (request->jresp != NULL) goto ProcessApiCall; @@ -431,7 +430,7 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co // Size is OK, let's allocate a buffer to hold post data postHandle->type = AFB_POST_JSON; - postHandle->privatebuf = malloc(contentlen + 1); // allocate memory for full POST data + 1 for '\0' enf of string + postHandle->privatebuf = malloc((unsigned)contentlen + 1); // allocate memory for full POST data + 1 for '\0' enf of string // if (verbose) fprintf(stderr, "Create PostJson[uid=%d] Size=%d\n", postHandle->uid, contentlen); return MHD_YES; @@ -566,10 +565,10 @@ STATIC AFB_plugin ** RegisterJsonPlugins(AFB_plugin **plugins) { for (jdx = 0; plugins[idx]->apis[jdx].name != NULL; jdx++) { AFB_privateApi *privateapi = malloc (sizeof (AFB_privateApi)); if (plugins[idx]->apis[jdx].privateapi != NULL) { - fprintf (stderr, "WARNING: plugin=%s api=%s private handle should be NULL=0x%x\n" + fprintf (stderr, "WARNING: plugin=%s api=%s private handle should be NULL=%p\n" ,plugins[idx]->prefix,plugins[idx]->apis[jdx].name, plugins[idx]->apis[jdx].privateapi); } - privateapi->len = strlen (plugins[idx]->apis[jdx].name); + privateapi->len = (int)strlen (plugins[idx]->apis[jdx].name); privateapi->jtype=json_object_new_string(plugins[idx]->apis[jdx].name); json_object_get(privateapi->jtype); // increase reference count to make it permanent plugins[idx]->apis[jdx].privateapi = privateapi; @@ -633,7 +632,7 @@ STATIC void scanDirectory(char *dirpath, int dirfd, AFB_plugin **plugins, int *c // if max plugin is reached let's stop searching if (*count == AFB_MAX_PLUGINS) { - fprintf(stderr, "[%s] is not loaded [Max Count=%d reached]\n", *count); + fprintf(stderr, "[%s] is not loaded [Max Count=%d reached]\n", pluginDir.d_name, *count); continue; } @@ -660,7 +659,7 @@ void initPlugins(AFB_session *session) { plugins = (AFB_plugin **) malloc (AFB_MAX_PLUGINS *sizeof(AFB_plugin*)); // Loop on every directory passed in --plugins=xxx - while (dirpath = strsep(&session->config->ldpaths, ":")) { + while ((dirpath = strsep(&session->config->ldpaths, ":"))) { // Ignore any directory we fail to open if ((dirfd = open(dirpath, O_DIRECTORY)) <= 0) { fprintf(stderr, "Invalid directory path=[%s]\n", dirpath); @@ -676,7 +675,7 @@ void initPlugins(AFB_session *session) { } // downsize structure to effective number of loaded plugins - plugins = (AFB_plugin **)realloc (plugins, (count+1)*sizeof(AFB_plugin*)); + plugins = (AFB_plugin **)realloc (plugins, (unsigned)(count+1)*sizeof(AFB_plugin*)); plugins[count] = NULL; // complete plugins and save them within current sessions diff --git a/src/session.c b/src/session.c index 481e04c7..66876cd9 100644 --- a/src/session.c +++ b/src/session.c @@ -338,10 +338,9 @@ STATIC void ctxUuidFreeCB (AFB_clientCtx *client) { // Create a new store in RAM, not that is too small it will be automatically extended PUBLIC void ctxStoreInit (int nbSession) { - int res; // let's create as store as hashtable does not have any - sessions.store = calloc (nbSession+1, sizeof(AFB_clientCtx)); + sessions.store = calloc (1 + (unsigned)nbSession, sizeof(AFB_clientCtx)); sessions.max=nbSession; } @@ -421,7 +420,7 @@ STATIC int ctxStoreToOld (AFB_clientCtx *ctx, int timeout) { } // Loop on every entry and remove old context sessions.hash -PUBLIC int ctxStoreGarbage (const int timeout) { +PUBLIC void ctxStoreGarbage (const int timeout) { AFB_clientCtx *ctx; long idx; @@ -439,7 +438,6 @@ PUBLIC AFB_clientCtx *ctxClientGet (AFB_request *request, int idx) { AFB_clientCtx *clientCtx=NULL; const char *uuid; uuid_t newuuid; - int ret; if (request->config->token == NULL) return NULL; @@ -457,7 +455,6 @@ PUBLIC AFB_clientCtx *ctxClientGet (AFB_request *request, int idx) { // Warning when no cookie defined MHD_lookup_connection_value may return something !!! if ((uuid != NULL) && (strnlen (uuid, 10) >= 10)) { - int search; // search if client context exist and it not timeout let's use it clientCtx = ctxStoreSearch (uuid); @@ -478,7 +475,7 @@ PUBLIC AFB_clientCtx *ctxClientGet (AFB_request *request, int idx) { // we have no session let's create one otherwise let's clean any exiting values if (clientCtx == NULL) { clientCtx = calloc(1, sizeof(AFB_clientCtx)); // init NULL clientContext - clientCtx->contexts = calloc (1, request->config->pluginCount * (sizeof (void*))); + clientCtx->contexts = calloc (1, (unsigned)request->config->pluginCount * (sizeof (void*))); clientCtx->plugins = request->plugins; } @@ -524,7 +521,6 @@ PUBLIC AFB_error ctxTokenCheck (AFB_clientCtx *clientCtx, AFB_request *request) // Free Client Session Context PUBLIC AFB_error ctxTokenReset (AFB_clientCtx *clientCtx, AFB_request *request) { - int ret; if (clientCtx == NULL) return AFB_EMPTY; //if (verbose) fprintf (stderr, "ctxClientReset New uuid=[%s] token=[%s] timestamp=%d\n", clientCtx->uuid, clientCtx->token, clientCtx->timeStamp); @@ -541,8 +537,6 @@ PUBLIC AFB_error ctxTokenReset (AFB_clientCtx *clientCtx, AFB_request *request) // generate a new token PUBLIC AFB_error ctxTokenCreate (AFB_clientCtx *clientCtx, AFB_request *request) { - int oldTnkValid; - const char *ornew; uuid_t newuuid; const char *token; @@ -573,8 +567,6 @@ PUBLIC AFB_error ctxTokenCreate (AFB_clientCtx *clientCtx, AFB_request *request) // generate a new token and update client context PUBLIC AFB_error ctxTokenRefresh (AFB_clientCtx *clientCtx, AFB_request *request) { - int oldTnkValid; - const char *oldornew; uuid_t newuuid; if (clientCtx == NULL) return AFB_EMPTY; |