mirror of
https://github.com/golang/go.git
synced 2025-05-19 06:14:40 +00:00
flag: validate Int and Uint values to be in range
Fixes #19230 Change-Id: I38df9732b88f0328506e74f1a46f52adf47db1e5 Reviewed-on: https://go-review.googlesource.com/38419 Reviewed-by: Robert Griesemer <gri@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
24dc8c6cb5
commit
c65ceff125
@ -114,7 +114,7 @@ func newIntValue(val int, p *int) *intValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *intValue) Set(s string) error {
|
func (i *intValue) Set(s string) error {
|
||||||
v, err := strconv.ParseInt(s, 0, 64)
|
v, err := strconv.ParseInt(s, 0, strconv.IntSize)
|
||||||
*i = intValue(v)
|
*i = intValue(v)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -150,7 +150,7 @@ func newUintValue(val uint, p *uint) *uintValue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *uintValue) Set(s string) error {
|
func (i *uintValue) Set(s string) error {
|
||||||
v, err := strconv.ParseUint(s, 0, 64)
|
v, err := strconv.ParseUint(s, 0, strconv.IntSize)
|
||||||
*i = uintValue(v)
|
*i = uintValue(v)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -415,3 +416,19 @@ func TestPrintDefaults(t *testing.T) {
|
|||||||
t.Errorf("got %q want %q\n", got, defaultOutput)
|
t.Errorf("got %q want %q\n", got, defaultOutput)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Issue 19230: validate range of Int and Uint flag values.
|
||||||
|
func TestIntFlagOverflow(t *testing.T) {
|
||||||
|
if strconv.IntSize != 32 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
ResetForTesting(nil)
|
||||||
|
Int("i", 0, "")
|
||||||
|
Uint("u", 0, "")
|
||||||
|
if err := Set("i", "2147483648"); err == nil {
|
||||||
|
t.Error("unexpected success setting Int")
|
||||||
|
}
|
||||||
|
if err := Set("u", "4294967296"); err == nil {
|
||||||
|
t.Error("unexpected success setting Uint")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user