From 60f42ea61cb7e1de8d54432d8fb9ab028b8a575d Mon Sep 17 00:00:00 2001 From: Ben Hoyt Date: Fri, 14 Aug 2020 22:58:49 +1200 Subject: strconv: fix incorrect bit size in ParseComplex; add tests In ParseComplex, the "size" passed to parseFloatPrefix should be 64 for complex128, not 128. It still works because of how parseFloatPrefix is forgiving about the size if it's not 32, but worth fixing anyway. Make ParseComplex and ParseFloat return a bit size error for anything other than 128 or 64 (for ParseComplex), or 64 or 32 (for ParseFloat). Add "InvalidBitSize" tests for these cases. Add tests for ParseComplex with bitSize==64: this is done in a similar way to how the ParseFloat 32-bit tests work, re-using the tests for the larger bit size. Add tests for FormatComplex -- there were none before. Fixes #40706 Change-Id: I16ddd546e5237207cc3b8c2181dd708eca42b04f Reviewed-on: https://go-review.googlesource.com/c/go/+/248219 Run-TryBot: Robert Griesemer TryBot-Result: Go Bot Trust: Minux Ma Trust: Robert Griesemer Reviewed-by: Robert Griesemer --- src/strconv/ftoa_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'src/strconv/ftoa_test.go') diff --git a/src/strconv/ftoa_test.go b/src/strconv/ftoa_test.go index 755c986b86..99cca17542 100644 --- a/src/strconv/ftoa_test.go +++ b/src/strconv/ftoa_test.go @@ -212,6 +212,15 @@ func TestFtoaRandom(t *testing.T) { } } +func TestFormatFloatInvalidBitSize(t *testing.T) { + defer func() { + if r := recover(); r == nil { + t.Fatalf("expected panic due to invalid bitSize") + } + }() + _ = FormatFloat(3.14, 'g', -1, 100) +} + var ftoaBenches = []struct { name string float float64 -- cgit v1.3