]> git.baikalelectronics.ru Git - kernel.git/commitdiff
ptp: clockmatrix: bug fix for idtcm_strverscmp
authorMin Li <min.li.xe@renesas.com>
Wed, 25 Nov 2020 02:58:35 +0000 (21:58 -0500)
committerJakub Kicinski <kuba@kernel.org>
Thu, 26 Nov 2020 01:24:49 +0000 (17:24 -0800)
Feed kstrtou8 with NULL terminated string.

Changes since v1:
-Use sscanf to get rid of adhoc string parse.
Changes since v2:
-Check if sscanf returns 3.

Fixes: 533e185f31fd ("ptp: ptp_clockmatrix: update to support 4.8.7 firmware")
Signed-off-by: Min Li <min.li.xe@renesas.com>
Link: https://lore.kernel.org/r/1606273115-25792-1-git-send-email-min.li.xe@renesas.com
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
drivers/ptp/ptp_clockmatrix.c

index e020faff7da53199875467c50ed9dda27c8dac5c..663255774c0b08adedac00d8517f1de6a1ab51ab 100644 (file)
@@ -103,43 +103,26 @@ static int timespec_to_char_array(struct timespec64 const *ts,
        return 0;
 }
 
-static int idtcm_strverscmp(const char *ver1, const char *ver2)
+static int idtcm_strverscmp(const char *version1, const char *version2)
 {
-       u8 num1;
-       u8 num2;
-       int result = 0;
-
-       /* loop through each level of the version string */
-       while (result == 0) {
-               /* extract leading version numbers */
-               if (kstrtou8(ver1, 10, &num1) < 0)
-                       return -1;
+       u8 ver1[3], ver2[3];
+       int i;
 
-               if (kstrtou8(ver2, 10, &num2) < 0)
-                       return -1;
+       if (sscanf(version1, "%hhu.%hhu.%hhu",
+                  &ver1[0], &ver1[1], &ver1[2]) != 3)
+               return -1;
+       if (sscanf(version2, "%hhu.%hhu.%hhu",
+                  &ver2[0], &ver2[1], &ver2[2]) != 3)
+               return -1;
 
-               /* if numbers differ, then set the result */
-               if (num1 < num2)
-                       result = -1;
-               else if (num1 > num2)
-                       result = 1;
-               else {
-                       /* if numbers are the same, go to next level */
-                       ver1 = strchr(ver1, '.');
-                       ver2 = strchr(ver2, '.');
-                       if (!ver1 && !ver2)
-                               break;
-                       else if (!ver1)
-                               result = -1;
-                       else if (!ver2)
-                               result = 1;
-                       else {
-                               ver1++;
-                               ver2++;
-                       }
-               }
+       for (i = 0; i < 3; i++) {
+               if (ver1[i] > ver2[i])
+                       return 1;
+               if (ver1[i] < ver2[i])
+                       return -1;
        }
-       return result;
+
+       return 0;
 }
 
 static int idtcm_xfer_read(struct idtcm *idtcm,