diff --git a/include/iscsi-private.h b/include/iscsi-private.h index 490876a..5c9b15a 100644 --- a/include/iscsi-private.h +++ b/include/iscsi-private.h @@ -61,8 +61,6 @@ enum iscsi_immediate_data { ISCSI_IMMEDIATE_DATA_YES = 1 }; -#define MAX_STRING_SIZE (255) - struct iscsi_context { char initiator_name[MAX_STRING_SIZE+1]; char target_name[MAX_STRING_SIZE+1]; diff --git a/include/iscsi.h b/include/iscsi.h index bdba224..13cc705 100644 --- a/include/iscsi.h +++ b/include/iscsi.h @@ -32,6 +32,8 @@ extern "C" { struct iscsi_context; struct sockaddr; +#define MAX_STRING_SIZE (255) + /* * Syntax for normal and portal/discovery URLs. */ @@ -69,13 +71,11 @@ EXTERN int iscsi_queue_length(struct iscsi_context *iscsi); */ int iscsi_set_tcp_keepalive(struct iscsi_context *iscsi, int idle, int count, int interval); - - struct iscsi_url { - const char *portal; - const char *target; - const char *user; - const char *passwd; + char portal[MAX_STRING_SIZE+1]; + char target[MAX_STRING_SIZE+1]; + char user[MAX_STRING_SIZE+1]; + char passwd[MAX_STRING_SIZE+1]; int lun; }; diff --git a/lib/init.c b/lib/init.c index c62bbae..278e20d 100644 --- a/lib/init.c +++ b/lib/init.c @@ -302,7 +302,7 @@ struct iscsi_url * iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url) { struct iscsi_url *iscsi_url; - char *str; + char str[MAX_STRING_SIZE+1]; char *portal; char *user = NULL; char *passwd = NULL; @@ -319,11 +319,7 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url) return NULL; } - str = strdup(url + 8); - if (str == NULL) { - iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup url %s", url); - return NULL; - } + strncpy(str,url + 8,MAX_STRING_SIZE); portal = str; user = getenv("LIBISCSI_CHAP_USERNAME"); @@ -353,7 +349,6 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url) "form: %s", url, ISCSI_URL_SYNTAX); - free(str); return NULL; } *target++ = 0; @@ -364,7 +359,6 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url) "iSCSI URL must be of the form: %s", url, ISCSI_URL_SYNTAX); - free(str); return NULL; } @@ -374,7 +368,6 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url) "iSCSI URL must be of the form: %s", url, ISCSI_URL_SYNTAX); - free(str); return NULL; } *lun++ = 0; @@ -385,54 +378,25 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url) "iSCSI URL must be of the form: %s", url, ISCSI_URL_SYNTAX); - free(str); return NULL; } iscsi_url = malloc(sizeof(struct iscsi_url)); if (iscsi_url == NULL) { iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate iscsi_url structure"); - free(str); return NULL; } memset(iscsi_url, 0, sizeof(struct iscsi_url)); - iscsi_url->portal = strdup(portal); - if (iscsi_url->portal == NULL) { - iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup portal string"); - iscsi_destroy_url(iscsi_url); - free(str); - return NULL; - } - - iscsi_url->target = strdup(target); - if (iscsi_url->target == NULL) { - iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup target string"); - iscsi_destroy_url(iscsi_url); - free(str); - return NULL; - } + strncpy(iscsi_url->portal,portal,MAX_STRING_SIZE); + strncpy(iscsi_url->target,target,MAX_STRING_SIZE); if (user != NULL && passwd != NULL) { - iscsi_url->user = strdup(user); - if (iscsi_url->user == NULL) { - iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup username string"); - iscsi_destroy_url(iscsi_url); - free(str); - return NULL; - } - - iscsi_url->passwd = strdup(passwd); - if (iscsi_url->passwd == NULL) { - iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup password string"); - iscsi_destroy_url(iscsi_url); - free(str); - return NULL; - } + strncpy(iscsi_url->user,user,MAX_STRING_SIZE); + strncpy(iscsi_url->user,passwd,MAX_STRING_SIZE); } iscsi_url->lun = l; - free(str); return iscsi_url; } @@ -440,7 +404,7 @@ struct iscsi_url * iscsi_parse_portal_url(struct iscsi_context *iscsi, const char *url) { struct iscsi_url *iscsi_url; - char *str; + char str[MAX_STRING_SIZE+1]; char *portal; char *user = NULL; char *passwd = NULL; @@ -454,11 +418,7 @@ iscsi_parse_portal_url(struct iscsi_context *iscsi, const char *url) return NULL; } - str = strdup(url + 8); - if (str == NULL) { - iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup url %s", url); - return NULL; - } + strncpy(str,url + 8,MAX_STRING_SIZE); portal = str; user = getenv("LIBISCSI_CHAP_USERNAME"); @@ -477,56 +437,26 @@ iscsi_parse_portal_url(struct iscsi_context *iscsi, const char *url) } } - iscsi_url = malloc(sizeof(struct iscsi_url)); if (iscsi_url == NULL) { iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate iscsi_url structure"); - free(str); return NULL; } memset(iscsi_url, 0, sizeof(struct iscsi_url)); - iscsi_url->portal = strdup(portal); - if (iscsi_url->portal == NULL) { - iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup portal string"); - iscsi_destroy_url(iscsi_url); - free(str); - return NULL; - } + strncpy(iscsi_url->portal,portal,MAX_STRING_SIZE); if (user != NULL && passwd != NULL) { - iscsi_url->user = strdup(user); - if (iscsi_url->user == NULL) { - iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup username string"); - iscsi_destroy_url(iscsi_url); - free(str); - return NULL; - } - - iscsi_url->passwd = strdup(passwd); - if (iscsi_url->passwd == NULL) { - iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup password string"); - iscsi_destroy_url(iscsi_url); - free(str); - return NULL; - } + strncpy(iscsi_url->user,user,MAX_STRING_SIZE); + strncpy(iscsi_url->user,passwd,MAX_STRING_SIZE); } - - free(str); + return iscsi_url; } void iscsi_destroy_url(struct iscsi_url *iscsi_url) { - if (iscsi_url == NULL) { - return; - } - - free(discard_const(iscsi_url->portal)); - free(discard_const(iscsi_url->target)); - free(discard_const(iscsi_url->user)); - free(discard_const(iscsi_url->passwd)); free(iscsi_url); } diff --git a/test-tool/iscsi-test.c b/test-tool/iscsi-test.c index c34bc1e..117773a 100644 --- a/test-tool/iscsi-test.c +++ b/test-tool/iscsi-test.c @@ -475,9 +475,9 @@ int main(int argc, const char *argv[]) printf("\n"); } - free(skipname); - free(testname); - free(url); + free(skipname); + free(testname); + free(url); return num_failed ? num_failed : num_skipped ? 77 : 0; }