public inbox for linux-mtd@lists.infradead.org
 help / color / mirror / Atom feed
* [PATCH] ubi: replace simple_strtoul() with kstrtoul()
       [not found] <1400488570-20288-1-git-send-email-zhenzhang.zhang@huawei.com>
@ 2014-05-19  8:38 ` Zhang Zhen
  2014-05-19  9:14   ` Geert Uytterhoeven
  0 siblings, 1 reply; 5+ messages in thread
From: Zhang Zhen @ 2014-05-19  8:38 UTC (permalink / raw)
  To: adrian.hunter, dedekind1; +Cc: linux-mtd, Hu Jianyang

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

Signed-off-by: Zhang Zhen <zhenzhang.zhang@huawei.com>
Signed-off-by: hujianyang <hujianyang@huawei.com>
---
 drivers/mtd/ubi/build.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
index 6e30a3c..80c539c 100644
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
@@ -1190,10 +1190,13 @@ 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;
+	int mtd_num, ret;
 	char *endp;

-	mtd_num = simple_strtoul(mtd_dev, &endp, 0);
+	endp = (char *)mtd_dev;
+	ret = kstrtoul(endp, 0, (unsigned long *)&mtd_num);
+	if (ret)
+		return ERR_PTR(-EINVAL);
 	if (*endp != '\0' || mtd_dev == endp) {
 		/*
 		 * This does not look like an ASCII integer, probably this is
@@ -1362,8 +1365,12 @@ static int __init bytes_str_to_int(const char *str)
 {
 	char *endp;
 	unsigned long result;
+	int ret;

-	result = simple_strtoul(str, &endp, 0);
+	endp = (char *)str;
+	ret = kstrtoul(endp, 0, &result);
+	if (ret)
+		return -EINVAL;
 	if (str == endp || result >= INT_MAX) {
 		ubi_err("incorrect bytes count: \"%s\"\n", str);
 		return -EINVAL;
-- 
1.8.1.2


.

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

* Re: [PATCH] ubi: replace simple_strtoul() with kstrtoul()
  2014-05-19  8:38 ` [PATCH] ubi: replace simple_strtoul() with kstrtoul() Zhang Zhen
@ 2014-05-19  9:14   ` Geert Uytterhoeven
  2014-05-20  1:18     ` Zhang Zhen
  0 siblings, 1 reply; 5+ messages in thread
From: Geert Uytterhoeven @ 2014-05-19  9:14 UTC (permalink / raw)
  To: Zhang Zhen; +Cc: Hu Jianyang, MTD Maling List, adrian.hunter, Artem Bityutskiy

Please don't add mindless casts!

On Mon, May 19, 2014 at 10:38 AM, Zhang Zhen <zhenzhang.zhang@huawei.com> wrote:
> --- a/drivers/mtd/ubi/build.c
> +++ b/drivers/mtd/ubi/build.c
> @@ -1190,10 +1190,13 @@ 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;
> +       int mtd_num, ret;
>         char *endp;
>
> -       mtd_num = simple_strtoul(mtd_dev, &endp, 0);
> +       endp = (char *)mtd_dev;
> +       ret = kstrtoul(endp, 0, (unsigned long *)&mtd_num);

On 64-bit, long is 64-bit, hence this will write beyond mtd_num and will corrupt
the stack.

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

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

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

* Re: [PATCH] ubi: replace simple_strtoul() with kstrtoul()
  2014-05-19  9:14   ` Geert Uytterhoeven
@ 2014-05-20  1:18     ` Zhang Zhen
  2014-05-20  6:57       ` Geert Uytterhoeven
  0 siblings, 1 reply; 5+ messages in thread
From: Zhang Zhen @ 2014-05-20  1:18 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: Hu Jianyang, MTD Maling List, adrian.hunter, Artem Bityutskiy

On 2014/5/19 17:14, Geert Uytterhoeven wrote:
> Please don't add mindless casts!
> 
> On Mon, May 19, 2014 at 10:38 AM, Zhang Zhen <zhenzhang.zhang@huawei.com> wrote:
>> --- a/drivers/mtd/ubi/build.c
>> +++ b/drivers/mtd/ubi/build.c
>> @@ -1190,10 +1190,13 @@ 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;
>> +       int mtd_num, ret;
>>         char *endp;
>>
>> -       mtd_num = simple_strtoul(mtd_dev, &endp, 0);
>> +       endp = (char *)mtd_dev;
>> +       ret = kstrtoul(endp, 0, (unsigned long *)&mtd_num);
> 
> On 64-bit, long is 64-bit, hence this will write beyond mtd_num and will corrupt
> the stack.

Yeah, you are right. This really may write beyond dev.

The kstrtoul(const char *s, unsigned int base, unsigned long *res) only accept unsigned long
pointer as the third parameter.
And the original function simple_strtoul() returns unsigned long type value.
It is also cast. So this may not corrupt the stack.

Or do you have any better suggestion about this?
Thanks.
> 
> Gr{oetje,eeting}s,
> 
>                         Geert
> 
> --
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
> 
> 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
> 
> 

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

* Re: [PATCH] ubi: replace simple_strtoul() with kstrtoul()
  2014-05-20  1:18     ` Zhang Zhen
@ 2014-05-20  6:57       ` Geert Uytterhoeven
  2014-05-20  8:14         ` Zhang Zhen
  0 siblings, 1 reply; 5+ messages in thread
From: Geert Uytterhoeven @ 2014-05-20  6:57 UTC (permalink / raw)
  To: Zhang Zhen; +Cc: Hu Jianyang, MTD Maling List, adrian.hunter, Artem Bityutskiy

On Tue, May 20, 2014 at 3:18 AM, Zhang Zhen <zhenzhang.zhang@huawei.com> wrote:
> On 2014/5/19 17:14, Geert Uytterhoeven wrote:
>> Please don't add mindless casts!
>>
>> On Mon, May 19, 2014 at 10:38 AM, Zhang Zhen <zhenzhang.zhang@huawei.com> wrote:
>>> --- a/drivers/mtd/ubi/build.c
>>> +++ b/drivers/mtd/ubi/build.c
>>> @@ -1190,10 +1190,13 @@ 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;
>>> +       int mtd_num, ret;
>>>         char *endp;
>>>
>>> -       mtd_num = simple_strtoul(mtd_dev, &endp, 0);
>>> +       endp = (char *)mtd_dev;
>>> +       ret = kstrtoul(endp, 0, (unsigned long *)&mtd_num);
>>
>> On 64-bit, long is 64-bit, hence this will write beyond mtd_num and will corrupt
>> the stack.
>
> Yeah, you are right. This really may write beyond dev.
>
> The kstrtoul(const char *s, unsigned int base, unsigned long *res) only accept unsigned long
> pointer as the third parameter.
> And the original function simple_strtoul() returns unsigned long type value.
> It is also cast. So this may not corrupt the stack.
>
> Or do you have any better suggestion about this?

kstrtoint().

Gr{oetje,eeting}s,

                        Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org

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

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

* Re: [PATCH] ubi: replace simple_strtoul() with kstrtoul()
  2014-05-20  6:57       ` Geert Uytterhoeven
@ 2014-05-20  8:14         ` Zhang Zhen
  0 siblings, 0 replies; 5+ messages in thread
From: Zhang Zhen @ 2014-05-20  8:14 UTC (permalink / raw)
  To: Geert Uytterhoeven
  Cc: Hu Jianyang, MTD Maling List, adrian.hunter, Artem Bityutskiy

On 2014/5/20 14:57, Geert Uytterhoeven wrote:
> On Tue, May 20, 2014 at 3:18 AM, Zhang Zhen <zhenzhang.zhang@huawei.com> wrote:
>> On 2014/5/19 17:14, Geert Uytterhoeven wrote:
>>> Please don't add mindless casts!
>>>
>>> On Mon, May 19, 2014 at 10:38 AM, Zhang Zhen <zhenzhang.zhang@huawei.com> wrote:
>>>> --- a/drivers/mtd/ubi/build.c
>>>> +++ b/drivers/mtd/ubi/build.c
>>>> @@ -1190,10 +1190,13 @@ 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;
>>>> +       int mtd_num, ret;
>>>>         char *endp;
>>>>
>>>> -       mtd_num = simple_strtoul(mtd_dev, &endp, 0);
>>>> +       endp = (char *)mtd_dev;
>>>> +       ret = kstrtoul(endp, 0, (unsigned long *)&mtd_num);
>>>
>>> On 64-bit, long is 64-bit, hence this will write beyond mtd_num and will corrupt
>>> the stack.
>>
>> Yeah, you are right. This really may write beyond dev.
>>
>> The kstrtoul(const char *s, unsigned int base, unsigned long *res) only accept unsigned long
>> pointer as the third parameter.
>> And the original function simple_strtoul() returns unsigned long type value.
>> It is also cast. So this may not corrupt the stack.
>>
>> Or do you have any better suggestion about this?
> 
> kstrtoint().
> 
Hi Geert,

Thanks for your suggestion, i will send the version2 out.
> Gr{oetje,eeting}s,
> 
>                         Geert
> 
> --
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
> 
> 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
> 
> 

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

end of thread, other threads:[~2014-05-20  8:15 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <1400488570-20288-1-git-send-email-zhenzhang.zhang@huawei.com>
2014-05-19  8:38 ` [PATCH] ubi: replace simple_strtoul() with kstrtoul() Zhang Zhen
2014-05-19  9:14   ` Geert Uytterhoeven
2014-05-20  1:18     ` Zhang Zhen
2014-05-20  6:57       ` Geert Uytterhoeven
2014-05-20  8:14         ` Zhang Zhen

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