wifi: libertas: prefer kstrtoX() for simple integer conversions
authorDmitry Antipov <dmantipov@yandex.ru>
Tue, 25 Jul 2023 06:04:48 +0000 (09:04 +0300)
committerKalle Valo <kvalo@kernel.org>
Tue, 1 Aug 2023 14:49:02 +0000 (17:49 +0300)
Prefer 'kstrtoX()' family of functions over 'sscanf()' to convert
strings to integers and always check results of the conversions.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
Signed-off-by: Kalle Valo <kvalo@kernel.org>
Link: https://lore.kernel.org/r/20230725060531.72968-6-dmantipov@yandex.ru
drivers/net/wireless/marvell/libertas/mesh.c

index 90ffe8d1e0e811c83c8d20b9496d47fe661209de..2dd635935448bbf06dd4c241429e2571dc17c6bc 100644 (file)
@@ -188,8 +188,11 @@ static ssize_t anycast_mask_store(struct device *dev,
        uint32_t datum;
        int ret;
 
+       ret = kstrtouint(buf, 16, &datum);
+       if (ret)
+               return ret;
+
        memset(&mesh_access, 0, sizeof(mesh_access));
-       sscanf(buf, "%x", &datum);
        mesh_access.data[0] = cpu_to_le32(datum);
 
        ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_ANYCAST, &mesh_access);
@@ -241,15 +244,14 @@ static ssize_t prb_rsp_limit_store(struct device *dev,
        int ret;
        unsigned long retry_limit;
 
-       memset(&mesh_access, 0, sizeof(mesh_access));
-       mesh_access.data[0] = cpu_to_le32(CMD_ACT_SET);
-
        ret = kstrtoul(buf, 10, &retry_limit);
        if (ret)
                return ret;
        if (retry_limit > 15)
                return -ENOTSUPP;
 
+       memset(&mesh_access, 0, sizeof(mesh_access));
+       mesh_access.data[0] = cpu_to_le32(CMD_ACT_SET);
        mesh_access.data[1] = cpu_to_le32(retry_limit);
 
        ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
@@ -285,9 +287,12 @@ static ssize_t lbs_mesh_store(struct device *dev,
                              const char *buf, size_t count)
 {
        struct lbs_private *priv = to_net_dev(dev)->ml_priv;
-       int enable;
+       int ret, enable;
+
+       ret = kstrtoint(buf, 16, &enable);
+       if (ret)
+               return ret;
 
-       sscanf(buf, "%x", &enable);
        enable = !!enable;
        if (enable == !!priv->mesh_dev)
                return count;
@@ -387,11 +392,13 @@ static ssize_t bootflag_store(struct device *dev, struct device_attribute *attr,
        uint32_t datum;
        int ret;
 
-       memset(&cmd, 0, sizeof(cmd));
-       ret = sscanf(buf, "%d", &datum);
-       if ((ret != 1) || (datum > 1))
+       ret = kstrtouint(buf, 10, &datum);
+       if (ret)
+               return ret;
+       if (datum > 1)
                return -EINVAL;
 
+       memset(&cmd, 0, sizeof(cmd));
        *((__le32 *)&cmd.data[0]) = cpu_to_le32(!!datum);
        cmd.length = cpu_to_le16(sizeof(uint32_t));
        ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
@@ -438,11 +445,14 @@ static ssize_t boottime_store(struct device *dev,
        uint32_t datum;
        int ret;
 
-       memset(&cmd, 0, sizeof(cmd));
-       ret = sscanf(buf, "%d", &datum);
-       if ((ret != 1) || (datum > 255))
+       ret = kstrtouint(buf, 10, &datum);
+       if (ret)
+               return ret;
+       if (datum > 255)
                return -EINVAL;
 
+       memset(&cmd, 0, sizeof(cmd));
+
        /* A too small boot time will result in the device booting into
         * standalone (no-host) mode before the host can take control of it,
         * so the change will be hard to revert.  This may be a desired
@@ -497,11 +507,13 @@ static ssize_t channel_store(struct device *dev, struct device_attribute *attr,
        uint32_t datum;
        int ret;
 
-       memset(&cmd, 0, sizeof(cmd));
-       ret = sscanf(buf, "%d", &datum);
-       if (ret != 1 || datum < 1 || datum > 11)
+       ret = kstrtouint(buf, 10, &datum);
+       if (ret)
+               return ret;
+       if (datum < 1 || datum > 11)
                return -EINVAL;
 
+       memset(&cmd, 0, sizeof(cmd));
        *((__le16 *)&cmd.data[0]) = cpu_to_le16(datum);
        cmd.length = cpu_to_le16(sizeof(uint16_t));
        ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
@@ -626,11 +638,14 @@ static ssize_t protocol_id_store(struct device *dev,
        uint32_t datum;
        int ret;
 
-       memset(&cmd, 0, sizeof(cmd));
-       ret = sscanf(buf, "%d", &datum);
-       if ((ret != 1) || (datum > 255))
+       ret = kstrtouint(buf, 10, &datum);
+       if (ret)
+               return ret;
+       if (datum > 255)
                return -EINVAL;
 
+       memset(&cmd, 0, sizeof(cmd));
+
        /* fetch all other Information Element parameters */
        ret = mesh_get_default_parameters(dev, &defs);