Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Error Handling for Memory Allocation Functions #1650

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/iperf_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}


Expand Down Expand Up @@ -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 */
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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));
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions src/iperf_api.c:Zone.Identifier
Original file line number Diff line number Diff line change
@@ -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
22 changes: 15 additions & 7 deletions src/iperf_auth.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions src/iperf_auth.c:Zone.Identifier
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions src/iperf_sctp.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
4 changes: 4 additions & 0 deletions src/iperf_sctp.c:Zone.Identifier
Original file line number Diff line number Diff line change
@@ -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-dGJYzhl4fpYjPllj0H0A6Beo_bWatFO68QkI5ywes2My3oTtuahnqRm982jRVtt4iEa_GYcLiVorTMciH4pyOm1BgQswMgDC6Rjs-w93cWnr7iQimvSTVJZLA4s52nn-4iqXED51peGUnJIwDXTYx1A6Omrbi8kr5rCnzHBXinhp4ua82YmFBrlWTTF96_mBC3O02wboVjrMmYDhVuHtpyeecS1ZNEKglJEvD2nJ_P0Sbd-5poHiGLyoRVgxjDEdaHHtuvKOIkd559r4ISLYVROu7h0YeCZ-h54bvOZIpBZpRD4jHC9AysvAuPlUL39o6xHUgvi6brPH7wiCmY7_STnYmjYxrX9Fb3gh4ggdT3JY52yl_9juqnHLe9sIOcTJIuTD3Ceb8bUVjH9q8PLP0WJIIvyjpQCfP9DaPW3CIfikq_QBxaDtSAae6EiicTtCZIaICSNauOKqsR1roacAA0CyEoMVcGyNUxliTY2-KCRdckzI7VcsEn00QExKC-zJMrrFyZNEO49eHhAhe4ixGRrzhpjntFC6-EQJeYU2wMKcxDgdGsFpYsIx6KdfBwEUcHCtDbCpr-qIqUGvBO6TTS3Dt_FfSPfZpiBsQ1SD_l0_USMIzd1NsSX-poQ5UVPJ7_HV-WA-PH9UhrryO2MKADBc9L9OMtC64V79bp4jpaVtmu4S1A5Fijhy74OPlLG64r0j9S3mMbdHiBqmtLJ5K4OIZ-wOqHU