From 10560afb540b783da568aebe83d0f782e46bb673 Mon Sep 17 00:00:00 2001 From: Alberto Donizetti Date: Thu, 20 Oct 2016 11:24:51 +0200 Subject: runtime/debug: avoid overflow in SetMaxThreads Fixes #16076 Change-Id: I91fa87b642592ee4604537dd8c3197cd61ec8b31 Reviewed-on: https://go-review.googlesource.com/31516 Run-TryBot: Ian Lance Taylor TryBot-Result: Gobot Gobot Reviewed-by: Ian Lance Taylor --- src/runtime/debug/garbage_test.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/runtime/debug/garbage_test.go') diff --git a/src/runtime/debug/garbage_test.go b/src/runtime/debug/garbage_test.go index d1fa7db23a..6b03455cf9 100644 --- a/src/runtime/debug/garbage_test.go +++ b/src/runtime/debug/garbage_test.go @@ -114,3 +114,16 @@ func TestSetGCPercent(t *testing.T) { t.Errorf("SetGCPercent(123); SetGCPercent(x) = %d, want 123", new) } } + +func TestSetMaxThreadsOvf(t *testing.T) { + // Verify that a big threads count will not overflow the int32 + // maxmcount variable, causing a panic (see Issue 16076). + // + // This can only happen when ints are 64 bits, since on platforms + // with 32 bit ints SetMaxThreads (which takes an int parameter) + // cannot be given anything that will overflow an int32. + // + // Call SetMaxThreads with 1<<31, but only on 64 bit systems. + nt := SetMaxThreads(1 << (30 + ^uint(0)>>63)) + SetMaxThreads(nt) // restore previous value +} -- cgit v1.3