* [PATCH kvm-unit-tests 0/2] Introduce strtoll/strtoull
@ 2021-10-13 16:42 Andrew Jones
2021-10-13 16:42 ` [PATCH kvm-unit-tests 1/2] compiler.h: Fix typos in mul and sub overflow checks Andrew Jones
` (2 more replies)
0 siblings, 3 replies; 6+ messages in thread
From: Andrew Jones @ 2021-10-13 16:42 UTC (permalink / raw)
To: kvm; +Cc: pbonzini, thuth, ahmeddan
A recent posting by Daniele Ahmed inspired me to write a patch adding
strtoll/strtoull. While doing that I noticed check_mul_overflow wasn't
working and found copy+paste errors with it and check_sub_overflow.
Andrew Jones (2):
compiler.h: Fix typos in mul and sub overflow checks
lib: Introduce strtoll/strtoull
lib/linux/compiler.h | 4 ++--
lib/stdlib.h | 2 ++
lib/string.c | 51 ++++++++++++++++++++++++++++++++------------
3 files changed, 41 insertions(+), 16 deletions(-)
--
2.31.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH kvm-unit-tests 1/2] compiler.h: Fix typos in mul and sub overflow checks
2021-10-13 16:42 [PATCH kvm-unit-tests 0/2] Introduce strtoll/strtoull Andrew Jones
@ 2021-10-13 16:42 ` Andrew Jones
2021-10-14 9:56 ` Ahmed, Daniele
2021-10-13 16:42 ` [PATCH kvm-unit-tests 2/2] lib: Introduce strtoll/strtoull Andrew Jones
2021-10-15 9:15 ` [PATCH kvm-unit-tests 0/2] " Paolo Bonzini
2 siblings, 1 reply; 6+ messages in thread
From: Andrew Jones @ 2021-10-13 16:42 UTC (permalink / raw)
To: kvm; +Cc: pbonzini, thuth, ahmeddan
Fixes: 4ceb02bf68f0 ("compiler: Add builtin overflow flag and predicate wrappers")
Signed-off-by: Andrew Jones <drjones@redhat.com>
---
lib/linux/compiler.h | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/lib/linux/compiler.h b/lib/linux/compiler.h
index c7fc0cf0852e..6f565e4a5107 100644
--- a/lib/linux/compiler.h
+++ b/lib/linux/compiler.h
@@ -33,8 +33,8 @@
#elif GCC_VERSION >= 70100
#define COMPILER_HAS_GENERIC_BUILTIN_OVERFLOW 1
#define check_add_overflow(a, b) __builtin_add_overflow_p(a, b, (typeof((a) + (b)))0)
-#define check_sub_overflow(a, b) __builtin_add_overflow_p(a, b, (typeof((a) - (b)))0)
-#define check_mul_overflow(a, b) __builtin_add_overflow_p(a, b, (typeof((a) * (b)))0)
+#define check_sub_overflow(a, b) __builtin_sub_overflow_p(a, b, (typeof((a) - (b)))0)
+#define check_mul_overflow(a, b) __builtin_mul_overflow_p(a, b, (typeof((a) * (b)))0)
#else
#define check_add_overflow(a, b) ({ (void)((int)(a) == (int)(b)); 0; })
#define check_sub_overflow(a, b) ({ (void)((int)(a) == (int)(b)); 0; })
--
2.31.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH kvm-unit-tests 2/2] lib: Introduce strtoll/strtoull
2021-10-13 16:42 [PATCH kvm-unit-tests 0/2] Introduce strtoll/strtoull Andrew Jones
2021-10-13 16:42 ` [PATCH kvm-unit-tests 1/2] compiler.h: Fix typos in mul and sub overflow checks Andrew Jones
@ 2021-10-13 16:42 ` Andrew Jones
2021-10-14 9:45 ` Ahmed, Daniele
2021-10-15 9:15 ` [PATCH kvm-unit-tests 0/2] " Paolo Bonzini
2 siblings, 1 reply; 6+ messages in thread
From: Andrew Jones @ 2021-10-13 16:42 UTC (permalink / raw)
To: kvm; +Cc: pbonzini, thuth, ahmeddan
Signed-off-by: Andrew Jones <drjones@redhat.com>
---
lib/stdlib.h | 2 ++
lib/string.c | 51 +++++++++++++++++++++++++++++++++++++--------------
2 files changed, 39 insertions(+), 14 deletions(-)
diff --git a/lib/stdlib.h b/lib/stdlib.h
index 33c00e8a5204..28496d7ae333 100644
--- a/lib/stdlib.h
+++ b/lib/stdlib.h
@@ -9,5 +9,7 @@
long int strtol(const char *nptr, char **endptr, int base);
unsigned long int strtoul(const char *nptr, char **endptr, int base);
+long long int strtoll(const char *nptr, char **endptr, int base);
+unsigned long long int strtoull(const char *nptr, char **endptr, int base);
#endif /* _STDLIB_H_ */
diff --git a/lib/string.c b/lib/string.c
index ffc7c7e4f855..27106dae0b0b 100644
--- a/lib/string.c
+++ b/lib/string.c
@@ -168,9 +168,10 @@ static int isspace(int c)
return c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == '\v' || c == '\f';
}
-static unsigned long __strtol(const char *nptr, char **endptr,
- int base, bool is_signed) {
- unsigned long acc = 0;
+static unsigned long long __strtoll(const char *nptr, char **endptr,
+ int base, bool is_signed,
+ bool is_longlong) {
+ unsigned long long ull = 0;
const char *s = nptr;
int neg, c;
@@ -210,36 +211,58 @@ static unsigned long __strtol(const char *nptr, char **endptr,
else
break;
- if (is_signed) {
- long sacc = (long)acc;
- assert(!check_mul_overflow(sacc, base));
- assert(!check_add_overflow(sacc * base, c));
+ if (!is_longlong) {
+ if (is_signed) {
+ long sl = (long)ull;
+ assert(!check_mul_overflow(sl, base));
+ assert(!check_add_overflow(sl * base, c));
+ } else {
+ unsigned long ul = (unsigned long)ull;
+ assert(!check_mul_overflow(ul, base));
+ assert(!check_add_overflow(ul * base, c));
+ }
} else {
- assert(!check_mul_overflow(acc, base));
- assert(!check_add_overflow(acc * base, c));
+ if (is_signed) {
+ long long sll = (long long)ull;
+ assert(!check_mul_overflow(sll, base));
+ assert(!check_add_overflow(sll * base, c));
+ } else {
+ assert(!check_mul_overflow(ull, base));
+ assert(!check_add_overflow(ull * base, c));
+ }
}
- acc = acc * base + c;
+ ull = ull * base + c;
s++;
}
if (neg)
- acc = -acc;
+ ull = -ull;
if (endptr)
*endptr = (char *)s;
- return acc;
+ return ull;
}
long int strtol(const char *nptr, char **endptr, int base)
{
- return __strtol(nptr, endptr, base, true);
+ return __strtoll(nptr, endptr, base, true, false);
}
unsigned long int strtoul(const char *nptr, char **endptr, int base)
{
- return __strtol(nptr, endptr, base, false);
+ return __strtoll(nptr, endptr, base, false, false);
+}
+
+long long int strtoll(const char *nptr, char **endptr, int base)
+{
+ return __strtoll(nptr, endptr, base, true, true);
+}
+
+unsigned long long int strtoull(const char *nptr, char **endptr, int base)
+{
+ return __strtoll(nptr, endptr, base, false, true);
}
long atol(const char *ptr)
--
2.31.1
^ permalink raw reply related [flat|nested] 6+ messages in thread
* Re: [PATCH kvm-unit-tests 2/2] lib: Introduce strtoll/strtoull
2021-10-13 16:42 ` [PATCH kvm-unit-tests 2/2] lib: Introduce strtoll/strtoull Andrew Jones
@ 2021-10-14 9:45 ` Ahmed, Daniele
0 siblings, 0 replies; 6+ messages in thread
From: Ahmed, Daniele @ 2021-10-14 9:45 UTC (permalink / raw)
To: Andrew Jones, kvm; +Cc: pbonzini, thuth
Reviewed-by: Daniele Ahmed <ahmeddan@amazon.com>
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH kvm-unit-tests 1/2] compiler.h: Fix typos in mul and sub overflow checks
2021-10-13 16:42 ` [PATCH kvm-unit-tests 1/2] compiler.h: Fix typos in mul and sub overflow checks Andrew Jones
@ 2021-10-14 9:56 ` Ahmed, Daniele
0 siblings, 0 replies; 6+ messages in thread
From: Ahmed, Daniele @ 2021-10-14 9:56 UTC (permalink / raw)
To: Andrew Jones, kvm; +Cc: pbonzini, thuth
Reviewed-by: Daniele Ahmed <ahmeddan@amazon.com>
Amazon Development Center Germany GmbH
Krausenstr. 38
10117 Berlin
Geschaeftsfuehrung: Christian Schlaeger, Jonathan Weiss
Eingetragen am Amtsgericht Charlottenburg unter HRB 149173 B
Sitz: Berlin
Ust-ID: DE 289 237 879
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH kvm-unit-tests 0/2] Introduce strtoll/strtoull
2021-10-13 16:42 [PATCH kvm-unit-tests 0/2] Introduce strtoll/strtoull Andrew Jones
2021-10-13 16:42 ` [PATCH kvm-unit-tests 1/2] compiler.h: Fix typos in mul and sub overflow checks Andrew Jones
2021-10-13 16:42 ` [PATCH kvm-unit-tests 2/2] lib: Introduce strtoll/strtoull Andrew Jones
@ 2021-10-15 9:15 ` Paolo Bonzini
2 siblings, 0 replies; 6+ messages in thread
From: Paolo Bonzini @ 2021-10-15 9:15 UTC (permalink / raw)
To: Andrew Jones, kvm; +Cc: thuth, ahmeddan
On 13/10/21 18:42, Andrew Jones wrote:
> A recent posting by Daniele Ahmed inspired me to write a patch adding
> strtoll/strtoull. While doing that I noticed check_mul_overflow wasn't
> working and found copy+paste errors with it and check_sub_overflow.
>
> Andrew Jones (2):
> compiler.h: Fix typos in mul and sub overflow checks
> lib: Introduce strtoll/strtoull
>
> lib/linux/compiler.h | 4 ++--
> lib/stdlib.h | 2 ++
> lib/string.c | 51 ++++++++++++++++++++++++++++++++------------
> 3 files changed, 41 insertions(+), 16 deletions(-)
>
Queued, thanks.
Paolo
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2021-10-15 9:15 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-13 16:42 [PATCH kvm-unit-tests 0/2] Introduce strtoll/strtoull Andrew Jones
2021-10-13 16:42 ` [PATCH kvm-unit-tests 1/2] compiler.h: Fix typos in mul and sub overflow checks Andrew Jones
2021-10-14 9:56 ` Ahmed, Daniele
2021-10-13 16:42 ` [PATCH kvm-unit-tests 2/2] lib: Introduce strtoll/strtoull Andrew Jones
2021-10-14 9:45 ` Ahmed, Daniele
2021-10-15 9:15 ` [PATCH kvm-unit-tests 0/2] " Paolo Bonzini
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.