public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] ocfs2: heartbeat: replace simple_strtoul with kstrtoul
@ 2024-11-13  8:41 Daniel Yang
  2024-11-15  1:01 ` Joseph Qi
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Yang @ 2024-11-13  8:41 UTC (permalink / raw)
  To: Mark Fasheh, Joel Becker, Joseph Qi, GitAuthor: Daniel Yang,
	open list:ORACLE CLUSTER FILESYSTEM 2 (OCFS2), open list

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.

Signed-off-by: Daniel Yang <danielyangkang@gmail.com>
---
 fs/ocfs2/cluster/heartbeat.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)

diff --git a/fs/ocfs2/cluster/heartbeat.c b/fs/ocfs2/cluster/heartbeat.c
index dff18efbc..4fc1b23f1 100644
--- a/fs/ocfs2/cluster/heartbeat.c
+++ b/fs/ocfs2/cluster/heartbeat.c
@@ -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. */
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH] ocfs2: heartbeat: replace simple_strtoul with kstrtoul
  2024-11-13  8:41 [PATCH] ocfs2: heartbeat: replace simple_strtoul with kstrtoul Daniel Yang
@ 2024-11-15  1:01 ` Joseph Qi
  2024-11-15  7:53   ` Daniel Yang
  0 siblings, 1 reply; 3+ messages in thread
From: Joseph Qi @ 2024-11-15  1:01 UTC (permalink / raw)
  To: Daniel Yang, Mark Fasheh, Joel Becker,
	open list:ORACLE CLUSTER FILESYSTEM 2 (OCFS2), open list



On 11/13/24 4:41 PM, Daniel Yang wrote:
> 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.
> 
> Signed-off-by: Daniel Yang <danielyangkang@gmail.com>
> ---
>  fs/ocfs2/cluster/heartbeat.c | 15 +++++++++------
>  1 file changed, 9 insertions(+), 6 deletions(-)
> 
> diff --git a/fs/ocfs2/cluster/heartbeat.c b/fs/ocfs2/cluster/heartbeat.c
> index dff18efbc..4fc1b23f1 100644
> --- a/fs/ocfs2/cluster/heartbeat.c
> +++ b/fs/ocfs2/cluster/heartbeat.c
> @@ -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.
>  
> -	bytes = simple_strtoul(p, &p, 0);
> -	if (!p || (*p && (*p != '\n')))
> +	int ret = kstrtoul(p, 0, &bytes);
> +

The above blane line can be eleminated.

> +	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.

> +	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
> +	if (ret)
>                  return -EINVAL;
>  
>  	/* this will validate ranges for us. */


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] ocfs2: heartbeat: replace simple_strtoul with kstrtoul
  2024-11-15  1:01 ` Joseph Qi
@ 2024-11-15  7:53   ` Daniel Yang
  0 siblings, 0 replies; 3+ messages in thread
From: Daniel Yang @ 2024-11-15  7:53 UTC (permalink / raw)
  To: Joseph Qi
  Cc: Mark Fasheh, Joel Becker,
	open list:ORACLE CLUSTER FILESYSTEM 2 (OCFS2), open list

On Thu, Nov 14, 2024 at 5:01 PM Joseph Qi <joseph.qi@linux.alibaba.com> wrote:
>
>
>
> On 11/13/24 4:41 PM, Daniel Yang wrote:
> > 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.
> >
> > Signed-off-by: Daniel Yang <danielyangkang@gmail.com>
> > ---
> >  fs/ocfs2/cluster/heartbeat.c | 15 +++++++++------
> >  1 file changed, 9 insertions(+), 6 deletions(-)
> >
> > diff --git a/fs/ocfs2/cluster/heartbeat.c b/fs/ocfs2/cluster/heartbeat.c
> > index dff18efbc..4fc1b23f1 100644
> > --- a/fs/ocfs2/cluster/heartbeat.c
> > +++ b/fs/ocfs2/cluster/heartbeat.c
> > @@ -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.
> >
> > -     bytes = simple_strtoul(p, &p, 0);
> > -     if (!p || (*p && (*p != '\n')))
> > +     int ret = kstrtoul(p, 0, &bytes);
> > +
>
> The above blane line can be eleminated.
>
> > +     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.
>
> > +     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
> > +     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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-11-15  7:54 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-11-13  8:41 [PATCH] ocfs2: heartbeat: replace simple_strtoul with kstrtoul Daniel Yang
2024-11-15  1:01 ` Joseph Qi
2024-11-15  7:53   ` Daniel Yang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox