Skip to contentSkip to conversation

ocfs2: heartbeat: replace simple_strtoul with kstrtoul

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

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 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.

---
v1->v2: moved ret definition and removed blank lines

 fs/ocfs2/cluster/heartbeat.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
fs/ocfs2/cluster/heartbeat.c +7 -8
index dff18efbc..84ee5e340 100644
@@ -1536,9 +1536,8 @@ 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. */
@@ -1623,12 +1622,13 @@ static ssize_t o2hb_region_blocks_store(struct config_item *item,
 	struct o2hb_region *reg = to_o2hb_region(item);
 	unsigned long tmp;
 	char *p = (char *)page;
+	int ret;
 
 	if (reg->hr_bdev_file)
 		return -EINVAL;
 
-	tmp = simple_strtoul(p, &p, 0);
-	if (!p || (*p && (*p != '\n')))
+	ret = kstrtoul(p, 0, &tmp);
+	if (ret)
 		return -EINVAL;
 
 	if (tmp > O2NM_MAX_NODES || tmp == 0)
@@ -2141,9 +2141,8 @@ 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 16 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.

---
v1->v2: moved ret definition and removed blank lines

 fs/ocfs2/cluster/heartbeat.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
fs/ocfs2/cluster/heartbeat.c +0 -0
index dff18efbc..84ee5e340 100644

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

Quoted 8 lines
fs/ocfs2/cluster/heartbeat.c +2 -3
@@ -1536,9 +1536,8 @@ 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)

The preferred code style is:

...
int ret;

ret = kstrtoul(p, 0, &bytes);
if (ret < 0)

	return ret;
Quoted 7 lines
fs/ocfs2/cluster/heartbeat.c +0 -0
 		return -EINVAL;
 
 	/* Heartbeat and fs min / max block sizes are the same. */
@@ -1623,12 +1622,13 @@ static ssize_t o2hb_region_blocks_store(struct config_item *item,
 	struct o2hb_region *reg = to_o2hb_region(item);
 	unsigned long tmp;
 	char *p = (char *)page;

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

Quoted 10 lines
fs/ocfs2/cluster/heartbeat.c +3 -2
+	int ret;
 
 	if (reg->hr_bdev_file)
 		return -EINVAL;
 
-	tmp = simple_strtoul(p, &p, 0);
-	if (!p || (*p && (*p != '\n')))
+	ret = kstrtoul(p, 0, &tmp);
+	if (ret)
 		return -EINVAL;

Better to return 'ret' directly since it may be -ERANGE.

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

Quoted 12 lines
fs/ocfs2/cluster/heartbeat.c +2 -3
 	if (tmp > O2NM_MAX_NODES || tmp == 0)
@@ -2141,9 +2141,8 @@ 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;

Prefer to:

...
int ret;

ret = kstrtoul(p, 10, &tmp);
if (ret < 0)

	return ret;

Thanks,
Joseph

Quoted 2 lines
fs/ocfs2/cluster/heartbeat.c +0 -0
 	/* this will validate ranges for us. */
#2 lore raw
Quoted 76 lines · Joseph Qi
Quoted 16 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.

---
v1->v2: moved ret definition and removed blank lines

 fs/ocfs2/cluster/heartbeat.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
fs/ocfs2/cluster/heartbeat.c +0 -0
index dff18efbc..84ee5e340 100644

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

Quoted 8 lines
fs/ocfs2/cluster/heartbeat.c +2 -3
@@ -1536,9 +1536,8 @@ 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)

The preferred code style is:

...
int ret;

ret = kstrtoul(p, 0, &bytes);
if (ret < 0)

        return ret;
Quoted 7 lines
fs/ocfs2/cluster/heartbeat.c +0 -0
              return -EINVAL;
 
      /* Heartbeat and fs min / max block sizes are the same. */
@@ -1623,12 +1622,13 @@ static ssize_t o2hb_region_blocks_store(struct config_item *item,
      struct o2hb_region *reg = to_o2hb_region(item);
      unsigned long tmp;
      char *p = (char *)page;

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

Quoted 10 lines
fs/ocfs2/cluster/heartbeat.c +3 -2
+     int ret;
 
      if (reg->hr_bdev_file)
              return -EINVAL;
 
-     tmp = simple_strtoul(p, &p, 0);
-     if (!p || (*p && (*p != '\n')))
+     ret = kstrtoul(p, 0, &tmp);
+     if (ret)
              return -EINVAL;

Better to return 'ret' directly since it may be -ERANGE.

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

Quoted 12 lines
fs/ocfs2/cluster/heartbeat.c +2 -3
      if (tmp > O2NM_MAX_NODES || tmp == 0)
@@ -2141,9 +2141,8 @@ 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;

Prefer to:

...
int ret;

Quoted 9 lines

ret = kstrtoul(p, 10, &tmp);
if (ret < 0)

        return ret;

Thanks,
Joseph

Quoted 2 lines
fs/ocfs2/cluster/heartbeat.c +0 -0
      /* this will validate ranges for us. */

Thanks for the clarification. I applied the changes and sent a v3 of the patch.

- Daniel