diff --git a/core/observe.c b/core/observe.c index 6051f1229..31370decd 100644 --- a/core/observe.c +++ b/core/observe.c @@ -746,15 +746,12 @@ void observe_step(lwm2m_context_t * contextP, { LOG_ARG("Checking minimal period (%d s)", watcherP->parameters->minPeriod); - if (watcherP->lastTime + watcherP->parameters->minPeriod > currentTime) - { + if ((time_t)(watcherP->lastTime + watcherP->parameters->minPeriod) > currentTime) { // Minimum Period did not elapse yet interval = watcherP->lastTime + watcherP->parameters->minPeriod - currentTime; if (*timeoutP > interval) *timeoutP = interval; notify = false; - } - else - { + } else { LOG("Notify on minimal period"); notify = true; } @@ -768,8 +765,7 @@ void observe_step(lwm2m_context_t * contextP, { LOG_ARG("Checking maximal period (%d s)", watcherP->parameters->maxPeriod); - if (watcherP->lastTime + watcherP->parameters->maxPeriod <= currentTime) - { + if ((time_t)(watcherP->lastTime + watcherP->parameters->maxPeriod) <= currentTime) { LOG("Notify on maximal period"); notify = true; } diff --git a/data/senml_json.c b/data/senml_json.c index a6b40ba59..3df7d6cca 100644 --- a/data/senml_json.c +++ b/data/senml_json.c @@ -178,10 +178,13 @@ static int prv_parseItem(const uint8_t * buffer, } break; case LWM2M_TYPE_FLOAT: + _Pragma("GCC diagnostic push"); + _Pragma("GCC diagnostic ignored \"-Wfloat-equal\""); if (baseValue->value.asFloat == 0.0) { baseValue->type = LWM2M_TYPE_UNDEFINED; } + _Pragma("GCC diagnostic pop"); break; default: return -1; diff --git a/examples/bootstrap_server/bootstrap_info.c b/examples/bootstrap_server/bootstrap_info.c index e73b6931c..80cbf89b7 100644 --- a/examples/bootstrap_server/bootstrap_info.c +++ b/examples/bootstrap_server/bootstrap_info.c @@ -47,9 +47,7 @@ typedef struct #endif } read_server_t; -static int prv_find_next_section(FILE * fd, - char * tag) -{ +static int prv_find_next_section(FILE *fd, const char *tag) { char * line; size_t length; int found; diff --git a/examples/bootstrap_server/bootstrap_server.c b/examples/bootstrap_server/bootstrap_server.c index 0f6befb3d..fb40e207f 100644 --- a/examples/bootstrap_server/bootstrap_server.c +++ b/examples/bootstrap_server/bootstrap_server.c @@ -80,9 +80,7 @@ void handle_sigint(int signum) prv_quit(NULL, NULL, NULL); } -void print_usage(char * filename, - char * port) -{ +void print_usage(const char *filename, const char *port) { fprintf(stdout, "Usage: bootstap_server [OPTION]\r\n"); fprintf(stderr, "Launch a LWM2M Bootstrap Server.\r\n\n"); fprintf(stdout, "Options:\r\n"); @@ -333,7 +331,7 @@ static void prv_send_command(lwm2m_context_t *lwm2mH, } static int prv_bootstrap_callback(lwm2m_context_t *lwm2mH, void *sessionH, uint8_t status, lwm2m_uri_t *uriP, - char *name, lwm2m_media_type_t format, uint8_t *data, size_t dataLength, + const char *name, lwm2m_media_type_t format, uint8_t *data, size_t dataLength, void *userData) { internal_data_t * dataP = (internal_data_t *)userData; endpoint_t * endP; @@ -486,7 +484,7 @@ static void prv_bootstrap_client(lwm2m_context_t *lwm2mH, { internal_data_t * dataP = (internal_data_t *)user_data; char * uri; - char * name = ""; + const char *name = ""; char * end = NULL; char * host; char * port; @@ -558,9 +556,9 @@ int main(int argc, char *argv[]) fd_set readfds; struct timeval tv; int result; - char * port = "5685"; + const char *port = "5685"; internal_data_t data; - char * filename = "bootstrap_server.ini"; + const char *filename = "bootstrap_server.ini"; int opt; FILE * fd; lwm2m_context_t * lwm2mH; @@ -721,7 +719,7 @@ int main(int argc, char *argv[]) else { char s[INET6_ADDRSTRLEN]; - in_port_t port; + in_port_t rec_port; connection_t * connP; s[0] = 0; @@ -729,16 +727,16 @@ int main(int argc, char *argv[]) { struct sockaddr_in *saddr = (struct sockaddr_in *)&addr; inet_ntop(saddr->sin_family, &saddr->sin_addr, s, INET6_ADDRSTRLEN); - port = saddr->sin_port; + rec_port = saddr->sin_port; } else if (AF_INET6 == addr.ss_family) { struct sockaddr_in6 *saddr = (struct sockaddr_in6 *)&addr; inet_ntop(saddr->sin6_family, &saddr->sin6_addr, s, INET6_ADDRSTRLEN); - port = saddr->sin6_port; + rec_port = saddr->sin6_port; } - fprintf(stderr, "%zd bytes received from [%s]:%hu\r\n", numBytes, s, ntohs(port)); + fprintf(stderr, "%zd bytes received from [%s]:%hu\r\n", numBytes, s, ntohs(rec_port)); output_buffer(stderr, buffer, (size_t)numBytes, 0); diff --git a/examples/client/lwm2mclient.c b/examples/client/lwm2mclient.c index c6cb11536..22111c8f5 100644 --- a/examples/client/lwm2mclient.c +++ b/examples/client/lwm2mclient.c @@ -831,6 +831,9 @@ static void prv_display_objects(lwm2m_context_t *lwm2mH, char *buffer, void *use case TEST_OBJECT_ID: display_test_object(object); break; + default: + fprintf(stdout, "unknown object ID: %" PRIu16 "\n", object->objID); + break; } } } @@ -866,7 +869,7 @@ int main(int argc, char *argv[]) const char * localPort = "56830"; const char * server = NULL; const char * serverPort = LWM2M_STANDARD_PORT_STR; - char * name = "testlwm2mclient"; + const char *name = "testlwm2mclient"; int lifetime = 300; int batterylevelchanging = 0; time_t reboot_time = 0; @@ -1382,7 +1385,7 @@ int main(int argc, char *argv[]) * Let liblwm2m respond to the query depending on the context */ #ifdef WITH_TINYDTLS - int result = connection_handle_packet(connP, buffer, numBytes); + result = connection_handle_packet(connP, buffer, numBytes); if (0 != result) { printf("error handling message %d\n",result); diff --git a/examples/lightclient/lightclient.c b/examples/lightclient/lightclient.c index d411d8eb0..bad379245 100644 --- a/examples/lightclient/lightclient.c +++ b/examples/lightclient/lightclient.c @@ -329,7 +329,7 @@ int main(int argc, char *argv[]) lwm2m_object_t * objArray[OBJ_COUNT]; const char * localPort = "56830"; - char * name = "testlwm2mclient"; + const char *name = "testlwm2mclient"; int result; int opt; diff --git a/examples/lightclient/object_security.c b/examples/lightclient/object_security.c index ef80cde89..2c52fab22 100644 --- a/examples/lightclient/object_security.c +++ b/examples/lightclient/object_security.c @@ -110,7 +110,7 @@ static uint8_t prv_get_value(lwm2m_data_t * dataP, case LWM2M_SECURITY_SMS_KEY_PARAM_ID: // Here we return an opaque of 6 bytes containing a buggy value { - char * value = "12345"; + const char *value = "12345"; lwm2m_data_encode_opaque((uint8_t *)value, 6, dataP); } return COAP_205_CONTENT; @@ -118,7 +118,7 @@ static uint8_t prv_get_value(lwm2m_data_t * dataP, case LWM2M_SECURITY_SMS_SECRET_KEY_ID: // Here we return an opaque of 32 bytes containing a buggy value { - char * value = "1234567890abcdefghijklmnopqrstu"; + const char *value = "1234567890abcdefghijklmnopqrstu"; lwm2m_data_encode_opaque((uint8_t *)value, 32, dataP); } return COAP_205_CONTENT; diff --git a/examples/shared/commandline.c b/examples/shared/commandline.c index f8a8aefb9..e73dd2517 100644 --- a/examples/shared/commandline.c +++ b/examples/shared/commandline.c @@ -267,7 +267,7 @@ void output_tlv(FILE * stream, uint8_t tmp; print_indent(stream, indent+2); - fprintf(stream, "data (%ld bytes):\r\n", dataLen); + fprintf(stream, "data (%zu bytes):\r\n", dataLen); output_buffer(stream, (uint8_t*)buffer + length + dataIndex, dataLen, indent+2); tmp = buffer[length + dataIndex + dataLen]; diff --git a/examples/shared/commandline.h b/examples/shared/commandline.h index 2c273b3b8..e5f491d8f 100644 --- a/examples/shared/commandline.h +++ b/examples/shared/commandline.h @@ -23,9 +23,9 @@ typedef void (*command_handler_t) (lwm2m_context_t *lwm2mH, char * args, void * typedef struct { - char * name; - char * shortDesc; - char * longDesc; + const char *name; + const char *shortDesc; + const char *longDesc; command_handler_t callback; void * userData; } command_desc_t; diff --git a/examples/shared/connection.c b/examples/shared/connection.c index e6cb94ac3..14d0fbffe 100644 --- a/examples/shared/connection.c +++ b/examples/shared/connection.c @@ -189,7 +189,7 @@ int connection_send(connection_t *connP, return -1; } - fprintf(stderr, "Sending %lu bytes to [%s]:%hu\r\n", length, s, ntohs(port)); + fprintf(stderr, "Sending %zu bytes to [%s]:%hu\r\n", length, s, ntohs(port)); output_buffer(stderr, buffer, length, 0); #endif @@ -215,13 +215,13 @@ uint8_t lwm2m_buffer_send(void * sessionH, if (connP == NULL) { - fprintf(stderr, "#> failed sending %lu bytes, missing connection\r\n", length); + fprintf(stderr, "#> failed sending %zu bytes, missing connection\r\n", length); return COAP_500_INTERNAL_SERVER_ERROR ; } if (-1 == connection_send(connP, buffer, length)) { - fprintf(stderr, "#> failed sending %lu bytes\r\n", length); + fprintf(stderr, "#> failed sending %zu bytes\r\n", length); return COAP_500_INTERNAL_SERVER_ERROR ; } diff --git a/examples/shared/dtlsconnection.c b/examples/shared/dtlsconnection.c index f61239351..0d9b7cac5 100644 --- a/examples/shared/dtlsconnection.c +++ b/examples/shared/dtlsconnection.c @@ -34,10 +34,9 @@ typedef struct _dtls_app_context_ dtls_connection_t * connList; } dtls_app_context_t; -dtls_app_context_t appContext; - /********************* Security Obj Helpers **********************/ -char * security_get_uri(lwm2m_context_t * lwm2mH, lwm2m_object_t * obj, int instanceId, char * uriBuffer, int bufferSize){ +char *security_get_uri(lwm2m_context_t *lwm2mH, lwm2m_object_t *obj, int instanceId, char *uriBuffer, + size_t bufferSize) { int size = 1; lwm2m_data_t * dataP = lwm2m_data_new(size); dataP->id = 0; // security server uri @@ -76,7 +75,7 @@ int64_t security_get_mode(lwm2m_context_t * lwm2mH, lwm2m_object_t * obj, int in return LWM2M_SECURITY_MODE_NONE; } -char * security_get_public_id(lwm2m_context_t * lwm2mH, lwm2m_object_t * obj, int instanceId, int * length){ +char *security_get_public_id(lwm2m_context_t *lwm2mH, lwm2m_object_t *obj, int instanceId, size_t *length) { int size = 1; lwm2m_data_t * dataP = lwm2m_data_new(size); dataP->id = 3; // public key or id @@ -101,8 +100,7 @@ char * security_get_public_id(lwm2m_context_t * lwm2mH, lwm2m_object_t * obj, in } } - -char * security_get_secret_key(lwm2m_context_t * lwm2mH, lwm2m_object_t * obj, int instanceId, int * length){ +char *security_get_secret_key(lwm2m_context_t *lwm2mH, lwm2m_object_t *obj, int instanceId, size_t *length) { int size = 1; lwm2m_data_t * dataP = lwm2m_data_new(size); dataP->id = 5; // secret key @@ -196,22 +194,22 @@ static int get_psk_info(struct dtls_context_t *ctx, switch (type) { case DTLS_PSK_IDENTITY: { - int idLen; - char * id; - id = security_get_public_id(appContext->lwm2mH, cnx->securityObj, cnx->securityInstId, &idLen); + size_t idLen; + char *id2; + id2 = security_get_public_id(appContext->lwm2mH, cnx->securityObj, cnx->securityInstId, &idLen); if (result_length < idLen) { printf("cannot set psk_identity -- buffer too small\n"); return dtls_alert_fatal_create(DTLS_ALERT_INTERNAL_ERROR); } - memcpy(result, id,idLen); - lwm2m_free(id); + memcpy(result, id2, idLen); + lwm2m_free(id2); return idLen; } case DTLS_PSK_KEY: { - int keyLen; + size_t keyLen; char * key; key = security_get_secret_key(appContext->lwm2mH, cnx->securityObj, cnx->securityInstId, &keyLen); @@ -292,6 +290,7 @@ static dtls_handler_t cb = { }; dtls_context_t * get_dtls_context(lwm2m_context_t * lwm2mH, dtls_connection_t * connList) { + static dtls_app_context_t appContext; appContext.lwm2mH = lwm2mH; appContext.connList = connList; if (dtlsContext == NULL) { @@ -462,16 +461,16 @@ dtls_connection_t * connection_create(dtls_connection_t * connList, if (uri == NULL) return NULL; // parse uri in the form "coaps://[host]:[port]" - char * defaultport; + char defaultport[5]; if (0 == strncmp(uri, "coaps://", strlen("coaps://"))) { host = uri+strlen("coaps://"); - defaultport = COAPS_PORT; + strncpy(defaultport, COAPS_PORT, 5); } else if (0 == strncmp(uri, "coap://", strlen("coap://"))) { host = uri+strlen("coap://"); - defaultport = COAP_PORT; + strncpy(defaultport, COAP_PORT, 5); } else { @@ -641,13 +640,13 @@ uint8_t lwm2m_buffer_send(void * sessionH, if (connP == NULL) { - fprintf(stderr, "#> failed sending %lu bytes, missing connection\r\n", length); + fprintf(stderr, "#> failed sending %zu bytes, missing connection\r\n", length); return COAP_500_INTERNAL_SERVER_ERROR ; } if (-1 == connection_send(connP, buffer, length)) { - fprintf(stderr, "#> failed sending %lu bytes\r\n", length); + fprintf(stderr, "#> failed sending %zu bytes\r\n", length); return COAP_500_INTERNAL_SERVER_ERROR ; } diff --git a/examples/shared/tinydtls.cmake b/examples/shared/tinydtls.cmake index 427588712..a077a34f6 100644 --- a/examples/shared/tinydtls.cmake +++ b/examples/shared/tinydtls.cmake @@ -81,6 +81,7 @@ endif() # Add tinydtls sources to an existing target. function(target_sources_tinydtls target) target_sources(${target} PRIVATE ${TINYDTLS_SOURCES} ${TINYDTLS_SOURCES_GENERATED}) + set_source_files_properties(${TINYDTLS_SOURCES} PROPERTIES COMPILE_FLAGS -Wno-cast-align) target_compile_definitions(${target} PRIVATE WITH_SHA256 SHA2_USE_INTTYPES_H) target_include_directories(${target} PRIVATE ${TINYDTLS_SOURCES_DIR}) endfunction() diff --git a/include/liblwm2m.h b/include/liblwm2m.h index 7575f2aa9..b4b3a8219 100644 --- a/include/liblwm2m.h +++ b/include/liblwm2m.h @@ -765,7 +765,7 @@ typedef enum // client, the URI of the operation (may be nil) and name is nil. The callback return value is ignored. // If data is present and no preferred format is provided by the client the format will be 0, otherwise it will be set. typedef int (*lwm2m_bootstrap_callback_t)(lwm2m_context_t *contextP, void *sessionH, uint8_t status, lwm2m_uri_t *uriP, - char *name, lwm2m_media_type_t format, uint8_t *data, size_t dataLength, + const char *name, lwm2m_media_type_t format, uint8_t *data, size_t dataLength, void *userData); #endif diff --git a/wakaama.cmake b/wakaama.cmake index 33b4d54e4..9a55aa3f2 100644 --- a/wakaama.cmake +++ b/wakaama.cmake @@ -109,7 +109,7 @@ function(target_sources_shared target) target_include_directories(${target} PUBLIC ${WAKAAMA_EXAMPLE_SHARED_DIRECTORY}) endfunction() -# Warn about problematic code (not yet enforced) +# Enforce a certain level of hygiene add_compile_options( -Waggregate-return -Wall @@ -120,13 +120,17 @@ add_compile_options( -Wshadow -Wswitch-default -Wwrite-strings - -Werror=absolute-value -pedantic -) -# Turn certain warnings into errors -add_compile_options( - -Werror=strict-prototypes + # Reduce noise: Unused parameters are common in this ifdef-littered code-base, but of no danger + -Wno-unused-parameter + # Reduce noise: Too many false positives + -Wno-uninitialized + + # Turn (most) warnings into errors + -Werror + # Disabled because of existing, non-trivially fixable code + -Wno-error=cast-align ) # The maximum buffer size that is provided for resource responses and must be respected due to the limited IP buffer.