[lxc-devel] [lxd/master] units: handle multiplication integer overflow
brauner on Github
lxc-bot at linuxcontainers.org
Tue Jul 7 14:57:24 UTC 2020
A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 380 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20200707/cdbfe769/attachment.bin>
-------------- next part --------------
From f332293942c0509af04e1cfe494de84b4c2545a3 Mon Sep 17 00:00:00 2001
From: Christian Brauner <christian.brauner at ubuntu.com>
Date: Tue, 7 Jul 2020 16:46:32 +0200
Subject: [PATCH] units: handle multiplication integer overflow
Closes: #7627.
Signed-off-by: Christian Brauner <christian.brauner at ubuntu.com>
---
shared/units/units.go | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/shared/units/units.go b/shared/units/units.go
index 23514c9c30..edc18c486b 100644
--- a/shared/units/units.go
+++ b/shared/units/units.go
@@ -5,6 +5,19 @@ import (
"strconv"
)
+func handleOverflow(val int64, mult int64) (int64, error) {
+ result := val * mult
+ if val == 0 || mult == 0 || val == 1 || mult == 1 {
+ return result, nil
+ }
+
+ if val != 0 && (result/val) != mult {
+ return 0, fmt.Errorf("Overflow multiplying %d with %d", val, mult)
+ }
+
+ return result, nil
+}
+
// ParseByteSizeString parses a human representation of an amount of
// data into a number of bytes
func ParseByteSizeString(input string) (int64, error) {
@@ -70,7 +83,7 @@ func ParseByteSizeString(input string) (int64, error) {
return -1, fmt.Errorf("Invalid value: %s", input)
}
- return valueInt * multiplicator, nil
+ return handleOverflow(valueInt, multiplicator)
}
// ParseBitSizeString parses a human representation of an amount of
@@ -139,7 +152,7 @@ func ParseBitSizeString(input string) (int64, error) {
return -1, fmt.Errorf("Unsupported suffix: %s", suffix)
}
- return valueInt * multiplicator, nil
+ return handleOverflow(valueInt, multiplicator)
}
// GetByteSizeString takes a number of bytes and precision and returns a
More information about the lxc-devel
mailing list