From: Alexey Dobriyan <adobriyan@gmail.com>
To: akpm@linux-foundation.org
Cc: linux-kernel@vger.kernel.org, mfasheh@suse.com, jlbec@evilplan.org
Subject: [PATCH 09/10] ocfs2: convert to parse_integer()/kstrto*()
Date: Sat, 2 May 2015 04:01:55 +0300 [thread overview]
Message-ID: <20150502010155.GI21655@p183.telecom.by> (raw)
In-Reply-To: <20150502004714.GA21655@p183.telecom.by>
Convert away from deprecated simple_strto*() interfaces.
Autodetection of value range from type allows to remove disgusting checks:
if ((major == LONG_MIN) || (major == LONG_MAX) ||
(major > (u8)-1) || (major < 1))
return -ERANGE;
if ((minor == LONG_MIN) || (minor == LONG_MAX) ||
(minor > (u8)-1) || (minor < 0))
Poof, they're gone!
Signed-off-by: Alexey Dobriyan <adobriyan@gmail.com>
---
fs/ocfs2/cluster/heartbeat.c | 54 +++++++++++++++++------------------------
fs/ocfs2/cluster/nodemanager.c | 50 ++++++++++++++++---------------------
fs/ocfs2/stack_user.c | 50 ++++++++++++++++++-------------------
3 files changed, 70 insertions(+), 84 deletions(-)
--- a/fs/ocfs2/cluster/heartbeat.c
+++ b/fs/ocfs2/cluster/heartbeat.c
@@ -1484,13 +1484,12 @@ static int o2hb_read_block_input(struct o2hb_region *reg,
unsigned long *ret_bytes,
unsigned int *ret_bits)
{
- unsigned long bytes;
- char *p = (char *)page;
-
- bytes = simple_strtoul(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
+ unsigned int bytes;
+ int rv;
+ rv = kstrtouint(page, 0, &bytes);
+ if (rv < 0)
+ return rv;
/* Heartbeat and fs min / max block sizes are the same. */
if (bytes > 4096 || bytes < 512)
return -ERANGE;
@@ -1543,18 +1542,14 @@ static ssize_t o2hb_region_start_block_write(struct o2hb_region *reg,
const char *page,
size_t count)
{
- unsigned long long tmp;
- char *p = (char *)page;
+ int rv;
if (reg->hr_bdev)
return -EINVAL;
- tmp = simple_strtoull(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
-
- reg->hr_start_block = tmp;
-
+ rv = kstrtoull(page, 0, ®->hr_start_block);
+ if (rv < 0)
+ return rv;
return count;
}
@@ -1568,20 +1563,19 @@ static ssize_t o2hb_region_blocks_write(struct o2hb_region *reg,
const char *page,
size_t count)
{
- unsigned long tmp;
- char *p = (char *)page;
+ unsigned int tmp;
+ int rv;
if (reg->hr_bdev)
return -EINVAL;
- tmp = simple_strtoul(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
-
+ rv = kstrtouint(page, 0, &tmp);
+ if (rv < 0)
+ return rv;
if (tmp > O2NM_MAX_NODES || tmp == 0)
return -ERANGE;
- reg->hr_blocks = (unsigned int)tmp;
+ reg->hr_blocks = tmp;
return count;
}
@@ -1717,9 +1711,8 @@ static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
size_t count)
{
struct task_struct *hb_task;
- long fd;
+ int fd;
int sectsize;
- char *p = (char *)page;
struct fd f;
struct inode *inode;
ssize_t ret = -EINVAL;
@@ -1733,10 +1726,9 @@ static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
if (o2nm_this_node() == O2NM_MAX_NODES)
goto out;
- fd = simple_strtol(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
+ ret = kstrtoint(page, 0, &fd);
+ if (ret < 0)
goto out;
-
if (fd < 0 || fd >= INT_MAX)
goto out;
@@ -2210,12 +2202,12 @@ static ssize_t o2hb_heartbeat_group_threshold_store(struct o2hb_heartbeat_group
const char *page,
size_t count)
{
- unsigned long tmp;
- char *p = (char *)page;
+ unsigned int tmp;
+ int rv;
- tmp = simple_strtoul(p, &p, 10);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
+ rv = kstrtouint(page, 10, &tmp);
+ if (rv < 0)
+ return rv;
/* this will validate ranges for us. */
o2hb_dead_threshold_set((unsigned int) tmp);
--- a/fs/ocfs2/cluster/nodemanager.c
+++ b/fs/ocfs2/cluster/nodemanager.c
@@ -195,13 +195,12 @@ static ssize_t o2nm_node_num_write(struct o2nm_node *node, const char *page,
size_t count)
{
struct o2nm_cluster *cluster = to_o2nm_cluster_from_node(node);
- unsigned long tmp;
- char *p = (char *)page;
-
- tmp = simple_strtoul(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
+ unsigned int tmp;
+ int rv;
+ rv = parse_integer(page, 0, &tmp);
+ if (rv < 0)
+ return rv;
if (tmp >= O2NM_MAX_NODES)
return -ERANGE;
@@ -215,16 +214,15 @@ static ssize_t o2nm_node_num_write(struct o2nm_node *node, const char *page,
write_lock(&cluster->cl_nodes_lock);
if (cluster->cl_nodes[tmp])
- p = NULL;
+ rv = -EEXIST;
else {
cluster->cl_nodes[tmp] = node;
node->nd_num = tmp;
set_bit(tmp, cluster->cl_nodes_bitmap);
}
write_unlock(&cluster->cl_nodes_lock);
- if (p == NULL)
- return -EEXIST;
-
+ if (rv < 0)
+ return rv;
return count;
}
static ssize_t o2nm_node_ipv4_port_read(struct o2nm_node *node, char *page)
@@ -235,13 +233,12 @@ static ssize_t o2nm_node_ipv4_port_read(struct o2nm_node *node, char *page)
static ssize_t o2nm_node_ipv4_port_write(struct o2nm_node *node,
const char *page, size_t count)
{
- unsigned long tmp;
- char *p = (char *)page;
-
- tmp = simple_strtoul(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
+ u16 tmp;
+ int rv;
+ rv = kstrtou16(page, 0, &tmp);
+ if (rv < 0)
+ return rv;
if (tmp == 0)
return -EINVAL;
if (tmp >= (u16)-1)
@@ -305,13 +302,11 @@ static ssize_t o2nm_node_local_write(struct o2nm_node *node, const char *page,
{
struct o2nm_cluster *cluster = to_o2nm_cluster_from_node(node);
unsigned long tmp;
- char *p = (char *)page;
ssize_t ret;
- tmp = simple_strtoul(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
-
+ ret = kstrtoul(page, 0, &tmp);
+ if (ret < 0)
+ return ret;
tmp = !!tmp; /* boolean of whether this node wants to be local */
/* setting local turns on networking rx for now so we require having
@@ -484,16 +479,15 @@ struct o2nm_cluster_attribute {
static ssize_t o2nm_cluster_attr_write(const char *page, ssize_t count,
unsigned int *val)
{
- unsigned long tmp;
- char *p = (char *)page;
-
- tmp = simple_strtoul(p, &p, 0);
- if (!p || (*p && (*p != '\n')))
- return -EINVAL;
+ unsigned int tmp;
+ int rv;
+ rv = kstrtouint(page, 0, &tmp);
+ if (rv < 0)
+ return rv;
if (tmp == 0)
return -EINVAL;
- if (tmp >= (u32)-1)
+ if (tmp >= (unsigned int)-1)
return -ERANGE;
*val = tmp;
--- a/fs/ocfs2/stack_user.c
+++ b/fs/ocfs2/stack_user.c
@@ -368,9 +368,9 @@ static int ocfs2_control_get_this_node(void)
static int ocfs2_control_do_setnode_msg(struct file *file,
struct ocfs2_control_message_setn *msg)
{
- long nodenum;
- char *ptr = NULL;
struct ocfs2_control_private *p = file->private_data;
+ int nodenum;
+ int rv;
if (ocfs2_control_get_handshake_state(file) !=
OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
@@ -384,12 +384,12 @@ static int ocfs2_control_do_setnode_msg(struct file *file,
return -EINVAL;
msg->space = msg->newline = '\0';
- nodenum = simple_strtol(msg->nodestr, &ptr, 16);
- if (!ptr || *ptr)
+ rv = parse_integer(msg->nodestr, 16, &nodenum);
+ if (rv < 0)
+ return rv;
+ if (msg->nodestr[rv])
return -EINVAL;
-
- if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
- (nodenum > INT_MAX) || (nodenum < 0))
+ if (nodenum < 0)
return -ERANGE;
p->op_this_node = nodenum;
@@ -399,11 +399,11 @@ static int ocfs2_control_do_setnode_msg(struct file *file,
static int ocfs2_control_do_setversion_msg(struct file *file,
struct ocfs2_control_message_setv *msg)
{
- long major, minor;
- char *ptr = NULL;
+ u8 major, minor;
struct ocfs2_control_private *p = file->private_data;
struct ocfs2_protocol_version *max =
&ocfs2_user_plugin.sp_max_proto;
+ int rv;
if (ocfs2_control_get_handshake_state(file) !=
OCFS2_CONTROL_HANDSHAKE_PROTOCOL)
@@ -418,11 +418,15 @@ static int ocfs2_control_do_setversion_msg(struct file *file,
return -EINVAL;
msg->space1 = msg->space2 = msg->newline = '\0';
- major = simple_strtol(msg->major, &ptr, 16);
- if (!ptr || *ptr)
+ rv = parse_integer(msg->major, 16, &major);
+ if (rv < 0)
+ return rv;
+ if (msg->major[rv])
return -EINVAL;
- minor = simple_strtol(msg->minor, &ptr, 16);
- if (!ptr || *ptr)
+ rv = parse_integer(msg->minor, 16, &minor);
+ if (rv < 0)
+ return rv;
+ if (msg->minor[rv])
return -EINVAL;
/*
@@ -430,11 +434,7 @@ static int ocfs2_control_do_setversion_msg(struct file *file,
* must be between 0 and 255, inclusive. The version passed in
* must be within the maximum version supported by the filesystem.
*/
- if ((major == LONG_MIN) || (major == LONG_MAX) ||
- (major > (u8)-1) || (major < 1))
- return -ERANGE;
- if ((minor == LONG_MIN) || (minor == LONG_MAX) ||
- (minor > (u8)-1) || (minor < 0))
+ if (major < 1)
return -ERANGE;
if ((major != max->pv_major) ||
(minor > max->pv_minor))
@@ -449,8 +449,8 @@ static int ocfs2_control_do_setversion_msg(struct file *file,
static int ocfs2_control_do_down_msg(struct file *file,
struct ocfs2_control_message_down *msg)
{
- long nodenum;
- char *p = NULL;
+ int nodenum;
+ int rv;
if (ocfs2_control_get_handshake_state(file) !=
OCFS2_CONTROL_HANDSHAKE_VALID)
@@ -465,12 +465,12 @@ static int ocfs2_control_do_down_msg(struct file *file,
return -EINVAL;
msg->space1 = msg->space2 = msg->newline = '\0';
- nodenum = simple_strtol(msg->nodestr, &p, 16);
- if (!p || *p)
+ rv = parse_integer(msg->nodestr, 16, &nodenum);
+ if (rv < 0)
+ return rv;
+ if (msg->nodestr[rv])
return -EINVAL;
-
- if ((nodenum == LONG_MIN) || (nodenum == LONG_MAX) ||
- (nodenum > INT_MAX) || (nodenum < 0))
+ if (nodenum < 0)
return -ERANGE;
ocfs2_control_send_down(msg->uuid, nodenum);
next prev parent reply other threads:[~2015-05-02 1:02 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-02 0:47 [PATCH 01/10] Add parse_integer() (replacement for simple_strto*()) Alexey Dobriyan
2015-05-02 0:48 ` [PATCH 02/10] parse_integer: rewrite kstrto*() Alexey Dobriyan
2015-05-02 0:50 ` [PATCH 03/10] parse_integer: convert sscanf() Alexey Dobriyan
2015-05-02 1:10 ` [PATCH CORRECT " Alexey Dobriyan
2015-05-02 0:51 ` [PATCH 04/10] sscanf: fix overflow Alexey Dobriyan
2015-05-05 9:51 ` Rasmus Villemoes
2015-05-05 11:10 ` Alexey Dobriyan
2015-05-06 7:49 ` Rasmus Villemoes
2015-05-02 0:53 ` [PATCH 05/10] parse_integer: convert lib/ Alexey Dobriyan
2015-05-04 14:10 ` Rasmus Villemoes
2015-05-04 14:57 ` Alexey Dobriyan
2015-05-02 0:55 ` [PATCH 06/10] parse_integer: convert mm/ Alexey Dobriyan
2015-05-04 14:33 ` Rasmus Villemoes
2015-05-04 15:09 ` Alexey Dobriyan
2015-05-02 0:56 ` [PATCH 07/10] parse_integer: convert misc fs/ code Alexey Dobriyan
2015-05-02 0:59 ` [PATCH 08/10] fs/cachefiles/: convert to parse_integer() Alexey Dobriyan
2015-05-02 1:01 ` Alexey Dobriyan [this message]
2015-05-02 1:03 ` [PATCH 10/10] ext2, ext3, ext4: convert to parse_integer()/kstrto*() Alexey Dobriyan
2015-05-04 13:24 ` [PATCH 01/10] Add parse_integer() (replacement for simple_strto*()) Rasmus Villemoes
2015-05-04 14:32 ` Alexey Dobriyan
2015-05-04 16:44 ` Rasmus Villemoes
2015-05-04 19:54 ` Alexey Dobriyan
2015-05-04 21:48 ` Rasmus Villemoes
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20150502010155.GI21655@p183.telecom.by \
--to=adobriyan@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=jlbec@evilplan.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mfasheh@suse.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).