qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Fam Zheng <famz@redhat.com>
To: qemu-devel@nongnu.org
Cc: Luiz Capitulino <lcapitul@redhat.com>, Fam Zheng <famz@redhat.com>
Subject: [Qemu-devel] [PATCH] monitor: fix parsing of big int
Date: Thu,  1 Aug 2013 14:31:35 +0800	[thread overview]
Message-ID: <1375338695-670-1-git-send-email-famz@redhat.com> (raw)

We call strtoull(3) to parse a string to int. the range we can accept
with our local variable "int64_t n" is (-9223372036854775808 ~
9223372036854775807), but strtoull(3) can return (0 ~
18446744073709551615UL).

So when we pass a int from HMP within the range of 9223372036854775808 ~
18446744073709551615, it's safely converted and implicitly casted to
int64_t, so n is assigned a negative value, silently, which is
incorrect.  We can verify this by

    (HMP) block_set_io_throttle ide0-hd0 999999999999999999 0 0 0 0 0
    (HMP) block_set_io_throttle ide0-hd0 9999999999999999999 0 0 0 0 0
    bps and iops values must be 0 or greater
    (HMP) block_set_io_throttle ide0-hd0 99999999999999999999 0 0 0 0 0
    number too large

The first command succeeds, the second is reporting a negative value
error, the third command has correct prompt.

Fix it by calling strtoll instead, which will report ERANGE as expected.

    (HMP) block_set_io_throttle ide0-hd0 999999999999999999 0 0 0 0 0
    (HMP) block_set_io_throttle ide0-hd0 9999999999999999999 0 0 0 0 0
    number too large
    (HMP) block_set_io_throttle ide0-hd0 99999999999999999999 0 0 0 0 0
    number too large

Signed-off-by: Fam Zheng <famz@redhat.com>
---
 monitor.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/monitor.c b/monitor.c
index 5dc0aa9..7bfb469 100644
--- a/monitor.c
+++ b/monitor.c
@@ -3286,7 +3286,7 @@ static int64_t expr_unary(Monitor *mon)
         break;
     default:
         errno = 0;
-        n = strtoull(pch, &p, 0);
+        n = strtoll(pch, &p, 0);
         if (errno == ERANGE) {
             expr_error(mon, "number too large");
         }
-- 
1.8.3.4

             reply	other threads:[~2013-08-01  6:32 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-01  6:31 Fam Zheng [this message]
2013-08-01 13:52 ` [Qemu-devel] [PATCH] monitor: fix parsing of big int Eric Blake
2013-08-01 14:00   ` Luiz Capitulino
2013-08-02  2:39     ` Fam Zheng
2013-08-02  3:07   ` Fam Zheng

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=1375338695-670-1-git-send-email-famz@redhat.com \
    --to=famz@redhat.com \
    --cc=lcapitul@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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).