Skip to contentSkip to conversation

ocfs2: heartbeat: replace simple_strtoul with kstrtoul

PATCH Superseded Daniel Yang 2 participants 3 messages Nov 13, 2024 – Nov 15, 2024 OCFS2

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocfs2: heartbeat: replace simple_strtoul with kstrtoul

The function simple_strtoul is deprecated due to ignoring overflows and also requires clunkier error checking. Replacing with kstrtoul() leads to safer code and cleaner error checking.

---
 fs/ocfs2/cluster/heartbeat.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
fs/ocfs2/cluster/heartbeat.c +9 -6
index dff18efbc..4fc1b23f1 100644
@@ -1537,8 +1537,9 @@ static int o2hb_read_block_input(struct o2hb_region *reg,
 	unsigned long bytes;
 	char *p = (char *)page;
 
-	bytes = simple_strtoul(p, &p, 0);
-	if (!p || (*p && (*p != '\n')))
+	int ret = kstrtoul(p, 0, &bytes);
+
+	if (ret)
 		return -EINVAL;
 
 	/* Heartbeat and fs min / max block sizes are the same. */
@@ -1627,8 +1628,9 @@ static ssize_t o2hb_region_blocks_store(struct config_item *item,
 	if (reg->hr_bdev_file)
 		return -EINVAL;
 
-	tmp = simple_strtoul(p, &p, 0);
-	if (!p || (*p && (*p != '\n')))
+	int ret = kstrtoul(p, 0, &tmp);
+
+	if (ret)
 		return -EINVAL;
 
 	if (tmp > O2NM_MAX_NODES || tmp == 0)
@@ -2142,8 +2144,9 @@ static ssize_t o2hb_heartbeat_group_dead_threshold_store(struct config_item *ite
 	unsigned long tmp;
 	char *p = (char *)page;
 
-	tmp = simple_strtoul(p, &p, 10);
-	if (!p || (*p && (*p != '\n')))
+	int ret = kstrtoul(p, 10, &tmp);
+
+	if (ret)
                 return -EINVAL;
 
 	/* this will validate ranges for us. */
Signature · 1 line
2.39.5
maintainer
#1 lore raw
Quoted 14 lines · Daniel Yang

The function simple_strtoul is deprecated due to ignoring overflows and also requires clunkier error checking. Replacing with kstrtoul() leads to safer code and cleaner error checking.

---
 fs/ocfs2/cluster/heartbeat.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
fs/ocfs2/cluster/heartbeat.c +0 -0
index dff18efbc..4fc1b23f1 100644

fs/ocfs2/cluster/heartbeat.c static int o2hb_read_block_input(struct o2hb_region *reg, · line 1538

Quoted 2 lines
fs/ocfs2/cluster/heartbeat.c +0 -0
@@ -1537,8 +1537,9 @@ static int o2hb_read_block_input(struct o2hb_region *reg,
 	unsigned long bytes;
 	char *p = (char *)page;

Please define 'ret' here to make code style consistent.

fs/ocfs2/cluster/heartbeat.c static int o2hb_read_block_input(struct o2hb_region *reg, · line 1541

Quoted 5 lines
fs/ocfs2/cluster/heartbeat.c +2 -2
-	bytes = simple_strtoul(p, &p, 0);
-	if (!p || (*p && (*p != '\n')))
+	int ret = kstrtoul(p, 0, &bytes);
+

The above blane line can be eleminated.

fs/ocfs2/cluster/heartbeat.c static ssize_t o2hb_region_blocks_store(struct config_item *item, · line 1632

Quoted 12 lines
fs/ocfs2/cluster/heartbeat.c +3 -2
+	if (ret)
 		return -EINVAL;
 
 	/* Heartbeat and fs min / max block sizes are the same. */
@@ -1627,8 +1628,9 @@ static ssize_t o2hb_region_blocks_store(struct config_item *item,
 	if (reg->hr_bdev_file)
 		return -EINVAL;
 
-	tmp = simple_strtoul(p, &p, 0);
-	if (!p || (*p && (*p != '\n')))
+	int ret = kstrtoul(p, 0, &tmp);
+

Ditto.

fs/ocfs2/cluster/heartbeat.c static ssize_t o2hb_heartbeat_group_dead_threshold_store(struct config_item *ite · line 2148

Quoted 12 lines
fs/ocfs2/cluster/heartbeat.c +3 -2
+	if (ret)
 		return -EINVAL;
 
 	if (tmp > O2NM_MAX_NODES || tmp == 0)
@@ -2142,8 +2144,9 @@ static ssize_t o2hb_heartbeat_group_dead_threshold_store(struct config_item *ite
 	unsigned long tmp;
 	char *p = (char *)page;
 
-	tmp = simple_strtoul(p, &p, 10);
-	if (!p || (*p && (*p != '\n')))
+	int ret = kstrtoul(p, 10, &tmp);
+

Ditto.

Thanks,
Joseph

Quoted 4 lines
fs/ocfs2/cluster/heartbeat.c +1 -0
+	if (ret)
                 return -EINVAL;
 
 	/* this will validate ranges for us. */
#2 lore raw
Quoted 60 lines · Joseph Qi
Quoted 14 lines · Daniel Yang

The function simple_strtoul is deprecated due to ignoring overflows and also requires clunkier error checking. Replacing with kstrtoul() leads to safer code and cleaner error checking.

---
 fs/ocfs2/cluster/heartbeat.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
fs/ocfs2/cluster/heartbeat.c +0 -0
index dff18efbc..4fc1b23f1 100644

fs/ocfs2/cluster/heartbeat.c static int o2hb_read_block_input(struct o2hb_region *reg, · line 1538

Quoted 2 lines
fs/ocfs2/cluster/heartbeat.c +0 -0
@@ -1537,8 +1537,9 @@ static int o2hb_read_block_input(struct o2hb_region *reg,
      unsigned long bytes;
      char *p = (char *)page;

Please define 'ret' here to make code style consistent.

fs/ocfs2/cluster/heartbeat.c static int o2hb_read_block_input(struct o2hb_region *reg, · line 1541

Quoted 5 lines
fs/ocfs2/cluster/heartbeat.c +2 -2
-     bytes = simple_strtoul(p, &p, 0);
-     if (!p || (*p && (*p != '\n')))
+     int ret = kstrtoul(p, 0, &bytes);
+

The above blane line can be eleminated.

fs/ocfs2/cluster/heartbeat.c static ssize_t o2hb_region_blocks_store(struct config_item *item, · line 1632

Quoted 12 lines
fs/ocfs2/cluster/heartbeat.c +3 -2
+     if (ret)
              return -EINVAL;
 
      /* Heartbeat and fs min / max block sizes are the same. */
@@ -1627,8 +1628,9 @@ static ssize_t o2hb_region_blocks_store(struct config_item *item,
      if (reg->hr_bdev_file)
              return -EINVAL;
 
-     tmp = simple_strtoul(p, &p, 0);
-     if (!p || (*p && (*p != '\n')))
+     int ret = kstrtoul(p, 0, &tmp);
+

Ditto.

fs/ocfs2/cluster/heartbeat.c static ssize_t o2hb_heartbeat_group_dead_threshold_store(struct config_item *ite · line 2148

Quoted 12 lines
fs/ocfs2/cluster/heartbeat.c +3 -2
+     if (ret)
              return -EINVAL;
 
      if (tmp > O2NM_MAX_NODES || tmp == 0)
@@ -2142,8 +2144,9 @@ static ssize_t o2hb_heartbeat_group_dead_threshold_store(struct config_item *ite
      unsigned long tmp;
      char *p = (char *)page;
 
-     tmp = simple_strtoul(p, &p, 10);
-     if (!p || (*p && (*p != '\n')))
+     int ret = kstrtoul(p, 10, &tmp);
+

Ditto.

Quoted 7 lines

Thanks,
Joseph

Quoted 4 lines
fs/ocfs2/cluster/heartbeat.c +1 -0
+     if (ret)
                 return -EINVAL;
 
      /* this will validate ranges for us. */

Ok sounds good. I moved ret to the beginning of each function and removed the empty line after each ret = kstrtoul() statement. I just sent a v2 of this patch. Let me know if there's any other issues.

- Daniel