ISCSI_URL change strings from dynamic to static

This commit is contained in:
Peter Lieven
2012-10-30 11:41:51 +01:00
parent e10a5a97be
commit 774ede1f46
4 changed files with 21 additions and 93 deletions

View File

@@ -61,8 +61,6 @@ enum iscsi_immediate_data {
ISCSI_IMMEDIATE_DATA_YES = 1 ISCSI_IMMEDIATE_DATA_YES = 1
}; };
#define MAX_STRING_SIZE (255)
struct iscsi_context { struct iscsi_context {
char initiator_name[MAX_STRING_SIZE+1]; char initiator_name[MAX_STRING_SIZE+1];
char target_name[MAX_STRING_SIZE+1]; char target_name[MAX_STRING_SIZE+1];

View File

@@ -32,6 +32,8 @@ extern "C" {
struct iscsi_context; struct iscsi_context;
struct sockaddr; struct sockaddr;
#define MAX_STRING_SIZE (255)
/* /*
* Syntax for normal and portal/discovery URLs. * 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); int iscsi_set_tcp_keepalive(struct iscsi_context *iscsi, int idle, int count, int interval);
struct iscsi_url { struct iscsi_url {
const char *portal; char portal[MAX_STRING_SIZE+1];
const char *target; char target[MAX_STRING_SIZE+1];
const char *user; char user[MAX_STRING_SIZE+1];
const char *passwd; char passwd[MAX_STRING_SIZE+1];
int lun; int lun;
}; };

View File

@@ -302,7 +302,7 @@ struct iscsi_url *
iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url) iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
{ {
struct iscsi_url *iscsi_url; struct iscsi_url *iscsi_url;
char *str; char str[MAX_STRING_SIZE+1];
char *portal; char *portal;
char *user = NULL; char *user = NULL;
char *passwd = NULL; char *passwd = NULL;
@@ -319,11 +319,7 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
return NULL; return NULL;
} }
str = strdup(url + 8); strncpy(str,url + 8,MAX_STRING_SIZE);
if (str == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup url %s", url);
return NULL;
}
portal = str; portal = str;
user = getenv("LIBISCSI_CHAP_USERNAME"); user = getenv("LIBISCSI_CHAP_USERNAME");
@@ -353,7 +349,6 @@ iscsi_parse_full_url(struct iscsi_context *iscsi, const char *url)
"form: %s", "form: %s",
url, url,
ISCSI_URL_SYNTAX); ISCSI_URL_SYNTAX);
free(str);
return NULL; return NULL;
} }
*target++ = 0; *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", "iSCSI URL must be of the form: %s",
url, url,
ISCSI_URL_SYNTAX); ISCSI_URL_SYNTAX);
free(str);
return NULL; 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", "iSCSI URL must be of the form: %s",
url, url,
ISCSI_URL_SYNTAX); ISCSI_URL_SYNTAX);
free(str);
return NULL; return NULL;
} }
*lun++ = 0; *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", "iSCSI URL must be of the form: %s",
url, url,
ISCSI_URL_SYNTAX); ISCSI_URL_SYNTAX);
free(str);
return NULL; return NULL;
} }
iscsi_url = malloc(sizeof(struct iscsi_url)); iscsi_url = malloc(sizeof(struct iscsi_url));
if (iscsi_url == NULL) { if (iscsi_url == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate iscsi_url structure"); iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate iscsi_url structure");
free(str);
return NULL; return NULL;
} }
memset(iscsi_url, 0, sizeof(struct iscsi_url)); memset(iscsi_url, 0, sizeof(struct iscsi_url));
iscsi_url->portal = strdup(portal); strncpy(iscsi_url->portal,portal,MAX_STRING_SIZE);
if (iscsi_url->portal == NULL) { strncpy(iscsi_url->target,target,MAX_STRING_SIZE);
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;
}
if (user != NULL && passwd != NULL) { if (user != NULL && passwd != NULL) {
iscsi_url->user = strdup(user); strncpy(iscsi_url->user,user,MAX_STRING_SIZE);
if (iscsi_url->user == NULL) { strncpy(iscsi_url->user,passwd,MAX_STRING_SIZE);
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;
}
} }
iscsi_url->lun = l; iscsi_url->lun = l;
free(str);
return iscsi_url; return iscsi_url;
} }
@@ -440,7 +404,7 @@ struct iscsi_url *
iscsi_parse_portal_url(struct iscsi_context *iscsi, const char *url) iscsi_parse_portal_url(struct iscsi_context *iscsi, const char *url)
{ {
struct iscsi_url *iscsi_url; struct iscsi_url *iscsi_url;
char *str; char str[MAX_STRING_SIZE+1];
char *portal; char *portal;
char *user = NULL; char *user = NULL;
char *passwd = NULL; char *passwd = NULL;
@@ -454,11 +418,7 @@ iscsi_parse_portal_url(struct iscsi_context *iscsi, const char *url)
return NULL; return NULL;
} }
str = strdup(url + 8); strncpy(str,url + 8,MAX_STRING_SIZE);
if (str == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to strdup url %s", url);
return NULL;
}
portal = str; portal = str;
user = getenv("LIBISCSI_CHAP_USERNAME"); 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)); iscsi_url = malloc(sizeof(struct iscsi_url));
if (iscsi_url == NULL) { if (iscsi_url == NULL) {
iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate iscsi_url structure"); iscsi_set_error(iscsi, "Out-of-memory: Failed to allocate iscsi_url structure");
free(str);
return NULL; return NULL;
} }
memset(iscsi_url, 0, sizeof(struct iscsi_url)); memset(iscsi_url, 0, sizeof(struct iscsi_url));
iscsi_url->portal = strdup(portal); strncpy(iscsi_url->portal,portal,MAX_STRING_SIZE);
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;
}
if (user != NULL && passwd != NULL) { if (user != NULL && passwd != NULL) {
iscsi_url->user = strdup(user); strncpy(iscsi_url->user,user,MAX_STRING_SIZE);
if (iscsi_url->user == NULL) { strncpy(iscsi_url->user,passwd,MAX_STRING_SIZE);
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;
}
} }
free(str);
return iscsi_url; return iscsi_url;
} }
void void
iscsi_destroy_url(struct iscsi_url *iscsi_url) 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); free(iscsi_url);
} }

View File

@@ -475,9 +475,9 @@ int main(int argc, const char *argv[])
printf("\n"); printf("\n");
} }
free(skipname); free(skipname);
free(testname); free(testname);
free(url); free(url);
return num_failed ? num_failed : num_skipped ? 77 : 0; return num_failed ? num_failed : num_skipped ? 77 : 0;
} }