From 214d6b738d4f586e63922361337415c45ad7a4d7 Mon Sep 17 00:00:00 2001 From: Falk Werner Date: Sat, 21 Mar 2020 21:22:22 +0100 Subject: [PATCH] removed NULL-checks after malloc: they are not necessary, they were not consequently used and objects constructed by 3rd party libs are also unchecked --- lib/webfuse/adapter/impl/authenticator.c | 11 +++---- lib/webfuse/adapter/impl/filesystem.c | 11 +++---- lib/webfuse/adapter/impl/operation/read.c | 2 +- lib/webfuse/adapter/impl/server.c | 11 +++---- lib/webfuse/adapter/impl/server_config.c | 5 +-- lib/webfuse/adapter/impl/server_protocol.c | 11 +++---- lib/webfuse/adapter/impl/session.c | 21 ++++++------- lib/webfuse/core/message.c | 9 ++---- lib/webfuse/core/string.c | 19 +++++------ lib/webfuse/provider/impl/client.c | 33 +++++++++----------- lib/webfuse/provider/impl/client_config.c | 11 +++---- lib/webfuse/provider/impl/client_protocol.c | 5 +-- lib/webfuse/provider/impl/dirbuffer.c | 5 +-- lib/webfuse/provider/impl/operation/read.c | 21 +++++-------- lib/webfuse/provider/impl/request.c | 9 ++---- lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.c | 11 +++---- lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.c | 5 +-- lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.c | 9 ++---- lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c | 5 +-- lib/wf/timer/src/wf/timer/impl/manager.c | 5 +-- test/webfuse/utils/path.c | 27 +++++++--------- test/webfuse/utils/static_filesystem.c | 23 ++++++-------- 22 files changed, 101 insertions(+), 168 deletions(-) diff --git a/lib/webfuse/adapter/impl/authenticator.c b/lib/webfuse/adapter/impl/authenticator.c index 0be4b62..562a1dd 100644 --- a/lib/webfuse/adapter/impl/authenticator.c +++ b/lib/webfuse/adapter/impl/authenticator.c @@ -11,13 +11,10 @@ struct wf_impl_authenticator * wf_impl_authenticator_create( void * user_data) { struct wf_impl_authenticator * authenticator = malloc(sizeof(struct wf_impl_authenticator)); - if (NULL != authenticator) - { - authenticator->type = strdup(type); - authenticator->authenticate = authenticate; - authenticator->user_data = user_data; - authenticator->next = NULL; - } + authenticator->type = strdup(type); + authenticator->authenticate = authenticate; + authenticator->user_data = user_data; + authenticator->next = NULL; return authenticator; } diff --git a/lib/webfuse/adapter/impl/filesystem.c b/lib/webfuse/adapter/impl/filesystem.c index 03c2217..b7966a1 100644 --- a/lib/webfuse/adapter/impl/filesystem.c +++ b/lib/webfuse/adapter/impl/filesystem.c @@ -99,14 +99,11 @@ struct wf_impl_filesystem * wf_impl_filesystem_create( struct wf_mountpoint * mountpoint) { struct wf_impl_filesystem * filesystem = malloc(sizeof(struct wf_impl_filesystem)); - if (NULL != filesystem) + bool success = wf_impl_filesystem_init(filesystem, session, name, mountpoint); + if (!success) { - bool success = wf_impl_filesystem_init(filesystem, session, name, mountpoint); - if (!success) - { - free(filesystem); - filesystem = NULL; - } + free(filesystem); + filesystem = NULL; } return filesystem; diff --git a/lib/webfuse/adapter/impl/operation/read.c b/lib/webfuse/adapter/impl/operation/read.c index 87330fc..8fa5bee 100644 --- a/lib/webfuse/adapter/impl/operation/read.c +++ b/lib/webfuse/adapter/impl/operation/read.c @@ -20,7 +20,7 @@ static char * wf_impl_fill_buffer( *status = WF_GOOD; char * buffer = malloc(count); - if ((NULL != buffer) && (0 < count)) + if (0 < count) { if (0 == strcmp("identity", format)) { diff --git a/lib/webfuse/adapter/impl/server.c b/lib/webfuse/adapter/impl/server.c index f6e1f63..8f9a01d 100644 --- a/lib/webfuse/adapter/impl/server.c +++ b/lib/webfuse/adapter/impl/server.c @@ -83,13 +83,10 @@ struct wf_server * wf_impl_server_create( if (wf_impl_mountpoint_factory_isvalid(&config->mountpoint_factory)) { server = malloc(sizeof(struct wf_server)); - if (NULL != server) - { - wf_impl_server_protocol_init(&server->protocol, &config->mountpoint_factory); - wf_impl_server_config_clone(config, &server->config); - wf_impl_authenticators_move(&server->config.authenticators, &server->protocol.authenticators); - server->context = wf_impl_server_context_create(server); - } + wf_impl_server_protocol_init(&server->protocol, &config->mountpoint_factory); + wf_impl_server_config_clone(config, &server->config); + wf_impl_authenticators_move(&server->config.authenticators, &server->protocol.authenticators); + server->context = wf_impl_server_context_create(server); } return server; diff --git a/lib/webfuse/adapter/impl/server_config.c b/lib/webfuse/adapter/impl/server_config.c index bcf022e..ab214f3 100644 --- a/lib/webfuse/adapter/impl/server_config.c +++ b/lib/webfuse/adapter/impl/server_config.c @@ -56,10 +56,7 @@ void wf_impl_server_config_clone( struct wf_server_config * wf_impl_server_config_create(void) { struct wf_server_config * config = malloc(sizeof(struct wf_server_config)); - if (NULL != config) - { - wf_impl_server_config_init(config); - } + wf_impl_server_config_init(config); return config; } diff --git a/lib/webfuse/adapter/impl/server_protocol.c b/lib/webfuse/adapter/impl/server_protocol.c index 927d1cb..ee5cbb0 100644 --- a/lib/webfuse/adapter/impl/server_protocol.c +++ b/lib/webfuse/adapter/impl/server_protocol.c @@ -85,14 +85,11 @@ struct wf_server_protocol * wf_impl_server_protocol_create( void * create_mountpoint_context) { struct wf_server_protocol * protocol = malloc(sizeof(struct wf_server_protocol)); - if (NULL != protocol) - { - struct wf_impl_mountpoint_factory mountpoint_factory; - wf_impl_mountpoint_factory_init(&mountpoint_factory, - create_mountpoint, create_mountpoint_context); + struct wf_impl_mountpoint_factory mountpoint_factory; + wf_impl_mountpoint_factory_init(&mountpoint_factory, + create_mountpoint, create_mountpoint_context); - wf_impl_server_protocol_init(protocol, &mountpoint_factory); - } + wf_impl_server_protocol_init(protocol, &mountpoint_factory); return protocol; diff --git a/lib/webfuse/adapter/impl/session.c b/lib/webfuse/adapter/impl/session.c index e1fdd87..778d155 100644 --- a/lib/webfuse/adapter/impl/session.c +++ b/lib/webfuse/adapter/impl/session.c @@ -51,18 +51,15 @@ struct wf_impl_session * wf_impl_session_create( { struct wf_impl_session * session = malloc(sizeof(struct wf_impl_session)); - if (NULL != session) - { - wf_slist_init(&session->filesystems); - - session->wsi = wsi; - session->is_authenticated = false; - session->authenticators = authenticators; - session->server = server; - session->mountpoint_factory = mountpoint_factory; - session->rpc = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session); - wf_slist_init(&session->messages); - } + wf_slist_init(&session->filesystems); + + session->wsi = wsi; + session->is_authenticated = false; + session->authenticators = authenticators; + session->server = server; + session->mountpoint_factory = mountpoint_factory; + session->rpc = wf_jsonrpc_proxy_create(timer_manager, WF_DEFAULT_TIMEOUT, &wf_impl_session_send, session); + wf_slist_init(&session->messages); return session; } diff --git a/lib/webfuse/core/message.c b/lib/webfuse/core/message.c index e94d1c6..bfb0016 100644 --- a/lib/webfuse/core/message.c +++ b/lib/webfuse/core/message.c @@ -12,13 +12,10 @@ extern struct wf_message * wf_message_create(json_t const * value) { char * data = malloc(sizeof(struct wf_message) + LWS_PRE + length); message = (struct wf_message *) data; - if (NULL != message) - { - message->data = &data[sizeof(struct wf_message) + LWS_PRE]; - message->length = length; + message->data = &data[sizeof(struct wf_message) + LWS_PRE]; + message->length = length; - json_dumpb(value, message->data, length, JSON_COMPACT); - } + json_dumpb(value, message->data, length, JSON_COMPACT); } return message; diff --git a/lib/webfuse/core/string.c b/lib/webfuse/core/string.c index c5117eb..84608cb 100644 --- a/lib/webfuse/core/string.c +++ b/lib/webfuse/core/string.c @@ -16,18 +16,15 @@ char * wf_create_string(char const * format, ...) if (0 <= needed) { result = malloc(needed + 1); - if (NULL != result) - { - va_list args; - va_start(args, format); - int count = vsnprintf(result, needed + 1, format, args); - va_end(args); + va_list args; + va_start(args, format); + int count = vsnprintf(result, needed + 1, format, args); + va_end(args); - if ((count < 0) || (needed < count)) - { - free(result); - result = NULL; - } + if ((count < 0) || (needed < count)) + { + free(result); + result = NULL; } } diff --git a/lib/webfuse/provider/impl/client.c b/lib/webfuse/provider/impl/client.c index 21af148..c3b432c 100644 --- a/lib/webfuse/provider/impl/client.c +++ b/lib/webfuse/provider/impl/client.c @@ -30,27 +30,24 @@ struct wfp_client * wfp_impl_client_create( wf_lwslog_disable(); struct wfp_client * client = malloc(sizeof(struct wfp_client)); - if (NULL != client) + wfp_impl_client_protocol_init(&client->protocol, &config->provider, config->user_data); + + memset(client->protocols, 0, sizeof(struct lws_protocols) * WFP_CLIENT_PROTOCOL_COUNT); + wfp_impl_client_protocol_init_lws(&client->protocol, &client->protocols[0]); + + memset(&client->info, 0, sizeof(struct lws_context_creation_info)); + client->info.port = CONTEXT_PORT_NO_LISTEN; + client->info.protocols = client->protocols; + client->info.uid = -1; + client->info.gid = -1; + + if ((NULL != config->cert_path) && (NULL != config->key_path)) { - wfp_impl_client_protocol_init(&client->protocol, &config->provider, config->user_data); - - memset(client->protocols, 0, sizeof(struct lws_protocols) * WFP_CLIENT_PROTOCOL_COUNT); - wfp_impl_client_protocol_init_lws(&client->protocol, &client->protocols[0]); - - memset(&client->info, 0, sizeof(struct lws_context_creation_info)); - client->info.port = CONTEXT_PORT_NO_LISTEN; - client->info.protocols = client->protocols; - client->info.uid = -1; - client->info.gid = -1; - - if ((NULL != config->cert_path) && (NULL != config->key_path)) - { - - } - - client->context = lws_create_context(&client->info); + } + client->context = lws_create_context(&client->info); + return client; } diff --git a/lib/webfuse/provider/impl/client_config.c b/lib/webfuse/provider/impl/client_config.c index 0e8afc6..3bbb3ea 100644 --- a/lib/webfuse/provider/impl/client_config.c +++ b/lib/webfuse/provider/impl/client_config.c @@ -6,13 +6,10 @@ struct wfp_client_config * wfp_impl_client_config_create(void) { struct wfp_client_config * config = malloc(sizeof(struct wfp_client_config)); - if (NULL != config) - { - wfp_impl_provider_init(&config->provider); - config->user_data = NULL; - config->key_path = NULL; - config->cert_path = NULL; - } + wfp_impl_provider_init(&config->provider); + config->user_data = NULL; + config->key_path = NULL; + config->cert_path = NULL; return config; } diff --git a/lib/webfuse/provider/impl/client_protocol.c b/lib/webfuse/provider/impl/client_protocol.c index 4f61b8a..4e2b176 100644 --- a/lib/webfuse/provider/impl/client_protocol.c +++ b/lib/webfuse/provider/impl/client_protocol.c @@ -270,10 +270,7 @@ struct wfp_client_protocol * wfp_impl_client_protocol_create( struct wfp_client_config const * config) { struct wfp_client_protocol * protocol = malloc(sizeof(struct wfp_client_protocol)); - if (NULL != protocol) - { - wfp_impl_client_protocol_init(protocol, &config->provider, config->user_data); - } + wfp_impl_client_protocol_init(protocol, &config->provider, config->user_data); return protocol; } diff --git a/lib/webfuse/provider/impl/dirbuffer.c b/lib/webfuse/provider/impl/dirbuffer.c index db401ef..9bc5f22 100644 --- a/lib/webfuse/provider/impl/dirbuffer.c +++ b/lib/webfuse/provider/impl/dirbuffer.c @@ -4,10 +4,7 @@ struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void) { struct wfp_dirbuffer * buffer = malloc(sizeof(struct wfp_dirbuffer)); - if (NULL != buffer) - { - buffer->entries = json_array(); - } + buffer->entries = json_array(); return buffer; } diff --git a/lib/webfuse/provider/impl/operation/read.c b/lib/webfuse/provider/impl/operation/read.c index 3101ea0..7be4bf3 100644 --- a/lib/webfuse/provider/impl/operation/read.c +++ b/lib/webfuse/provider/impl/operation/read.c @@ -56,22 +56,15 @@ void wfp_impl_respond_read( { size_t const size = wf_base64_encoded_size(length) + 1; char * buffer = malloc(size); - if (NULL != buffer) - { - wf_base64_encode((uint8_t const *) data, length, buffer, size); + wf_base64_encode((uint8_t const *) data, length, buffer, size); - json_t * result = json_object(); - json_object_set_new(result, "data", json_string(buffer)); - json_object_set_new(result, "format", json_string("base64")); - json_object_set_new(result, "count", json_integer((int) length)); + json_t * result = json_object(); + json_object_set_new(result, "data", json_string(buffer)); + json_object_set_new(result, "format", json_string("base64")); + json_object_set_new(result, "count", json_integer((int) length)); - wfp_impl_respond(request, result); - free(buffer); - } - else - { - wfp_impl_respond_error(request, WF_BAD); - } + wfp_impl_respond(request, result); + free(buffer); } else { diff --git a/lib/webfuse/provider/impl/request.c b/lib/webfuse/provider/impl/request.c index 42ea835..7f8b05f 100644 --- a/lib/webfuse/provider/impl/request.c +++ b/lib/webfuse/provider/impl/request.c @@ -8,12 +8,9 @@ struct wfp_request * wfp_impl_request_create( int id) { struct wfp_request * request = malloc(sizeof(struct wfp_request)); - if (NULL != request) - { - request->respond = prototype->respond; - request->user_data = prototype->user_data; - request->id = id; - } + request->respond = prototype->respond; + request->user_data = prototype->user_data; + request->id = id; return request; } diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.c b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.c index 87b3ff1..ef0d16c 100644 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.c +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/method.c @@ -8,13 +8,10 @@ struct wf_jsonrpc_method * wf_jsonrpc_impl_method_create( void * user_data) { struct wf_jsonrpc_method * method = malloc(sizeof(struct wf_jsonrpc_method)); - if (NULL != method) - { - method->next = NULL; - method->name = strdup(method_name); - method->invoke = invoke; - method->user_data = user_data; - } + method->next = NULL; + method->name = strdup(method_name); + method->invoke = invoke; + method->user_data = user_data; return method; } diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.c b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.c index a2b9816..99b3f03 100644 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.c +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/proxy.c @@ -16,10 +16,7 @@ wf_jsonrpc_impl_proxy_create( void * user_data) { struct wf_jsonrpc_proxy * proxy = malloc(sizeof(struct wf_jsonrpc_proxy)); - if (NULL != proxy) - { - wf_jsonrpc_impl_proxy_init(proxy, manager, timeout, send, user_data); - } + wf_jsonrpc_impl_proxy_init(proxy, manager, timeout, send, user_data); return proxy; } diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.c b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.c index e46f19e..071ec4b 100644 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.c +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/request.c @@ -29,12 +29,9 @@ wf_jsonrpc_impl_request_create( void * user_data) { struct wf_jsonrpc_request * request = malloc(sizeof(struct wf_jsonrpc_request)); - if (NULL != request) - { - request->id = id; - request->send = send; - request->user_data = user_data; - } + request->id = id; + request->send = send; + request->user_data = user_data; return request; } diff --git a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c index e510daa..b3e27c8 100644 --- a/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c +++ b/lib/wf/jsonrpc/src/wf/jsonrpc/impl/server.c @@ -24,10 +24,7 @@ struct wf_jsonrpc_server * wf_jsonrpc_impl_server_create(void) { struct wf_jsonrpc_server * server = malloc(sizeof(struct wf_jsonrpc_server)); - if (NULL != server) - { - wf_jsonrpc_impl_server_init(server); - } + wf_jsonrpc_impl_server_init(server); return server; } diff --git a/lib/wf/timer/src/wf/timer/impl/manager.c b/lib/wf/timer/src/wf/timer/impl/manager.c index 2b00f80..f198502 100644 --- a/lib/wf/timer/src/wf/timer/impl/manager.c +++ b/lib/wf/timer/src/wf/timer/impl/manager.c @@ -14,10 +14,7 @@ struct wf_timer_manager * wf_timer_impl_manager_create(void) { struct wf_timer_manager * manager = malloc(sizeof(struct wf_timer_manager)); - if (NULL != manager) - { - manager->timers = NULL; - } + manager->timers = NULL; return manager; } diff --git a/test/webfuse/utils/path.c b/test/webfuse/utils/path.c index 3cb222e..e63aafb 100644 --- a/test/webfuse/utils/path.c +++ b/test/webfuse/utils/path.c @@ -45,24 +45,21 @@ wf_path_create( char const * value) { struct wf_path * path = malloc(sizeof(struct wf_path)); - if (NULL != path) + path->elements = malloc(sizeof(char*) * WF_PATH_DEFAULT_CAPACITY); + path->capacity = WF_PATH_DEFAULT_CAPACITY; + path->count = 0; + + char const * remainder = value; + char const * pos = strchr(remainder, '/'); + while (NULL != pos) { - path->elements = malloc(sizeof(char*) * WF_PATH_DEFAULT_CAPACITY); - path->capacity = WF_PATH_DEFAULT_CAPACITY; - path->count = 0; - - char const * remainder = value; - char const * pos = strchr(remainder, '/'); - while (NULL != pos) - { - wf_path_add(path, remainder, (pos - remainder)); - remainder = pos + 1; - pos = strchr(remainder, '/'); - } - - wf_path_add(path, remainder, strlen(remainder)); + wf_path_add(path, remainder, (pos - remainder)); + remainder = pos + 1; + pos = strchr(remainder, '/'); } + wf_path_add(path, remainder, strlen(remainder)); + return path; } diff --git a/test/webfuse/utils/static_filesystem.c b/test/webfuse/utils/static_filesystem.c index 62b507a..855564b 100644 --- a/test/webfuse/utils/static_filesystem.c +++ b/test/webfuse/utils/static_filesystem.c @@ -332,21 +332,18 @@ wfp_static_filesystem_create( (void) config; struct wfp_static_filesystem * filesystem = malloc(sizeof(struct wfp_static_filesystem)); - if (NULL != filesystem) - { - filesystem->entries = malloc(sizeof(struct wfp_static_filesystem_entry) * WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY); - filesystem->size = 0; - filesystem->capacity = WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY; + filesystem->entries = malloc(sizeof(struct wfp_static_filesystem_entry) * WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY); + filesystem->size = 0; + filesystem->capacity = WFP_STATIC_FILESYSTEM_DEFAULT_CAPACITY; - wfp_static_filesystem_add_dir(filesystem, 0, ""); + wfp_static_filesystem_add_dir(filesystem, 0, ""); - wfp_client_config_set_userdata(config, filesystem); - wfp_client_config_set_onlookup(config, &wfp_static_filesystem_lookup); - wfp_client_config_set_ongetattr(config, &wfp_static_filesystem_getattr); - wfp_client_config_set_onreaddir(config, &wfp_static_filesystem_readdir); - wfp_client_config_set_onopen(config, &wfp_static_filesystem_open); - wfp_client_config_set_onread(config, &wfp_static_filesystem_read); - } + wfp_client_config_set_userdata(config, filesystem); + wfp_client_config_set_onlookup(config, &wfp_static_filesystem_lookup); + wfp_client_config_set_ongetattr(config, &wfp_static_filesystem_getattr); + wfp_client_config_set_onreaddir(config, &wfp_static_filesystem_readdir); + wfp_client_config_set_onopen(config, &wfp_static_filesystem_open); + wfp_client_config_set_onread(config, &wfp_static_filesystem_read); return filesystem; }