Skip to contentSkip to conversation

UBIFS: replace simple_strtoul() with kstrtoint()

PATCH Superseded Zhang Zhen 3 participants 6 messages May 20, 2014 – May 21, 2014 1 message not in the archive

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Zhang Zhen

author
#1 lore raw

PATCH v2 1/1 UBIFS: replace simple_strtoul() with kstrtoint()

use the newer and more pleasant kstrtoint() to replace simple_strtoul(), because simple_strtoul() is marked for obsoletion.

---
 drivers/mtd/ubi/build.c | 7 +++----
 fs/ubifs/super.c        | 7 ++++---
 2 files changed, 7 insertions(+), 7 deletions(-)
drivers/mtd/ubi/build.c +3 -4
index 6e30a3c..77a2884 100644
@@ -1190,11 +1190,10 @@ static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
 static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
 {
 	struct mtd_info *mtd;
-	int mtd_num;
-	char *endp;
+	int mtd_num, ret;
 
-	mtd_num = simple_strtoul(mtd_dev, &endp, 0);
-	if (*endp != '\0' || mtd_dev == endp) {
+	ret = kstrtoint(mtd_dev, 0, &mtd_num);
+	if (ret) {
 		/*
 		 * This does not look like an ASCII integer, probably this is
 		 * MTD device name.
fs/ubifs/super.c +4 -3
index a81c7b5..d8c71a6 100644
@@ -1903,7 +1903,7 @@ const struct super_operations ubifs_super_operations = {
 static struct ubi_volume_desc *open_ubi(const char *name, int mode)
 {
 	struct ubi_volume_desc *ubi;
-	int dev, vol;
+	int dev, vol, ret;
 	char *endptr;
 
 	/* First, try to open using the device node path method */
@@ -1922,10 +1922,11 @@ static struct ubi_volume_desc *open_ubi(const char *name, int mode)
 	if (!isdigit(name[3]))
 		return ERR_PTR(-EINVAL);
 
-	dev = simple_strtoul(name + 3, &endptr, 0);
+	endptr = (char *)name + 3;
+	ret = kstrtoint(endptr, 0, &dev);
 
 	/* ubiY method */
-	if (*endptr == '\0')
+	if (!ret)
 		return ubi_open_volume(0, dev, mode);
 
 	/* ubiX_Y method */
Signature · 4 lines
1.8.1.2


.
#2 lore raw
Quoted 43 lines · Zhang Zhen

use the newer and more pleasant kstrtoint() to replace simple_strtoul(), because simple_strtoul() is marked for obsoletion.

---
 drivers/mtd/ubi/build.c | 7 +++----
 fs/ubifs/super.c        | 7 ++++---
 2 files changed, 7 insertions(+), 7 deletions(-)
drivers/mtd/ubi/build.c +3 -4
index 6e30a3c..77a2884 100644
@@ -1190,11 +1190,10 @@ static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
 static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
 {
 	struct mtd_info *mtd;
-	int mtd_num;
-	char *endp;
+	int mtd_num, ret;
 
-	mtd_num = simple_strtoul(mtd_dev, &endp, 0);
-	if (*endp != '\0' || mtd_dev == endp) {
+	ret = kstrtoint(mtd_dev, 0, &mtd_num);
+	if (ret) {
 		/*
 		 * This does not look like an ASCII integer, probably this is
 		 * MTD device name.
fs/ubifs/super.c +1 -1
index a81c7b5..d8c71a6 100644
@@ -1903,7 +1903,7 @@ const struct super_operations ubifs_super_operations = {
 static struct ubi_volume_desc *open_ubi(const char *name, int mode)
 {
 	struct ubi_volume_desc *ubi;
-	int dev, vol;
+	int dev, vol, ret;
 	char *endptr;
 
 	/* First, try to open using the device node path method */

fs/ubifs/super.c static struct ubi_volume_desc *open_ubi(const char *name, int mode) · line 1926

Quoted 6 lines
fs/ubifs/super.c +2 -1
@@ -1922,10 +1922,11 @@ static struct ubi_volume_desc *open_ubi(const char *name, int mode)
 	if (!isdigit(name[3]))
 		return ERR_PTR(-EINVAL);
 
-	dev = simple_strtoul(name + 3, &endptr, 0);
+	endptr = (char *)name + 3;
+	ret = kstrtoint(endptr, 0, &dev);

But endptr is used in the code later, so this is wrong.

Quoted 8 lines
fs/ubifs/super.c +1 -1
 	/* ubiY method */
-	if (*endptr == '\0')
+	if (!ret)
 		return ubi_open_volume(0, dev, mode);
 
 	/* ubiX_Y method */
 
#3 lore raw
Quoted 11 lines · Adrian Hunter
Quoted 10 lines · Zhang Zhen
fs/ubifs/super.c +1 -1
@@ -1903,7 +1903,7 @@ const struct super_operations ubifs_super_operations = {
 static struct ubi_volume_desc *open_ubi(const char *name, int mode)
 {
      struct ubi_volume_desc *ubi;
-     int dev, vol;
+     int dev, vol, ret;
      char *endptr;
 
Quoted 7 lines

fs/ubifs/super.c static struct ubi_volume_desc *open_ubi(const char *name, int mode) · line 1925

Quoted 7 lines
fs/ubifs/super.c +1 -1
      /* First, try to open using the device node path method */
@@ -1922,10 +1922,11 @@ static struct ubi_volume_desc *open_ubi(const char *name, int mode)
      if (!isdigit(name[3]))
              return ERR_PTR(-EINVAL);
 
-     dev = simple_strtoul(name + 3, &endptr, 0);
+     endptr = (char *)name + 3;

Please make endptr const char *, so the cast can be killed. Always think twice before adding a cast!

Ah, there's still another simple_strtoul() left below, that needs to be converted first.

Quoted 3 lines

fs/ubifs/super.c static struct ubi_volume_desc *open_ubi(const char *name, int mode) · line 1926

Quoted 1 line
fs/ubifs/super.c +1 -0
+     ret = kstrtoint(endptr, 0, &dev);

But endptr is used in the code later, so this is wrong.

That was my first thought, too. But upon closer look, I think it's correct.

Quoted 3 lines

fs/ubifs/super.c static struct ubi_volume_desc *open_ubi(const char *name, int mode) · line 1928

Quoted 3 lines
fs/ubifs/super.c +0 -1
      /* ubiY method */
-     if (*endptr == '\0')

endptr would point to the trailing nul on success...

Quoted 1 line

fs/ubifs/super.c static struct ubi_volume_desc *open_ubi(const char *name, int mode) · line 1929

Quoted 1 line
fs/ubifs/super.c +1 -0
+     if (!ret)

... which is now replaced by a test for ret not being an errorcoe.

Quoted 3 lines

fs/ubifs/super.c static struct ubi_volume_desc *open_ubi(const char *name, int mode) · line 1932

Quoted 3 lines
fs/ubifs/super.c +0 -0
              return ubi_open_volume(0, dev, mode);
 
      /* ubiX_Y method */

If ret is an errorcode, the flow continues.

As parsing the number failed, the code checks if the first character (name + 3) is an underscore or colon:

       if (*endptr == '_' && isdigit(endptr[1])) {


                vol = simple_strtoul(endptr + 1, &endptr, 0);
                if (*endptr != '\0')
                        return ERR_PTR(-EINVAL);
                return ubi_open_volume(dev, vol, mode);
        }

        /* ubiX:NAME method */
        if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
                return ubi_open_volume_nm(dev, ++endptr, mode);

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#4 lore raw
Quoted 42 lines · Geert Uytterhoeven
Quoted 11 lines · Adrian Hunter
Quoted 10 lines · Zhang Zhen
fs/ubifs/super.c +1 -1
@@ -1903,7 +1903,7 @@ const struct super_operations ubifs_super_operations = {
 static struct ubi_volume_desc *open_ubi(const char *name, int mode)
 {
      struct ubi_volume_desc *ubi;
-     int dev, vol;
+     int dev, vol, ret;
      char *endptr;
 
Quoted 7 lines

fs/ubifs/super.c static struct ubi_volume_desc *open_ubi(const char *name, int mode) · line 1925

Quoted 7 lines
fs/ubifs/super.c +1 -1
      /* First, try to open using the device node path method */
@@ -1922,10 +1922,11 @@ static struct ubi_volume_desc *open_ubi(const char *name, int mode)
      if (!isdigit(name[3]))
              return ERR_PTR(-EINVAL);
 
-     dev = simple_strtoul(name + 3, &endptr, 0);
+     endptr = (char *)name + 3;

Please make endptr const char *, so the cast can be killed. Always think twice before adding a cast!

Ah, there's still another simple_strtoul() left below, that needs to be converted first.

Quoted 3 lines

fs/ubifs/super.c static struct ubi_volume_desc *open_ubi(const char *name, int mode) · line 1926

Quoted 1 line
fs/ubifs/super.c +1 -0
+     ret = kstrtoint(endptr, 0, &dev);

But endptr is used in the code later, so this is wrong.

That was my first thought, too. But upon closer look, I think it's correct.

Quoted 3 lines

fs/ubifs/super.c static struct ubi_volume_desc *open_ubi(const char *name, int mode) · line 1928

Quoted 3 lines
fs/ubifs/super.c +0 -1
      /* ubiY method */
-     if (*endptr == '\0')

endptr would point to the trailing nul on success...

Quoted 1 line

fs/ubifs/super.c static struct ubi_volume_desc *open_ubi(const char *name, int mode) · line 1929

Quoted 1 line
fs/ubifs/super.c +1 -0
+     if (!ret)

... which is now replaced by a test for ret not being an errorcoe.

Quoted 8 lines
Quoted 3 lines

fs/ubifs/super.c static struct ubi_volume_desc *open_ubi(const char *name, int mode) · line 1932

Quoted 3 lines
fs/ubifs/super.c +0 -0
              return ubi_open_volume(0, dev, mode);
 
      /* ubiX_Y method */

If ret is an errorcode, the flow continues.

As parsing the number failed, the code checks if the first character (name + 3) is an underscore or colon:

No, it does not check if parsing failed, it checks for end-of-input. The "X" and "Y" of "ubiX_Y" are numbers.

Quoted 26 lines
       if (*endptr == '_' && isdigit(endptr[1])) {


                vol = simple_strtoul(endptr + 1, &endptr, 0);
                if (*endptr != '\0')
                        return ERR_PTR(-EINVAL);
                return ubi_open_volume(dev, vol, mode);
        }

        /* ubiX:NAME method */
        if ((*endptr == ':' || *endptr == '!') && endptr[1] != '\0')
                return ubi_open_volume_nm(dev, ++endptr, mode);

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#5 lore raw
Quoted 20 lines · Adrian Hunter
Quoted 20 lines
Quoted 3 lines
Quoted 1 line

+ ret = kstrtoint(endptr, 0, &dev);

But endptr is used in the code later, so this is wrong.

That was my first thought, too. But upon closer look, I think it's correct.

Quoted 3 lines
Quoted 3 lines
      /* ubiY method */

- if (*endptr == '\0')

endptr would point to the trailing nul on success...

Quoted 1 line
Quoted 1 line

+ if (!ret)

... which is now replaced by a test for ret not being an errorcoe.

Quoted 3 lines
Quoted 3 lines
              return ubi_open_volume(0, dev, mode);

      /* ubiX_Y method */
Quoted 7 lines
Quoted 4 lines

If ret is an errorcode, the flow continues.

As parsing the number failed, the code checks if the first character (name + 3) is an underscore or colon:

No, it does not check if parsing failed, it checks for end-of-input. The "X" and "Y" of "ubiX_Y" are numbers.

Sorry, you're right. Thanks for noticing!

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

author
#6 lore raw
Quoted 21 lines · Geert Uytterhoeven
Quoted 20 lines · Adrian Hunter
Quoted 20 lines
Quoted 3 lines
Quoted 1 line

+ ret = kstrtoint(endptr, 0, &dev);

But endptr is used in the code later, so this is wrong.

That was my first thought, too. But upon closer look, I think it's correct.

Quoted 3 lines
Quoted 3 lines
      /* ubiY method */

- if (*endptr == '\0')

endptr would point to the trailing nul on success...

Quoted 1 line
Quoted 1 line

+ if (!ret)

... which is now replaced by a test for ret not being an errorcoe.

Quoted 3 lines
Quoted 3 lines
              return ubi_open_volume(0, dev, mode);

      /* ubiX_Y method */
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines

If ret is an errorcode, the flow continues.

As parsing the number failed, the code checks if the first character (name + 3) is an underscore or colon:

No, it does not check if parsing failed, it checks for end-of-input. The "X" and "Y" of "ubiX_Y" are numbers.

Sorry, i made a mistake. As parsing the number failed, it should just return ERR_PTR(-EINVAL).

Thank you for your review. I learned a lot from you and Geert. I will send another version.

Quoted 15 lines

Sorry, you're right. Thanks for noticing!

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds