summaryrefslogtreecommitdiffstats
path: root/cli/src
diff options
context:
space:
mode:
authorRajesh Joseph <rjoseph@redhat.com>2014-01-15 00:42:01 -0800
committerGerrit Code Review <review@dev.gluster.org>2014-01-15 00:42:01 -0800
commit510d2577b41f4880ea4c02147061e91a38250e8a (patch)
tree0ffb6d70a369958137c3adf31b002c0a6c4458c8 /cli/src
parent727a63c3a5f9cab5af6089826d81df2035e1c0b6 (diff)
parent5b9c5cd92305373ae128c2e9e3c35cd23bffaf9f (diff)
Merge "cli: check the description length before appending it to the buffer" into development
Diffstat (limited to 'cli/src')
-rw-r--r--cli/src/cli-cmd-parser.c64
1 files changed, 29 insertions, 35 deletions
diff --git a/cli/src/cli-cmd-parser.c b/cli/src/cli-cmd-parser.c
index 59e951636..1059ea90e 100644
--- a/cli/src/cli-cmd-parser.c
+++ b/cli/src/cli-cmd-parser.c
@@ -2754,12 +2754,10 @@ out:
int32_t
cli_snap_create_desc_parse (dict_t *dict, const char **words,
- int wordcount, int32_t desc_opt_loc,
- unsigned int no_of_wrds_in_desc)
+ size_t wordcount, int32_t desc_opt_loc)
{
int32_t ret = -1;
char *desc = NULL;
- int32_t i = 0;
int32_t desc_len = 0;
desc = GF_CALLOC (MAX_SNAP_DESCRIPTION_LEN + 1, sizeof(char),
@@ -2771,31 +2769,26 @@ cli_snap_create_desc_parse (dict_t *dict, const char **words,
}
/* Creating the description string */
- for (i = 0; i < no_of_wrds_in_desc; i++) {
- if ((strcmp (words[desc_opt_loc + 1 + i], "-n") == 0) ||
- (strcmp (words[desc_opt_loc + 1 + i], "-d") == 0)) {
- cli_out ("snapshot create: failed: Options(-n/-d) "
- "are not valid descriptions");
- ret = -1;
- goto out;
- }
-
- strcat (desc, words[desc_opt_loc + 1 + i]);
- strcat (desc, " ");
- /* Calculating the size of the description as given by the user */
- desc_len += strlen(words[desc_opt_loc + 1 + i]);
- desc_len++;
+ if ((strcmp (words[desc_opt_loc], "-n") == 0) ||
+ (strcmp (words[desc_opt_loc], "-d") == 0)) {
+ cli_out ("snapshot create: failed: Options(-n/-d) "
+ "are not valid descriptions");
+ ret = -1;
+ goto out;
}
- /* Removing the last space in the string */
- desc[--desc_len] = '\0';
-
- if (desc_len > MAX_SNAP_DESCRIPTION_LEN) {
+ if (strlen (words[desc_opt_loc]) >= MAX_SNAP_DESCRIPTION_LEN) {
cli_out ("snapshot create: description truncated: "
"Description provided is longer than 1024 characters");
- desc[MAX_SNAP_DESCRIPTION_LEN] = '\0';
+ desc_len = MAX_SNAP_DESCRIPTION_LEN;
+ } else {
+ desc_len = strlen (words[desc_opt_loc]);
}
+ strncpy (desc, words[desc_opt_loc], desc_len);
+ desc[desc_len] = '\0';
+ /* Calculating the size of the description as given by the user */
+
ret = dict_set_dynstr (dict, "snap-description", desc);
if (ret) {
gf_log ("", GF_LOG_ERROR, "Unable to save snap description");
@@ -2804,11 +2797,8 @@ cli_snap_create_desc_parse (dict_t *dict, const char **words,
ret = 0;
out:
-
- if (ret) {
- if (desc)
- GF_FREE (desc);
- }
+ if (ret && desc)
+ GF_FREE (desc);
return ret;
}
@@ -2859,9 +2849,9 @@ cli_snap_create_parse (dict_t *dict, const char **words, int wordcount,
/* Description should not be blank */
no_of_wrds_in_desc = (wordcount - 1) - desc_opt_loc;
- if (no_of_wrds_in_desc == 0) {
+ if (no_of_wrds_in_desc != 1) {
cli_out ("snapshot create: failed: "
- "No description provided");
+ "Invalid description");
ret = -1;
goto out;
}
@@ -2882,9 +2872,9 @@ cli_snap_create_parse (dict_t *dict, const char **words, int wordcount,
/* Description should not be blank */
no_of_wrds_in_desc = (wordcount - 1) - desc_opt_loc;
- if (no_of_wrds_in_desc == 0) {
+ if (no_of_wrds_in_desc != 1) {
cli_out ("snapshot create: failed: "
- "No description provided");
+ "Invalid description");
ret = -1;
goto out;
}
@@ -2901,9 +2891,9 @@ cli_snap_create_parse (dict_t *dict, const char **words, int wordcount,
/* Description should not be blank */
no_of_wrds_in_desc = (name_opt_loc) - desc_opt_loc -1;
- if (no_of_wrds_in_desc == 0) {
+ if (no_of_wrds_in_desc != 1) {
cli_out ("snapshot create: failed: "
- "No description provided");
+ "Invalid description");
ret = -1;
goto out;
}
@@ -2977,10 +2967,14 @@ cli_snap_create_parse (dict_t *dict, const char **words, int wordcount,
/* Parsing the description and saving it in the dict */
if (desc_opt_loc > cmdi + 1) {
+ /* desc_opt_loc contains the position of -d option
+ used to indicate description. So send directly
+ the position of the description (i.e desc_opt_loc + 1)
+ for parsing
+ */
ret = cli_snap_create_desc_parse (dict, words,
wordcount,
- desc_opt_loc,
- no_of_wrds_in_desc);
+ desc_opt_loc + 1);
if (ret) {
gf_log ("", GF_LOG_ERROR,
"Unable to parse snap-description");