From 8844e4c15992369eeca71197d7b3e9f3cba54023 Mon Sep 17 00:00:00 2001 From: KellyLTran Date: Thu, 8 Feb 2024 22:34:57 -0500 Subject: [PATCH] Implement error handling for memory allocation functions --- src/iperf_api.c | 26 +++++++++++++++++++++++--- src/iperf_api.c:Zone.Identifier | 4 ++++ src/iperf_auth.c | 22 +++++++++++++++------- src/iperf_auth.c:Zone.Identifier | 4 ++++ src/iperf_sctp.c | 1 + src/iperf_sctp.c:Zone.Identifier | 4 ++++ 6 files changed, 51 insertions(+), 10 deletions(-) create mode 100644 src/iperf_api.c:Zone.Identifier create mode 100644 src/iperf_auth.c:Zone.Identifier create mode 100644 src/iperf_sctp.c:Zone.Identifier diff --git a/src/iperf_api.c b/src/iperf_api.c index dd0abe814..33ced0eef 100644 --- a/src/iperf_api.c +++ b/src/iperf_api.c @@ -1457,6 +1457,7 @@ iperf_parse_arguments(struct iperf_test *test, int argc, char **argv) xbe = (struct xbind_entry *)malloc(sizeof(struct xbind_entry)); if (!xbe) { i_errno = IESETSCTPBINDX; + printf("WARNING: Memory allocation failed for xbe buffer\n"); return -1; } memset(xbe, 0, sizeof(*xbe)); @@ -2434,6 +2435,10 @@ send_results(struct iperf_test *test) /* Allocate and build it up from the component lines */ char *output = calloc(buflen + 1, 1); + if (output == NULL) { + printf("WARNING: Memory allocation failed for output buffer\n"); + return -1; + } TAILQ_FOREACH(t, &(test->server_output_list), textlineentries) { strncat(output, t->line, buflen); buflen -= strlen(t->line); @@ -2752,7 +2757,7 @@ JSON_read(int fd) free(str); } else { - printf("WARNING: Data length overflow\n"); + printf("WARNING: Memory Allocation failed for data length buffer\n"); } } return json; @@ -2792,8 +2797,13 @@ add_to_interval_list(struct iperf_stream_result * rp, struct iperf_interval_resu struct iperf_interval_results *irp; irp = (struct iperf_interval_results *) malloc(sizeof(struct iperf_interval_results)); - memcpy(irp, new, sizeof(struct iperf_interval_results)); - TAILQ_INSERT_TAIL(&rp->interval_results, irp, irlistentries); + if (irp == NULL) { + printf("WARNING: Memory allocation failed for irp buffer\n"); + } + else { + memcpy(irp, new, sizeof(struct iperf_interval_results)); + TAILQ_INSERT_TAIL(&rp->interval_results, irp, irlistentries); + } } @@ -2845,6 +2855,7 @@ iperf_new_test() test = (struct iperf_test *) malloc(sizeof(struct iperf_test)); if (!test) { i_errno = IENEWTEST; + printf("WARNING: Memory allocation failed for test buffer\n"); return NULL; } /* initialize everything to zero */ @@ -2869,6 +2880,7 @@ iperf_new_test() if (!test->settings) { free(test); i_errno = IENEWTEST; + printf("WARNING: Memory allocation failed for test->settings buffer\n"); return NULL; } memset(test->settings, 0, sizeof(struct iperf_settings)); @@ -2878,6 +2890,7 @@ iperf_new_test() free(test->settings); free(test); i_errno = IENEWTEST; + printf("WARNING: Memory allocation failed for test->bitrate_limit_intervals_traffic_bytes buffer\n"); return NULL; } memset(test->bitrate_limit_intervals_traffic_bytes, 0, sizeof(sizeof(iperf_size_t) * MAX_INTERVAL)); @@ -2897,6 +2910,7 @@ protocol_new(void) proto = malloc(sizeof(struct protocol)); if(!proto) { + printf("WARNING: Memory allocation failed for proto buffer\n"); return NULL; } memset(proto, 0, sizeof(struct protocol)); @@ -4402,6 +4416,7 @@ iperf_new_stream(struct iperf_test *test, int s, int sender) sp = (struct iperf_stream *) malloc(sizeof(struct iperf_stream)); if (!sp) { i_errno = IECREATESTREAM; + printf("WARNING: Memory allocation failed for sp buffer\n"); return NULL; } @@ -4414,6 +4429,7 @@ iperf_new_stream(struct iperf_test *test, int s, int sender) if (!sp->result) { free(sp); i_errno = IECREATESTREAM; + printf("WARNING: Memory allocation failed for sp->result buffer\n"); return NULL; } @@ -5096,6 +5112,10 @@ iperf_printf(struct iperf_test *test, const char* format, ...) if (test->role == 's' && iperf_get_test_get_server_output(test)) { struct iperf_textline *l = (struct iperf_textline *) malloc(sizeof(struct iperf_textline)); + if (l == NULL) { + printf("WARNING: Memory allocation failed for l buffer\n"); + return -1; + } l->line = strdup(linebuffer); TAILQ_INSERT_TAIL(&(test->server_output_list), l, textlineentries); } diff --git a/src/iperf_api.c:Zone.Identifier b/src/iperf_api.c:Zone.Identifier new file mode 100644 index 000000000..501dd749b --- /dev/null +++ b/src/iperf_api.c:Zone.Identifier @@ -0,0 +1,4 @@ +[ZoneTransfer] +ZoneId=3 +ReferrerUrl=https://mail.google.com/ +HostUrl=https://mail-attachment.googleusercontent.com/attachment/u/0/?ui=2&ik=75046721e3&attid=0.2&permmsgid=msg-f:1790368317446107019&th=18d8aa7fba7d2f8b&view=att&disp=safe&realattid=f_lsdqpbqe0&saddbat=ANGjdJ9J9X1bf5rTTbXmYlNAUZk7he6ty6ZlynEf0JkUPrY8EGSjJcAOfAEASGeo9bCDtcE0yjZLUy6DavlEUVmQBCt2Rhx95pHOlb7WZ_Fgh-2GJL6kLbu-QbuL8qe8zUtW5Wk1TuEWg3tUKe9IQADiGGcdle_eCwXvC9yfafDSY3PGBVPE63robWO3QKlcA1DG7ogqX-7yGnjr8ltkynKSBBBPoTXrg7SY9VhnAjxkosC6_u8Ku7G9GC0wNZxCjYqhsO_IOLFTV-Jw3Q-y729iWhhivcPBgl0GE-dRRnxx-UP4eg_qgYLWcb2IiHxzap99E1JdiRuJWWktv3GrrJGHaBRp_SkI5xF0o7i6hB8LZUPtrYl_BzKg-L49xEsmcygBrM7KaeZGoU_wPCrO6ntR8_lpzS97sUr2yoDFicxTr2rRreCls4M1VqwaXdn-WFXeeQiOzxSz_4LZz8zUPHgjnBUFeKvc_B3k1lND1ilo-aKj7LzDstLvI624VNksAg9YP9eMxPQ7JFNABs82VBMYqw5bjEMDixEQasLQO8i8waGSY-cc6f3OJsFgLXIq4lJTZyQv7EI4rVTf4ZfrXTNd-NshqWaQgZe4GrRkk1URfPirnDnzqWufO5liLLVAmH4iCxdqodZfHX9vsE8JGGHuRulf98jvPG_gpiAJv0DL6G8fqrhKdV4IzS6PsHMmOr37Kx7tFcQd8uMtCeNtNp6LKAidQiSyL0fuY3EnYRgALJxqZdq2mdwOQKVOVK_9npTntMVDQ_NROY0i4wQNgnJSj6fluDdTX1D-BwwDvqBYGGPE5bH_Zm4tOuqBqzek1Ro7BkapkyMB7vCHAxrRGZ9YjSao1FmEYN1Pc5CD5BstdGIX7_exHVwTLdibhr1TfdpeI9jRKgh-Zqt6kCethwfDkEjfXHr7YzB3078S4P8DnHmYyY2Xsgloqsst-Iy27it_cb-sRusopZ-nMg2_n2eXHjJCRkIjB8IRQzhnEOGeAJN5y039ImdTEfNkvlQ diff --git a/src/iperf_auth.c b/src/iperf_auth.c index c89bde7c3..fe72ac3ee 100644 --- a/src/iperf_auth.c +++ b/src/iperf_auth.c @@ -144,6 +144,10 @@ int Base64Decode(const char* b64message, unsigned char** buffer, size_t* length) int decodeLen = calcDecodeLength(b64message); *buffer = (unsigned char*)malloc(decodeLen + 1); + if (buffer == NULL) { + printf("WARNING: Memory allocation failed for buffer\n"); + return -1; + } (*buffer)[decodeLen] = '\0'; bio = BIO_new_mem_buf(b64message, -1); @@ -342,7 +346,8 @@ int encode_auth_setting(const char *username, const char *password, EVP_PKEY *pu const int text_len = strlen(auth_text_format) + strlen(username) + strlen(password) + 32; char *text = (char *) calloc(text_len, sizeof(char)); if (text == NULL) { - return -1; + printf("WARNING: Memory allocation failed for authentication text buffer.\n"); + return -1; } snprintf(text, text_len, auth_text_format, username, password, (int64_t)utc_seconds); @@ -377,19 +382,22 @@ int decode_auth_setting(int enable_debug, const char *authtoken, EVP_PKEY *priva char *s_username, *s_password; s_username = (char *) calloc(plaintext_len, sizeof(char)); if (s_username == NULL) { - return -1; + printf("WARNING: Memory allocation failed for username buffer\n"); + return -1; } s_password = (char *) calloc(plaintext_len, sizeof(char)); if (s_password == NULL) { - free(s_username); - return -1; + free(s_username); + printf("WARNING: Memory allocation failed for password buffer\n"); + return -1; } int rc = sscanf((char *) plaintext, auth_text_format, s_username, s_password, &utc_seconds); if (rc != 3) { - free(s_password); - free(s_username); - return -1; + free(s_password); + free(s_username); + printf("WARNING: Parsing failed for authentication token\n"); + return -1; } if (enable_debug) { diff --git a/src/iperf_auth.c:Zone.Identifier b/src/iperf_auth.c:Zone.Identifier new file mode 100644 index 000000000..a0cd63e87 --- /dev/null +++ b/src/iperf_auth.c:Zone.Identifier @@ -0,0 +1,4 @@ +[ZoneTransfer] +ZoneId=3 +ReferrerUrl=https://mail.google.com/ +HostUrl=https://mail-attachment.googleusercontent.com/attachment/u/0/?ui=2&ik=75046721e3&attid=0.1&permmsgid=msg-f:1790368317446107019&th=18d8aa7fba7d2f8b&view=att&disp=safe&realattid=f_lsdqpbqm1&saddbat=ANGjdJ82YjkeqcW0edqKyFP4CIuLvl7Ij1G9IIc96z1YdwQHhd_Zpm9zXxXhcdnzAp9MtD5AkEU8gfPnRuKHRcYQNmNtVj-3ARa4qo1iqkWSG8nBzyYjAJZB5pGumkIOCuI7gd-lwGSA22HV8TqgHx3kWtcBzDd4FZ_Aeyl1G_AQkDS1zP_lq8R3i8MFbDys21li9Nh42gWE_9cQkNi0I0DC5v-bXgryZOqhTYkXNa6-_oh8yKJ7u-AnGN2HLE0tgJjFOknAF6N7iSpAY8U1hFLwXh8THz2bRW9FvfRl7dP22zP5cYXQOE3dYqoB1Lfr6v2XPXygjbwjj_rzOsxI-JxUkPqfk9oakVWljO_XKvR8yszex3818AzZPq81nucLwzpB2tGkn4hRCBNc8khSLXDGFzXkRsOTwybnzrj8eln-jM1BZbujxkWLU-PA8JiAmmNYWuc766XPSORpBDtFjGCzpghsPz2cqXNrtUJGMN0BcoUMEAujCDVkMHUxz4A8oXe6RmB2BH6VJeS85WqG1nlbyEuwGfg2RO_J3hX0fXoVlQyn1PcGdXbk9kXY6Nb4yzNlBncFKFa3w7Ob4l3MyLRAj6Gu9dQqSYRKb5OCrS6oqmTCl41paW9s5WywiW7BhADpEwWmZCyTMwFQOE2ga66oMhA6y8AgzroJZUYSGYWhsJ87fpF_PlwukJQrMBaCZ2CBzZt1aLNte56-oK-1PPvz8g8P2_QAVZ14JTo_1MjzllPYiN8ttVQgZwJ78U-3Is6pZg6saQKJq2hctWYfdeYbY9s6yfeFPat-8NfcUE6vvZpM1HViZSh7jtN3hR8xKgzDM27YV6tMvE4ZjtKiJto5k3Se24U_ucQgosZ9AfXNWFUOSla4KA7f1wlLZbNSD-AYL7UiMaNGR74SqFdDJzZinmOv8TPyenKkqbZKbqT6WXuJlVxpgja313x4c8-TSqLWAQz30HZUIW4yRlkUkCxSJVdQi8ugGJ5SZClprI4Pn9Ctpi58Ra6n3s-D1CU diff --git a/src/iperf_sctp.c b/src/iperf_sctp.c index 104083281..edaf68920 100644 --- a/src/iperf_sctp.c +++ b/src/iperf_sctp.c @@ -689,6 +689,7 @@ iperf_sctp_bindx(struct iperf_test *test, int s, int is_server) xaddrs = (struct sockaddr *)malloc(xaddrlen); if (!xaddrs) { i_errno = IESETSCTPBINDX; + printf("WARNING: Memory allocation failed for xaddrs buffer\n"); retval = -1; goto out; } diff --git a/src/iperf_sctp.c:Zone.Identifier b/src/iperf_sctp.c:Zone.Identifier new file mode 100644 index 000000000..a33ac3c94 --- /dev/null +++ b/src/iperf_sctp.c:Zone.Identifier @@ -0,0 +1,4 @@ +[ZoneTransfer] +ZoneId=3 +ReferrerUrl=https://mail.google.com/ +HostUrl=https://mail-attachment.googleusercontent.com/attachment/u/0/?ui=2&ik=75046721e3&attid=0.3&permmsgid=msg-f:1790368317446107019&th=18d8aa7fba7d2f8b&view=att&disp=safe&realattid=f_lsdqpbqq2&saddbat=ANGjdJ9QjtAoUSPcU5D7LF_5XI57xU8RsSbT_mYTFAhTM9T7kB51Uuu86r5zZz-_7vwSK-3TkZecAUhd0waoT3TksZ7HMAHljwLUZ1ELtVnpTzDJwpwqfptrDYLYYNGh0jxXq0qJ-PkvTzuhxJdMdKQiQI1VPiKb2HgY07vtvnmkWF2huqUSgj8t1MVM7-oFCHDZn1VUc7jVqMmmCDbuuGyUEz_GocXI3ZTnyWsaPTairCH7jvyi2XGcakAPJkGy6W38aK764R4nrm_qi-s5OAOFEXc2bri-dGJYzhl4f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