From: Andrew Jones <drjones@redhat.com>
To: kvm@vger.kernel.org
Cc: pbonzini@redhat.com, bgardon@google.com, borntraeger@de.ibm.com,
frankja@linux.ibm.com, thuth@redhat.com, peterx@redhat.com
Subject: [PATCH 04/13] fixup! KVM: selftests: Add memory size parameter to the demand paging test
Date: Fri, 14 Feb 2020 15:59:11 +0100 [thread overview]
Message-ID: <20200214145920.30792-5-drjones@redhat.com> (raw)
In-Reply-To: <20200214145920.30792-1-drjones@redhat.com>
[Rewrote parse_size() to simplify and provide user more flexibility as
to how sizes are input. Also fixed size overflow assert.]
Signed-off-by: Andrew Jones <drjones@redhat.com>
---
tools/testing/selftests/kvm/lib/test_util.c | 76 +++++++++------------
1 file changed, 33 insertions(+), 43 deletions(-)
diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
index 706e0f963a44..cbd7f51b07a1 100644
--- a/tools/testing/selftests/kvm/lib/test_util.c
+++ b/tools/testing/selftests/kvm/lib/test_util.c
@@ -4,58 +4,48 @@
*
* Copyright (C) 2020, Google LLC.
*/
-
-#include "test_util.h"
-
+#include <stdlib.h>
#include <ctype.h>
+#include <limits.h>
+#include "test_util.h"
/*
* Parses "[0-9]+[kmgt]?".
*/
size_t parse_size(const char *size)
{
- size_t len = strlen(size);
- size_t i;
- size_t scale_shift = 0;
size_t base;
-
- TEST_ASSERT(len > 0, "Need at least 1 digit in '%s'", size);
-
- /* Find the first letter in the string, indicating scale. */
- for (i = 0; i < len; i++) {
- if (!isdigit(size[i])) {
- TEST_ASSERT(i > 0, "Need at least 1 digit in '%s'",
- size);
- TEST_ASSERT(i == len - 1,
- "Expected letter at the end in '%s'.",
- size);
- switch (tolower(size[i])) {
- case 't':
- scale_shift = 40;
- break;
- case 'g':
- scale_shift = 30;
- break;
- case 'm':
- scale_shift = 20;
- break;
- case 'k':
- scale_shift = 10;
- break;
- default:
- TEST_ASSERT(false, "Unknown size letter %c",
- size[i]);
- }
- }
+ char *scale;
+ int shift = 0;
+
+ TEST_ASSERT(size && isdigit(size[0]), "Need at least one digit in '%s'", size);
+
+ base = strtoull(size, &scale, 0);
+
+ TEST_ASSERT(base != ULLONG_MAX, "Overflow parsing size!");
+
+ switch (tolower(*scale)) {
+ case 't':
+ shift = 40;
+ break;
+ case 'g':
+ shift = 30;
+ break;
+ case 'm':
+ shift = 20;
+ break;
+ case 'k':
+ shift = 10;
+ break;
+ case 'b':
+ case '\0':
+ shift = 0;
+ break;
+ default:
+ TEST_ASSERT(false, "Unknown size letter %c", *scale);
}
- TEST_ASSERT(scale_shift < 8 * sizeof(size_t),
- "Overflow parsing scale!");
-
- base = atoi(size);
-
- TEST_ASSERT(!(base & ~((1 << (sizeof(size_t) - scale_shift)) - 1)),
- "Overflow parsing size!");
+ TEST_ASSERT((base << shift) >> shift == base, "Overflow scaling size!");
- return base << scale_shift;
+ return base << shift;
}
--
2.21.1
next prev parent reply other threads:[~2020-02-14 14:59 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-02-14 14:59 [PATCH 00/13] KVM: selftests: Various fixes and cleanups Andrew Jones
2020-02-14 14:59 ` [PATCH 01/13] HACK: Ensure __NR_userfaultfd is defined Andrew Jones
2020-02-20 16:38 ` Paolo Bonzini
2020-02-14 14:59 ` [PATCH 02/13] fixup! KVM: selftests: Add support for vcpu_args_set to aarch64 and s390x Andrew Jones
2020-02-14 20:35 ` Christian Borntraeger
2020-02-15 7:04 ` Andrew Jones
2020-02-18 17:30 ` Ben Gardon
2020-02-18 17:38 ` Andrew Jones
2020-02-20 16:40 ` Paolo Bonzini
2020-02-14 14:59 ` [PATCH 03/13] fixup! KVM: selftests: Support multiple vCPUs in demand paging test Andrew Jones
2020-02-18 17:39 ` Ben Gardon
2020-02-14 14:59 ` Andrew Jones [this message]
2020-02-18 17:43 ` [PATCH 04/13] fixup! KVM: selftests: Add memory size parameter to the " Ben Gardon
2020-02-14 14:59 ` [PATCH 05/13] fixup! KVM: selftests: Time guest demand paging Andrew Jones
2020-02-14 14:59 ` [PATCH 06/13] KVM: selftests: Remove unnecessary defines Andrew Jones
2020-02-14 14:59 ` [PATCH 07/13] KVM: selftests: aarch64: Remove unnecessary ifdefs Andrew Jones
2020-02-14 14:59 ` [PATCH 08/13] KVM: selftests: aarch64: Use stream when given Andrew Jones
2020-02-14 14:59 ` [PATCH 09/13] KVM: selftests: Rework debug message printing Andrew Jones
2020-02-14 14:59 ` [PATCH 10/13] KVM: selftests: Convert some printf's to pr_info's Andrew Jones
2020-02-14 14:59 ` [PATCH 11/13] KVM: selftests: Rename vm_guest_mode_params Andrew Jones
2020-02-14 14:59 ` [PATCH 12/13] KVM: selftests: Introduce vm_guest_mode_params Andrew Jones
2020-02-14 14:59 ` [PATCH 13/13] KVM: selftests: Introduce num-pages conversion utilities Andrew Jones
2020-02-20 16:46 ` Paolo Bonzini
2020-02-14 15:23 ` [PATCH 00/13] KVM: selftests: Various fixes and cleanups Paolo Bonzini
2020-02-15 7:04 ` Andrew Jones
2020-02-14 22:26 ` Peter Xu
2020-02-15 7:07 ` Andrew Jones
2020-02-15 19:11 ` Peter Xu
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=20200214145920.30792-5-drjones@redhat.com \
--to=drjones@redhat.com \
--cc=bgardon@google.com \
--cc=borntraeger@de.ibm.com \
--cc=frankja@linux.ibm.com \
--cc=kvm@vger.kernel.org \
--cc=pbonzini@redhat.com \
--cc=peterx@redhat.com \
--cc=thuth@redhat.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.