Whamcloud - gitweb
LU-9325 obdclass: handle strings correctly in lmd_find_delimiter 58/26558/7
authorJames Simmons <uja.ornl@yahoo.com>
Tue, 1 May 2018 15:42:28 +0000 (11:42 -0400)
committerOleg Drokin <oleg.drokin@intel.com>
Thu, 17 May 2018 02:30:45 +0000 (02:30 +0000)
The feedback from the pushing of the lmd_find_delimiter() work
upstream was not to directly parse the string data passed in.

Change the return value to a bool and return true when the
character is found. Currently even though this function is
named lmd_find_delimiter() it return 1 when nothing is found
which is counter intuitive. Use strcspn() to determine the
position where the first delimiter is found. Add a test to
make sure the mount string is valid.

Change-Id: I7ef53f29a6d3284acd01225c115712d3674e8435
Signed-off-by: James Simmons <uja.ornl@yahoo.com>
Reviewed-on: https://review.whamcloud.com/26558
Tested-by: Jenkins
Tested-by: Maloo <hpdd-maloo@intel.com>
Reviewed-by: John L. Hammond <john.hammond@intel.com>
Reviewed-by: Jian Yu <jian.yu@intel.com>
Reviewed-by: Oleg Drokin <oleg.drokin@intel.com>
lustre/obdclass/obd_mount.c

index c3637e0..6bebaf3 100644 (file)
@@ -1156,37 +1156,52 @@ static int lmd_parse_mgs(struct lustre_mount_data *lmd, char **ptr)
  * make \a *endh point to the string starting with the delimiter. The commas
  * in expression list [...] will be skipped.
  *
- * \param[in] buf      a delimiter-separated string
- * \param[in] endh     a pointer to a pointer that will point to the string
- *                     starting with the delimiter
+ * @buf                a delimiter-separated string
+ * @endh       a pointer to a pointer that will point to the string
+ *             starting with the delimiter
  *
- * \retval 0           if delimiter is found
- * \retval 1           if delimiter is not found
+ * RETURNS     true if delimiter is found, false if delimiter is not found
  */
-static int lmd_find_delimiter(char *buf, char **endh)
+static bool lmd_find_delimiter(char *buf, char **endh)
 {
        char *c = buf;
-       int   skip = 0;
-
-       if (buf == NULL)
-               return 1;
+       size_t pos;
+       bool found;
+
+       if (!buf)
+               return false;
+try_again:
+       if (*c == ',' || *c == ':')
+               return true;
+
+       pos = strcspn(c, "[:,]");
+       if (!pos)
+               return false;
+
+       /* Not a valid mount string */
+       if (*c == ']') {
+               CWARN("invalid mount string format\n");
+               return false;
+       }
 
-       while (*c != '\0') {
-               if (*c == '[')
-                       skip++;
-               else if (*c == ']')
-                       skip--;
+       c += pos;
+       if (*c == '[') {
+               c = strchr(c, ']');
 
-               if ((*c == ',' || *c == ':') && skip == 0) {
-                       if (endh != NULL)
-                               *endh = c;
-                       return 0;
+               /* invalid mount string */
+               if (!c) {
+                       CWARN("invalid mount string format\n");
+                       return false;
                }
-
                c++;
+               goto try_again;
        }
 
-       return 1;
+       found = *c != '\0';
+       if (found && endh)
+               *endh = c;
+
+       return found;
 }
 
 /**
@@ -1215,7 +1230,7 @@ static int lmd_parse_nidlist(char *buf, char **endh)
        if (*buf == ' ' || *buf == '/' || *buf == '\0')
                return 1;
 
-       if (lmd_find_delimiter(buf, &endp) != 0)
+       if (!lmd_find_delimiter(buf, &endp))
                endp = buf + strlen(buf);
 
        tmp = *endp;
@@ -1360,9 +1375,8 @@ static int lmd_parse(char *options, struct lustre_mount_data *lmd)
                } else if (strncmp(s1, "param=", 6) == 0) {
                        size_t length, params_length;
                        char  *tail = s1;
-                       if (lmd_find_delimiter(s1 + 6, &tail) != 0)
-                               length = strlen(s1);
-                       else {
+
+                       if (lmd_find_delimiter(s1 + 6, &tail)) {
                                char *param_str = tail + 1;
                                int   supplementary = 1;
                                while (lmd_parse_nidlist(param_str,
@@ -1370,6 +1384,8 @@ static int lmd_parse(char *options, struct lustre_mount_data *lmd)
                                        supplementary = 0;
                                }
                                length = param_str - s1 - supplementary;
+                       } else {
+                               length = strlen(s1);
                        }
                        length -= 6;
                        params_length = strlen(lmd->lmd_params);