From 5b9c5cd92305373ae128c2e9e3c35cd23bffaf9f Mon Sep 17 00:00:00 2001 From: Raghavendra Bhat Date: Fri, 13 Dec 2013 20:27:30 +0530 Subject: cli: check the description length before appending it to the buffer Change-Id: I34cf0f2ee42074bb40edda875e0b290069d856ae Signed-off-by: Raghavendra Bhat --- cli/src/cli-cmd-parser.c | 64 ++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 35 deletions(-) (limited to 'cli') diff --git a/cli/src/cli-cmd-parser.c b/cli/src/cli-cmd-parser.c index 2762f0cf4..3b1a8a92d 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"); -- cgit