diff mbox

[v4,1/3] qtest: replace strtoXX() by qemu_strtoXX()

Message ID 1473167877-2545-2-git-send-email-lvivier@redhat.com
State New
Headers show

Commit Message

Laurent Vivier Sept. 6, 2016, 1:17 p.m. UTC
Signed-off-by: Laurent Vivier <lvivier@redhat.com>
---
v4:
- add this patch in the series to change all strtoXX() in qtest.c

 qtest.c | 49 ++++++++++++++++++++++++++-----------------------
 1 file changed, 26 insertions(+), 23 deletions(-)

Comments

Greg Kurz Sept. 6, 2016, 3:55 p.m. UTC | #1
On Tue,  6 Sep 2016 15:17:55 +0200
Laurent Vivier <lvivier@redhat.com> wrote:

> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> ---

The patch also adds error checking and assertions. Maybe worth to be mentioned
in the changelog...

> v4:
> - add this patch in the series to change all strtoXX() in qtest.c
> 
>  qtest.c | 49 ++++++++++++++++++++++++++-----------------------
>  1 file changed, 26 insertions(+), 23 deletions(-)
> 
> diff --git a/qtest.c b/qtest.c
> index da4826c..4c94708 100644
> --- a/qtest.c
> +++ b/qtest.c
> @@ -27,6 +27,7 @@
>  #include "qemu/config-file.h"
>  #include "qemu/option.h"
>  #include "qemu/error-report.h"
> +#include "qemu/cutils.h"
>  
>  #define MAX_IRQ 256
>  
> @@ -324,12 +325,13 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>      } else if (strcmp(words[0], "outb") == 0 ||
>                 strcmp(words[0], "outw") == 0 ||
>                 strcmp(words[0], "outl") == 0) {
> -        uint16_t addr;
> -        uint32_t value;
> +        unsigned long addr;
> +        unsigned long value;
>  
>          g_assert(words[1] && words[2]);
> -        addr = strtoul(words[1], NULL, 0);
> -        value = strtoul(words[2], NULL, 0);
> +        g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoul(words[2], NULL, 0, &value) == 0);
> +        g_assert(addr <= 0xffff);
>  
>          if (words[0][3] == 'b') {
>              cpu_outb(addr, value);
> @@ -343,11 +345,12 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>      } else if (strcmp(words[0], "inb") == 0 ||
>          strcmp(words[0], "inw") == 0 ||
>          strcmp(words[0], "inl") == 0) {
> -        uint16_t addr;
> +        unsigned long addr;
>          uint32_t value = -1U;
>  
>          g_assert(words[1]);
> -        addr = strtoul(words[1], NULL, 0);
> +        g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
> +        g_assert(addr <= 0xffff);
>  
>          if (words[0][2] == 'b') {
>              value = cpu_inb(addr);
> @@ -366,8 +369,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          uint64_t value;
>  
>          g_assert(words[1] && words[2]);
> -        addr = strtoull(words[1], NULL, 0);
> -        value = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &value) == 0);
>  
>          if (words[0][5] == 'b') {
>              uint8_t data = value;
> @@ -395,7 +398,7 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          uint64_t value = UINT64_C(-1);
>  
>          g_assert(words[1]);
> -        addr = strtoull(words[1], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
>  
>          if (words[0][4] == 'b') {
>              uint8_t data;
> @@ -421,8 +424,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          char *enc;
>  
>          g_assert(words[1] && words[2]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>  
>          data = g_malloc(len);
>          cpu_physical_memory_read(addr, data, len);
> @@ -443,8 +446,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          gchar *b64_data;
>  
>          g_assert(words[1] && words[2]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>  
>          data = g_malloc(len);
>          cpu_physical_memory_read(addr, data, len);
> @@ -460,8 +463,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          size_t data_len;
>  
>          g_assert(words[1] && words[2] && words[3]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>  
>          data_len = strlen(words[3]);
>          if (data_len < 3) {
> @@ -486,12 +489,12 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>      } else if (strcmp(words[0], "memset") == 0) {
>          uint64_t addr, len;
>          uint8_t *data;
> -        uint8_t pattern;
> +        unsigned long pattern;
>  
>          g_assert(words[1] && words[2] && words[3]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> -        pattern = strtoull(words[3], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
> +        g_assert(qemu_strtoul(words[3], NULL, 0, &pattern) == 0);

And:

g_assert(pattern <= 0xff)

>  
>          data = g_malloc(len);
>          memset(data, pattern, len);
> @@ -507,8 +510,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          gsize out_len;
>  
>          g_assert(words[1] && words[2] && words[3]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>  
>          data_len = strlen(words[3]);
>          if (data_len < 3) {
> @@ -532,7 +535,7 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          int64_t ns;
>  
>          if (words[1]) {
> -            ns = strtoll(words[1], NULL, 0);
> +            g_assert(qemu_strtoll(words[1], NULL, 0, &ns) == 0);
>          } else {
>              ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
>          }
> @@ -544,7 +547,7 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          int64_t ns;
>  
>          g_assert(words[1]);
> -        ns = strtoll(words[1], NULL, 0);
> +        g_assert(qemu_strtoll(words[1], NULL, 0, &ns) == 0);
>          qtest_clock_warp(ns);
>          qtest_send_prefix(chr);
>          qtest_sendf(chr, "OK %"PRIi64"\n",
Laurent Vivier Sept. 6, 2016, 6:17 p.m. UTC | #2
On 06/09/2016 17:55, Greg Kurz wrote:
> On Tue,  6 Sep 2016 15:17:55 +0200
> Laurent Vivier <lvivier@redhat.com> wrote:
> 
>> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
>> ---
> 
> The patch also adds error checking and assertions. Maybe worth to be mentioned
> in the changelog...

In case of a new version of the patch, I will...

> 
>> v4:
>> - add this patch in the series to change all strtoXX() in qtest.c
>>
>>  qtest.c | 49 ++++++++++++++++++++++++++-----------------------
>>  1 file changed, 26 insertions(+), 23 deletions(-)
>>
>> diff --git a/qtest.c b/qtest.c
>> index da4826c..4c94708 100644
>> --- a/qtest.c
>> +++ b/qtest.c
>> @@ -27,6 +27,7 @@
>>  #include "qemu/config-file.h"
>>  #include "qemu/option.h"
>>  #include "qemu/error-report.h"
>> +#include "qemu/cutils.h"
>>  
>>  #define MAX_IRQ 256
>>  
>> @@ -324,12 +325,13 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>>      } else if (strcmp(words[0], "outb") == 0 ||
>>                 strcmp(words[0], "outw") == 0 ||
>>                 strcmp(words[0], "outl") == 0) {
>> -        uint16_t addr;
>> -        uint32_t value;
>> +        unsigned long addr;
>> +        unsigned long value;
>>  
>>          g_assert(words[1] && words[2]);
>> -        addr = strtoul(words[1], NULL, 0);
>> -        value = strtoul(words[2], NULL, 0);
>> +        g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
>> +        g_assert(qemu_strtoul(words[2], NULL, 0, &value) == 0);
>> +        g_assert(addr <= 0xffff);
>>  
>>          if (words[0][3] == 'b') {
>>              cpu_outb(addr, value);
>> @@ -343,11 +345,12 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>>      } else if (strcmp(words[0], "inb") == 0 ||
>>          strcmp(words[0], "inw") == 0 ||
>>          strcmp(words[0], "inl") == 0) {
>> -        uint16_t addr;
>> +        unsigned long addr;
>>          uint32_t value = -1U;
>>  
>>          g_assert(words[1]);
>> -        addr = strtoul(words[1], NULL, 0);
>> +        g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
>> +        g_assert(addr <= 0xffff);
>>  
>>          if (words[0][2] == 'b') {
>>              value = cpu_inb(addr);
>> @@ -366,8 +369,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>>          uint64_t value;
>>  
>>          g_assert(words[1] && words[2]);
>> -        addr = strtoull(words[1], NULL, 0);
>> -        value = strtoull(words[2], NULL, 0);
>> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
>> +        g_assert(qemu_strtoull(words[2], NULL, 0, &value) == 0);
>>  
>>          if (words[0][5] == 'b') {
>>              uint8_t data = value;
>> @@ -395,7 +398,7 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>>          uint64_t value = UINT64_C(-1);
>>  
>>          g_assert(words[1]);
>> -        addr = strtoull(words[1], NULL, 0);
>> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
>>  
>>          if (words[0][4] == 'b') {
>>              uint8_t data;
>> @@ -421,8 +424,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>>          char *enc;
>>  
>>          g_assert(words[1] && words[2]);
>> -        addr = strtoull(words[1], NULL, 0);
>> -        len = strtoull(words[2], NULL, 0);
>> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
>> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>>  
>>          data = g_malloc(len);
>>          cpu_physical_memory_read(addr, data, len);
>> @@ -443,8 +446,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>>          gchar *b64_data;
>>  
>>          g_assert(words[1] && words[2]);
>> -        addr = strtoull(words[1], NULL, 0);
>> -        len = strtoull(words[2], NULL, 0);
>> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
>> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>>  
>>          data = g_malloc(len);
>>          cpu_physical_memory_read(addr, data, len);
>> @@ -460,8 +463,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>>          size_t data_len;
>>  
>>          g_assert(words[1] && words[2] && words[3]);
>> -        addr = strtoull(words[1], NULL, 0);
>> -        len = strtoull(words[2], NULL, 0);
>> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
>> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>>  
>>          data_len = strlen(words[3]);
>>          if (data_len < 3) {
>> @@ -486,12 +489,12 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>>      } else if (strcmp(words[0], "memset") == 0) {
>>          uint64_t addr, len;
>>          uint8_t *data;
>> -        uint8_t pattern;
>> +        unsigned long pattern;
>>  
>>          g_assert(words[1] && words[2] && words[3]);
>> -        addr = strtoull(words[1], NULL, 0);
>> -        len = strtoull(words[2], NULL, 0);
>> -        pattern = strtoull(words[3], NULL, 0);
>> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
>> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>> +        g_assert(qemu_strtoul(words[3], NULL, 0, &pattern) == 0);
> 
> And:
> 
> g_assert(pattern <= 0xff)

I think pattern > 0xff is valid as memset() takes an "int" and only uses
the byte value (for instance to use -1 to fill memory with 0xff). It
can't do bad things...

In the previous case ("g_assert(addr <= 0xffff)"), if addr > 0xffff,
cpu_out/in can write at a bad address. We could just ignore the upper
part of the word, but to debug test case I think it's good to have an
assert in this case.

Thanks,
Laurent
Greg Kurz Sept. 6, 2016, 9:13 p.m. UTC | #3
On Tue, 6 Sep 2016 20:17:00 +0200
Laurent Vivier <lvivier@redhat.com> wrote:

> On 06/09/2016 17:55, Greg Kurz wrote:
> > On Tue,  6 Sep 2016 15:17:55 +0200
> > Laurent Vivier <lvivier@redhat.com> wrote:
> >   
> >> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> >> ---  
> > 
> > The patch also adds error checking and assertions. Maybe worth to be mentioned
> > in the changelog...  
> 
> In case of a new version of the patch, I will...
> 
> >   
> >> v4:
> >> - add this patch in the series to change all strtoXX() in qtest.c
> >>
> >>  qtest.c | 49 ++++++++++++++++++++++++++-----------------------
> >>  1 file changed, 26 insertions(+), 23 deletions(-)
> >>
> >> diff --git a/qtest.c b/qtest.c
> >> index da4826c..4c94708 100644
> >> --- a/qtest.c
> >> +++ b/qtest.c
> >> @@ -27,6 +27,7 @@
> >>  #include "qemu/config-file.h"
> >>  #include "qemu/option.h"
> >>  #include "qemu/error-report.h"
> >> +#include "qemu/cutils.h"
> >>  
> >>  #define MAX_IRQ 256
> >>  
> >> @@ -324,12 +325,13 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
> >>      } else if (strcmp(words[0], "outb") == 0 ||
> >>                 strcmp(words[0], "outw") == 0 ||
> >>                 strcmp(words[0], "outl") == 0) {
> >> -        uint16_t addr;
> >> -        uint32_t value;
> >> +        unsigned long addr;
> >> +        unsigned long value;
> >>  
> >>          g_assert(words[1] && words[2]);
> >> -        addr = strtoul(words[1], NULL, 0);
> >> -        value = strtoul(words[2], NULL, 0);
> >> +        g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
> >> +        g_assert(qemu_strtoul(words[2], NULL, 0, &value) == 0);
> >> +        g_assert(addr <= 0xffff);
> >>  
> >>          if (words[0][3] == 'b') {
> >>              cpu_outb(addr, value);
> >> @@ -343,11 +345,12 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
> >>      } else if (strcmp(words[0], "inb") == 0 ||
> >>          strcmp(words[0], "inw") == 0 ||
> >>          strcmp(words[0], "inl") == 0) {
> >> -        uint16_t addr;
> >> +        unsigned long addr;
> >>          uint32_t value = -1U;
> >>  
> >>          g_assert(words[1]);
> >> -        addr = strtoul(words[1], NULL, 0);
> >> +        g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
> >> +        g_assert(addr <= 0xffff);
> >>  
> >>          if (words[0][2] == 'b') {
> >>              value = cpu_inb(addr);
> >> @@ -366,8 +369,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
> >>          uint64_t value;
> >>  
> >>          g_assert(words[1] && words[2]);
> >> -        addr = strtoull(words[1], NULL, 0);
> >> -        value = strtoull(words[2], NULL, 0);
> >> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> >> +        g_assert(qemu_strtoull(words[2], NULL, 0, &value) == 0);
> >>  
> >>          if (words[0][5] == 'b') {
> >>              uint8_t data = value;
> >> @@ -395,7 +398,7 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
> >>          uint64_t value = UINT64_C(-1);
> >>  
> >>          g_assert(words[1]);
> >> -        addr = strtoull(words[1], NULL, 0);
> >> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> >>  
> >>          if (words[0][4] == 'b') {
> >>              uint8_t data;
> >> @@ -421,8 +424,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
> >>          char *enc;
> >>  
> >>          g_assert(words[1] && words[2]);
> >> -        addr = strtoull(words[1], NULL, 0);
> >> -        len = strtoull(words[2], NULL, 0);
> >> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> >> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
> >>  
> >>          data = g_malloc(len);
> >>          cpu_physical_memory_read(addr, data, len);
> >> @@ -443,8 +446,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
> >>          gchar *b64_data;
> >>  
> >>          g_assert(words[1] && words[2]);
> >> -        addr = strtoull(words[1], NULL, 0);
> >> -        len = strtoull(words[2], NULL, 0);
> >> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> >> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
> >>  
> >>          data = g_malloc(len);
> >>          cpu_physical_memory_read(addr, data, len);
> >> @@ -460,8 +463,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
> >>          size_t data_len;
> >>  
> >>          g_assert(words[1] && words[2] && words[3]);
> >> -        addr = strtoull(words[1], NULL, 0);
> >> -        len = strtoull(words[2], NULL, 0);
> >> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> >> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
> >>  
> >>          data_len = strlen(words[3]);
> >>          if (data_len < 3) {
> >> @@ -486,12 +489,12 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
> >>      } else if (strcmp(words[0], "memset") == 0) {
> >>          uint64_t addr, len;
> >>          uint8_t *data;
> >> -        uint8_t pattern;
> >> +        unsigned long pattern;
> >>  
> >>          g_assert(words[1] && words[2] && words[3]);
> >> -        addr = strtoull(words[1], NULL, 0);
> >> -        len = strtoull(words[2], NULL, 0);
> >> -        pattern = strtoull(words[3], NULL, 0);
> >> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> >> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
> >> +        g_assert(qemu_strtoul(words[3], NULL, 0, &pattern) == 0);  
> > 
> > And:
> > 
> > g_assert(pattern <= 0xff)  
> 
> I think pattern > 0xff is valid as memset() takes an "int" and only uses
> the byte value (for instance to use -1 to fill memory with 0xff). It
> can't do bad things...
> 

Of course... sorry for the noise :)

> In the previous case ("g_assert(addr <= 0xffff)"), if addr > 0xffff,
> cpu_out/in can write at a bad address. We could just ignore the upper
> part of the word, but to debug test case I think it's good to have an
> assert in this case.
> 

It makes sense indeed.

> Thanks,
> Laurent

Cheers.

--
Greg
David Gibson Sept. 8, 2016, 1:56 a.m. UTC | #4
On Tue, Sep 06, 2016 at 03:17:55PM +0200, Laurent Vivier wrote:
> Signed-off-by: Laurent Vivier <lvivier@redhat.com>

Reviewed-by: David Gibson <david@gibson.dropbear.id.au>

> ---
> v4:
> - add this patch in the series to change all strtoXX() in qtest.c
> 
>  qtest.c | 49 ++++++++++++++++++++++++++-----------------------
>  1 file changed, 26 insertions(+), 23 deletions(-)
> 
> diff --git a/qtest.c b/qtest.c
> index da4826c..4c94708 100644
> --- a/qtest.c
> +++ b/qtest.c
> @@ -27,6 +27,7 @@
>  #include "qemu/config-file.h"
>  #include "qemu/option.h"
>  #include "qemu/error-report.h"
> +#include "qemu/cutils.h"
>  
>  #define MAX_IRQ 256
>  
> @@ -324,12 +325,13 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>      } else if (strcmp(words[0], "outb") == 0 ||
>                 strcmp(words[0], "outw") == 0 ||
>                 strcmp(words[0], "outl") == 0) {
> -        uint16_t addr;
> -        uint32_t value;
> +        unsigned long addr;
> +        unsigned long value;
>  
>          g_assert(words[1] && words[2]);
> -        addr = strtoul(words[1], NULL, 0);
> -        value = strtoul(words[2], NULL, 0);
> +        g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoul(words[2], NULL, 0, &value) == 0);
> +        g_assert(addr <= 0xffff);
>  
>          if (words[0][3] == 'b') {
>              cpu_outb(addr, value);
> @@ -343,11 +345,12 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>      } else if (strcmp(words[0], "inb") == 0 ||
>          strcmp(words[0], "inw") == 0 ||
>          strcmp(words[0], "inl") == 0) {
> -        uint16_t addr;
> +        unsigned long addr;
>          uint32_t value = -1U;
>  
>          g_assert(words[1]);
> -        addr = strtoul(words[1], NULL, 0);
> +        g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
> +        g_assert(addr <= 0xffff);
>  
>          if (words[0][2] == 'b') {
>              value = cpu_inb(addr);
> @@ -366,8 +369,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          uint64_t value;
>  
>          g_assert(words[1] && words[2]);
> -        addr = strtoull(words[1], NULL, 0);
> -        value = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &value) == 0);
>  
>          if (words[0][5] == 'b') {
>              uint8_t data = value;
> @@ -395,7 +398,7 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          uint64_t value = UINT64_C(-1);
>  
>          g_assert(words[1]);
> -        addr = strtoull(words[1], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
>  
>          if (words[0][4] == 'b') {
>              uint8_t data;
> @@ -421,8 +424,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          char *enc;
>  
>          g_assert(words[1] && words[2]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>  
>          data = g_malloc(len);
>          cpu_physical_memory_read(addr, data, len);
> @@ -443,8 +446,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          gchar *b64_data;
>  
>          g_assert(words[1] && words[2]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>  
>          data = g_malloc(len);
>          cpu_physical_memory_read(addr, data, len);
> @@ -460,8 +463,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          size_t data_len;
>  
>          g_assert(words[1] && words[2] && words[3]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>  
>          data_len = strlen(words[3]);
>          if (data_len < 3) {
> @@ -486,12 +489,12 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>      } else if (strcmp(words[0], "memset") == 0) {
>          uint64_t addr, len;
>          uint8_t *data;
> -        uint8_t pattern;
> +        unsigned long pattern;
>  
>          g_assert(words[1] && words[2] && words[3]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> -        pattern = strtoull(words[3], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
> +        g_assert(qemu_strtoul(words[3], NULL, 0, &pattern) == 0);
>  
>          data = g_malloc(len);
>          memset(data, pattern, len);
> @@ -507,8 +510,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          gsize out_len;
>  
>          g_assert(words[1] && words[2] && words[3]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>  
>          data_len = strlen(words[3]);
>          if (data_len < 3) {
> @@ -532,7 +535,7 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          int64_t ns;
>  
>          if (words[1]) {
> -            ns = strtoll(words[1], NULL, 0);
> +            g_assert(qemu_strtoll(words[1], NULL, 0, &ns) == 0);
>          } else {
>              ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
>          }
> @@ -544,7 +547,7 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          int64_t ns;
>  
>          g_assert(words[1]);
> -        ns = strtoll(words[1], NULL, 0);
> +        g_assert(qemu_strtoll(words[1], NULL, 0, &ns) == 0);
>          qtest_clock_warp(ns);
>          qtest_send_prefix(chr);
>          qtest_sendf(chr, "OK %"PRIi64"\n",
Greg Kurz Sept. 8, 2016, 7:55 a.m. UTC | #5
On Tue,  6 Sep 2016 15:17:55 +0200
Laurent Vivier <lvivier@redhat.com> wrote:

> Signed-off-by: Laurent Vivier <lvivier@redhat.com>
> ---

FWIW,

Reviewed-by: Greg Kurz <groug@kaod.org>

> v4:
> - add this patch in the series to change all strtoXX() in qtest.c
> 
>  qtest.c | 49 ++++++++++++++++++++++++++-----------------------
>  1 file changed, 26 insertions(+), 23 deletions(-)
> 
> diff --git a/qtest.c b/qtest.c
> index da4826c..4c94708 100644
> --- a/qtest.c
> +++ b/qtest.c
> @@ -27,6 +27,7 @@
>  #include "qemu/config-file.h"
>  #include "qemu/option.h"
>  #include "qemu/error-report.h"
> +#include "qemu/cutils.h"
>  
>  #define MAX_IRQ 256
>  
> @@ -324,12 +325,13 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>      } else if (strcmp(words[0], "outb") == 0 ||
>                 strcmp(words[0], "outw") == 0 ||
>                 strcmp(words[0], "outl") == 0) {
> -        uint16_t addr;
> -        uint32_t value;
> +        unsigned long addr;
> +        unsigned long value;
>  
>          g_assert(words[1] && words[2]);
> -        addr = strtoul(words[1], NULL, 0);
> -        value = strtoul(words[2], NULL, 0);
> +        g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoul(words[2], NULL, 0, &value) == 0);
> +        g_assert(addr <= 0xffff);
>  
>          if (words[0][3] == 'b') {
>              cpu_outb(addr, value);
> @@ -343,11 +345,12 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>      } else if (strcmp(words[0], "inb") == 0 ||
>          strcmp(words[0], "inw") == 0 ||
>          strcmp(words[0], "inl") == 0) {
> -        uint16_t addr;
> +        unsigned long addr;
>          uint32_t value = -1U;
>  
>          g_assert(words[1]);
> -        addr = strtoul(words[1], NULL, 0);
> +        g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
> +        g_assert(addr <= 0xffff);
>  
>          if (words[0][2] == 'b') {
>              value = cpu_inb(addr);
> @@ -366,8 +369,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          uint64_t value;
>  
>          g_assert(words[1] && words[2]);
> -        addr = strtoull(words[1], NULL, 0);
> -        value = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &value) == 0);
>  
>          if (words[0][5] == 'b') {
>              uint8_t data = value;
> @@ -395,7 +398,7 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          uint64_t value = UINT64_C(-1);
>  
>          g_assert(words[1]);
> -        addr = strtoull(words[1], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
>  
>          if (words[0][4] == 'b') {
>              uint8_t data;
> @@ -421,8 +424,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          char *enc;
>  
>          g_assert(words[1] && words[2]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>  
>          data = g_malloc(len);
>          cpu_physical_memory_read(addr, data, len);
> @@ -443,8 +446,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          gchar *b64_data;
>  
>          g_assert(words[1] && words[2]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>  
>          data = g_malloc(len);
>          cpu_physical_memory_read(addr, data, len);
> @@ -460,8 +463,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          size_t data_len;
>  
>          g_assert(words[1] && words[2] && words[3]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>  
>          data_len = strlen(words[3]);
>          if (data_len < 3) {
> @@ -486,12 +489,12 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>      } else if (strcmp(words[0], "memset") == 0) {
>          uint64_t addr, len;
>          uint8_t *data;
> -        uint8_t pattern;
> +        unsigned long pattern;
>  
>          g_assert(words[1] && words[2] && words[3]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> -        pattern = strtoull(words[3], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
> +        g_assert(qemu_strtoul(words[3], NULL, 0, &pattern) == 0);
>  
>          data = g_malloc(len);
>          memset(data, pattern, len);
> @@ -507,8 +510,8 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          gsize out_len;
>  
>          g_assert(words[1] && words[2] && words[3]);
> -        addr = strtoull(words[1], NULL, 0);
> -        len = strtoull(words[2], NULL, 0);
> +        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
> +        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
>  
>          data_len = strlen(words[3]);
>          if (data_len < 3) {
> @@ -532,7 +535,7 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          int64_t ns;
>  
>          if (words[1]) {
> -            ns = strtoll(words[1], NULL, 0);
> +            g_assert(qemu_strtoll(words[1], NULL, 0, &ns) == 0);
>          } else {
>              ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
>          }
> @@ -544,7 +547,7 @@ static void qtest_process_command(CharDriverState *chr, gchar **words)
>          int64_t ns;
>  
>          g_assert(words[1]);
> -        ns = strtoll(words[1], NULL, 0);
> +        g_assert(qemu_strtoll(words[1], NULL, 0, &ns) == 0);
>          qtest_clock_warp(ns);
>          qtest_send_prefix(chr);
>          qtest_sendf(chr, "OK %"PRIi64"\n",
diff mbox

Patch

diff --git a/qtest.c b/qtest.c
index da4826c..4c94708 100644
--- a/qtest.c
+++ b/qtest.c
@@ -27,6 +27,7 @@ 
 #include "qemu/config-file.h"
 #include "qemu/option.h"
 #include "qemu/error-report.h"
+#include "qemu/cutils.h"
 
 #define MAX_IRQ 256
 
@@ -324,12 +325,13 @@  static void qtest_process_command(CharDriverState *chr, gchar **words)
     } else if (strcmp(words[0], "outb") == 0 ||
                strcmp(words[0], "outw") == 0 ||
                strcmp(words[0], "outl") == 0) {
-        uint16_t addr;
-        uint32_t value;
+        unsigned long addr;
+        unsigned long value;
 
         g_assert(words[1] && words[2]);
-        addr = strtoul(words[1], NULL, 0);
-        value = strtoul(words[2], NULL, 0);
+        g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
+        g_assert(qemu_strtoul(words[2], NULL, 0, &value) == 0);
+        g_assert(addr <= 0xffff);
 
         if (words[0][3] == 'b') {
             cpu_outb(addr, value);
@@ -343,11 +345,12 @@  static void qtest_process_command(CharDriverState *chr, gchar **words)
     } else if (strcmp(words[0], "inb") == 0 ||
         strcmp(words[0], "inw") == 0 ||
         strcmp(words[0], "inl") == 0) {
-        uint16_t addr;
+        unsigned long addr;
         uint32_t value = -1U;
 
         g_assert(words[1]);
-        addr = strtoul(words[1], NULL, 0);
+        g_assert(qemu_strtoul(words[1], NULL, 0, &addr) == 0);
+        g_assert(addr <= 0xffff);
 
         if (words[0][2] == 'b') {
             value = cpu_inb(addr);
@@ -366,8 +369,8 @@  static void qtest_process_command(CharDriverState *chr, gchar **words)
         uint64_t value;
 
         g_assert(words[1] && words[2]);
-        addr = strtoull(words[1], NULL, 0);
-        value = strtoull(words[2], NULL, 0);
+        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
+        g_assert(qemu_strtoull(words[2], NULL, 0, &value) == 0);
 
         if (words[0][5] == 'b') {
             uint8_t data = value;
@@ -395,7 +398,7 @@  static void qtest_process_command(CharDriverState *chr, gchar **words)
         uint64_t value = UINT64_C(-1);
 
         g_assert(words[1]);
-        addr = strtoull(words[1], NULL, 0);
+        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
 
         if (words[0][4] == 'b') {
             uint8_t data;
@@ -421,8 +424,8 @@  static void qtest_process_command(CharDriverState *chr, gchar **words)
         char *enc;
 
         g_assert(words[1] && words[2]);
-        addr = strtoull(words[1], NULL, 0);
-        len = strtoull(words[2], NULL, 0);
+        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
+        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
 
         data = g_malloc(len);
         cpu_physical_memory_read(addr, data, len);
@@ -443,8 +446,8 @@  static void qtest_process_command(CharDriverState *chr, gchar **words)
         gchar *b64_data;
 
         g_assert(words[1] && words[2]);
-        addr = strtoull(words[1], NULL, 0);
-        len = strtoull(words[2], NULL, 0);
+        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
+        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
 
         data = g_malloc(len);
         cpu_physical_memory_read(addr, data, len);
@@ -460,8 +463,8 @@  static void qtest_process_command(CharDriverState *chr, gchar **words)
         size_t data_len;
 
         g_assert(words[1] && words[2] && words[3]);
-        addr = strtoull(words[1], NULL, 0);
-        len = strtoull(words[2], NULL, 0);
+        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
+        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
 
         data_len = strlen(words[3]);
         if (data_len < 3) {
@@ -486,12 +489,12 @@  static void qtest_process_command(CharDriverState *chr, gchar **words)
     } else if (strcmp(words[0], "memset") == 0) {
         uint64_t addr, len;
         uint8_t *data;
-        uint8_t pattern;
+        unsigned long pattern;
 
         g_assert(words[1] && words[2] && words[3]);
-        addr = strtoull(words[1], NULL, 0);
-        len = strtoull(words[2], NULL, 0);
-        pattern = strtoull(words[3], NULL, 0);
+        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
+        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
+        g_assert(qemu_strtoul(words[3], NULL, 0, &pattern) == 0);
 
         data = g_malloc(len);
         memset(data, pattern, len);
@@ -507,8 +510,8 @@  static void qtest_process_command(CharDriverState *chr, gchar **words)
         gsize out_len;
 
         g_assert(words[1] && words[2] && words[3]);
-        addr = strtoull(words[1], NULL, 0);
-        len = strtoull(words[2], NULL, 0);
+        g_assert(qemu_strtoull(words[1], NULL, 0, &addr) == 0);
+        g_assert(qemu_strtoull(words[2], NULL, 0, &len) == 0);
 
         data_len = strlen(words[3]);
         if (data_len < 3) {
@@ -532,7 +535,7 @@  static void qtest_process_command(CharDriverState *chr, gchar **words)
         int64_t ns;
 
         if (words[1]) {
-            ns = strtoll(words[1], NULL, 0);
+            g_assert(qemu_strtoll(words[1], NULL, 0, &ns) == 0);
         } else {
             ns = qemu_clock_deadline_ns_all(QEMU_CLOCK_VIRTUAL);
         }
@@ -544,7 +547,7 @@  static void qtest_process_command(CharDriverState *chr, gchar **words)
         int64_t ns;
 
         g_assert(words[1]);
-        ns = strtoll(words[1], NULL, 0);
+        g_assert(qemu_strtoll(words[1], NULL, 0, &ns) == 0);
         qtest_clock_warp(ns);
         qtest_send_prefix(chr);
         qtest_sendf(chr, "OK %"PRIi64"\n",