diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-21 16:19:39 +0100 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2024-12-21 16:19:39 +0100 |
| commit | b0d8a2f5f5718ba3fea628cad7bcbbc76c6a140c (patch) | |
| tree | 14fa46de9219f95858c1d7de04d1f2cb66db3e0b | |
| parent | 4d9b0b7cb5c749cd1db3828ab9a3475e6c9811b6 (diff) | |
| download | zmodn-b0d8a2f5f5718ba3fea628cad7bcbbc76c6a140c.tar.gz zmodn-b0d8a2f5f5718ba3fea628cad7bcbbc76c6a140c.zip | |
Fix possible overflow with assignment operations
| -rwxr-xr-x | test | 18 | ||||
| -rw-r--r-- | zmodn.h | 6 |
2 files changed, 21 insertions, 3 deletions
| @@ -115,6 +115,24 @@ public: | |||
| 115 | assert_equal(n, Zmod<12>(4)); | 115 | assert_equal(n, Zmod<12>(4)); |
| 116 | } | 116 | } |
| 117 | }, | 117 | }, |
| 118 | { | ||
| 119 | .name = "Multiplication overflow", | ||
| 120 | .f = []() { | ||
| 121 | Zmod<10> n = 8; | ||
| 122 | Zmod<10> m = 9; | ||
| 123 | auto prod = m * n; | ||
| 124 | assert_equal(prod.toint64(), 2); | ||
| 125 | } | ||
| 126 | }, | ||
| 127 | { | ||
| 128 | .name = "Multiplication and assignment overflow", | ||
| 129 | .f = []() { | ||
| 130 | Zmod<10> n = 8; | ||
| 131 | Zmod<10> m = 9; | ||
| 132 | n *= m; | ||
| 133 | assert_equal(n.toint64(), 2); | ||
| 134 | } | ||
| 135 | }, | ||
| 118 | }; | 136 | }; |
| 119 | 137 | ||
| 120 | int main() { | 138 | int main() { |
| @@ -17,9 +17,9 @@ public: | |||
| 17 | Zmod operator+(const Zmod& z) const { return int64 + z.int64; } | 17 | Zmod operator+(const Zmod& z) const { return int64 + z.int64; } |
| 18 | Zmod operator-(const Zmod& z) const { return int64 - z.int64; } | 18 | Zmod operator-(const Zmod& z) const { return int64 - z.int64; } |
| 19 | Zmod operator*(const Zmod& z) const { return int64 * z.int64; } | 19 | Zmod operator*(const Zmod& z) const { return int64 * z.int64; } |
| 20 | Zmod operator+=(const Zmod& z) { return int64 += z.int64; } | 20 | Zmod operator+=(const Zmod& z) { return (*this) = int64 + z.int64; } |
| 21 | Zmod operator-=(const Zmod& z) { return int64 -= z.int64; } | 21 | Zmod operator-=(const Zmod& z) { return (*this) = int64 - z.int64; } |
| 22 | Zmod operator*=(const Zmod& z) { return int64 *= z.int64; } | 22 | Zmod operator*=(const Zmod& z) { return (*this) = int64 * z.int64; } |
| 23 | 23 | ||
| 24 | bool operator==(const Zmod& z) const { return int64 == z.int64; } | 24 | bool operator==(const Zmod& z) const { return int64 == z.int64; } |
| 25 | bool operator!=(const Zmod& z) const { return int64 != z.int64; } | 25 | bool operator!=(const Zmod& z) const { return int64 != z.int64; } |
