@@ -51,7 +51,8 @@ func (z *Rat) Scan(s fmt.ScanState, ch rune) error {
51
51
// An optional base-10 ``e'' or base-2 ``p'' (or their upper-case variants)
52
52
// exponent may be provided as well, except for hexadecimal floats which
53
53
// only accept an (optional) ``p'' exponent (because an ``e'' or ``E'' cannot
54
- // be distinguished from a mantissa digit).
54
+ // be distinguished from a mantissa digit). If the exponent's absolute value
55
+ // is too large, the operation may fail.
55
56
// The entire string, not just a prefix, must be valid for success. If the
56
57
// operation failed, the value of z is undefined but the returned value is nil.
57
58
func (z * Rat ) SetString (s string ) (* Rat , bool ) {
@@ -169,6 +170,9 @@ func (z *Rat) SetString(s string) (*Rat, bool) {
169
170
if n < 0 {
170
171
n = - n
171
172
}
173
+ if n > 1e6 {
174
+ return nil , false // avoid excessively large exponents
175
+ }
172
176
pow5 := z .b .abs .expNN (natFive , nat (nil ).setWord (Word (n )), nil ) // use underlying array of z.b.abs
173
177
if exp5 > 0 {
174
178
z .a .abs = z .a .abs .mul (z .a .abs , pow5 )
@@ -181,15 +185,12 @@ func (z *Rat) SetString(s string) (*Rat, bool) {
181
185
}
182
186
183
187
// apply exp2 contributions
188
+ if exp2 < - 1e7 || exp2 > 1e7 {
189
+ return nil , false // avoid excessively large exponents
190
+ }
184
191
if exp2 > 0 {
185
- if int64 (uint (exp2 )) != exp2 {
186
- panic ("exponent too large" )
187
- }
188
192
z .a .abs = z .a .abs .shl (z .a .abs , uint (exp2 ))
189
193
} else if exp2 < 0 {
190
- if int64 (uint (- exp2 )) != - exp2 {
191
- panic ("exponent too large" )
192
- }
193
194
z .b .abs = z .b .abs .shl (z .b .abs , uint (- exp2 ))
194
195
}
195
196
0 commit comments