From 64c4e795b5a155cf6991412be93fafb29ab0f55e Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Sat, 11 Oct 2025 13:56:34 +0200 Subject: [PATCH] fix allocation issue net tests --- tests/kds_s2000w_net_tests.c | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/tests/kds_s2000w_net_tests.c b/tests/kds_s2000w_net_tests.c index 3057387..455d515 100644 --- a/tests/kds_s2000w_net_tests.c +++ b/tests/kds_s2000w_net_tests.c @@ -18,6 +18,8 @@ int setup(void** state) { response** response_list = malloc(sizeof(response*) * 4); + if (response_list == NULL) + return 1; response_list[0] = kds_s2000w_client_response_init(); response_list[0]->code = 200; response_list[1] = kds_s2000w_client_response_init(); @@ -38,6 +40,15 @@ int setup(void** state) response_list[2]->size = 17; response_list[2]->data = malloc(sizeof(char) * 17); char* image_data = (char*) response_list[2]->data; + if (image_data == NULL) { + for (int i = 0; i < 4; i++) { + free(response_list[i]); + response_list[i] = NULL; + } + free(response_list); + response_list = NULL; + return 1; + } image_data[0] = 0x50; image_data[1] = 0x36; image_data[2] = 0x0a; @@ -75,6 +86,8 @@ int teardown(void** state) void kds_s2000w_net_init_test() { SANE_Int* version = malloc(sizeof(SANE_Int)); + if (version == NULL) + return; SANE_Status status = sane_kds_s2000w_net_init(version, NULL); @@ -88,6 +101,8 @@ void kds_s2000w_net_init_test() void kds_s2000w_net_get_devices_test() { SANE_Device*** device_list = malloc(sizeof(SANE_Device**)); + if (device_list == NULL) + return; sane_kds_s2000w_net_get_devices(device_list, 0); @@ -104,6 +119,8 @@ void kds_s2000w_net_get_devices_test() void kds_s2000w_net_get_devices_only_remote_test() { SANE_Device*** device_list = malloc(sizeof(SANE_Device**)); + if (device_list == NULL) + return; SANE_Status result = sane_kds_s2000w_net_get_devices(device_list, 1); @@ -116,11 +133,18 @@ void kds_s2000w_net_get_devices_only_remote_test() void sane_kds_s2000w_net_get_parameter_color_test() { handler* h = kds_s2000w_handler_init(); + if (h == NULL) + return; const char* config = "{\"Configuration\":{\"ColorMode\":\"Color\"}}"; h->current_scanner_config = json_tokener_parse(config); h->image->size = 17; h->image->data = malloc(sizeof(char) * 17); char* image_data = (char*) h->image->data; + if (image_data == NULL) { + kds_s2000w_handler_free(h); + h = NULL; + return; + } image_data[0] = 0x50; image_data[1] = 0x36; image_data[2] = 0x0a; @@ -137,6 +161,13 @@ void sane_kds_s2000w_net_get_parameter_color_test() } SANE_Parameters* params = malloc(sizeof(SANE_Parameters)); + if (params == NULL) { + free(image_data); + image_data = NULL; + kds_s2000w_handler_free(h); + h = NULL; + return; + } SANE_Status status = sane_kds_s2000w_net_get_parameters(h, params); @@ -158,11 +189,18 @@ void sane_kds_s2000w_net_get_parameter_color_test() void sane_kds_s2000w_net_get_parameter_bw_test() { handler* h = kds_s2000w_handler_init(); + if (h == NULL) + return; const char* config = "{\"Configuration\":{\"ColorMode\":\"Color\"}}"; h->current_scanner_config = json_tokener_parse(config); h->image->size = 17; h->image->data = malloc(sizeof(char) * 17); char* image_data = (char*) h->image->data; + if (image_data == NULL) { + kds_s2000w_handler_free(h); + h = NULL; + return; + } image_data[0] = 0x50; image_data[1] = 0x34; image_data[2] = 0x0a; @@ -175,6 +213,13 @@ void sane_kds_s2000w_net_get_parameter_bw_test() } SANE_Parameters* params = malloc(sizeof(SANE_Parameters)); + if (params == NULL) { + free(image_data); + image_data = NULL; + kds_s2000w_handler_free(h); + h = NULL; + return; + } SANE_Status status = sane_kds_s2000w_net_get_parameters(h, params); @@ -196,6 +241,8 @@ void sane_kds_s2000w_net_get_parameter_bw_test() void sane_kds_s2000w_net_open_test() { response* resp = kds_s2000w_client_response_init(); + if (resp == NULL) + return; const char* responsedata = "{\"SessionId\":\"1251877821\"}"; resp->size = 27; resp->data = malloc(resp->size); @@ -205,6 +252,11 @@ void sane_kds_s2000w_net_open_test() will_return(__wrap_kds_s2000w_client_open_session, 0); expect_function_call(__wrap_kds_s2000w_client_open_session); void** hlist = malloc(sizeof(void*)); + if (hlist == NULL) { + kds_s2000w_client_response_free(resp); + resp = NULL; + return; + } SANE_Status status = sane_kds_s2000w_net_open("kds_s2000w_net", hlist); handler* h = (handler*) *hlist; -- 2.47.3