11[ ![ Build Status] ( https://travis-ci.org/JohnCGriffin/overflow.png )] ( https://travis-ci.org/JohnCGriffin/overflow )
22# overflow
3- Check for integer overflow in Golang arithmetic.
3+ Check for integer overflow in Golang arithmetic and type conversion .
44### Install
55```
66go get github.com/johncgriffin/overflow
@@ -13,6 +13,7 @@ go generate
1313```
1414### Synopsis
1515
16+ #### Arithmetic overflow detection
1617```
1718package main
1819
@@ -48,6 +49,34 @@ yields the output
4849
4950For (u)int types, provide (U)Add, (U)Sub, (U)Mul, (U)Div, (U)Quotient, etc.
5051
52+
53+ #### Type conversion overflow detection
54+ ```
55+ func main() {
56+ var i uint
57+ for i = math.MaxInt - 5; i <= math.MaxInt+5; i++ {
58+ ret, ok := overflow.UintToInt(i)
59+ fmt.Printf("%v -> (%v,%v)\n",
60+ i, ret, ok)
61+ }
62+ }
63+ ```
64+ yields the output
65+ ```
66+ 9223372036854775802 -> (9223372036854775802,true)
67+ 9223372036854775803 -> (9223372036854775803,true)
68+ 9223372036854775804 -> (9223372036854775804,true)
69+ 9223372036854775805 -> (9223372036854775805,true)
70+ 9223372036854775806 -> (9223372036854775806,true)
71+ 9223372036854775807 -> (9223372036854775807,true)
72+ 9223372036854775808 -> (-9223372036854775808,false)
73+ 9223372036854775809 -> (-9223372036854775807,false)
74+ 9223372036854775810 -> (-9223372036854775806,false)
75+ 9223372036854775811 -> (-9223372036854775805,false)
76+ 9223372036854775812 -> (-9223372036854775804,false)
77+ ```
78+ Provide UintToInt, IntToUint, Uint64ToInt32, Int32ToUint64, etc.
79+
5180### Stay calm and panic
5281
5382There's a good case to be made that a panic is an unidiomatic but proper response. Iff you
@@ -80,4 +109,3 @@ SOFTWARE.
80109
81110
82111
83-
0 commit comments