base: Validate input in setNumCapabilities
authorBen Gamari <bgamari.foss@gmail.com>
Fri, 16 Jun 2017 19:18:48 +0000 (15:18 -0400)
committerBen Gamari <ben@smart-cactus.org>
Fri, 16 Jun 2017 19:18:49 +0000 (15:18 -0400)
Test Plan: validate

Reviewers: austin, hvr, erikd, simonmar

Subscribers: rwbarton, thomie

GHC Trac Issues: #13832

Differential Revision: https://phabricator.haskell.org/D3652

libraries/base/GHC/Conc/Sync.hs
testsuite/tests/rts/T13832.hs [new file with mode: 0644]
testsuite/tests/rts/T13832.stderr [new file with mode: 0644]
testsuite/tests/rts/all.T

index 78a0334..44d34d8 100644 (file)
@@ -379,7 +379,9 @@ to avoid contention with other processes in the machine.
 @since 4.5.0.0
 -}
 setNumCapabilities :: Int -> IO ()
-setNumCapabilities i = c_setNumCapabilities (fromIntegral i)
+setNumCapabilities i
+  | i <= 0    = fail $ "setNumCapabilities: Capability count ("++show i++") must be positive"
+  | otherwise = c_setNumCapabilities (fromIntegral i)
 
 foreign import ccall safe "setNumCapabilities"
   c_setNumCapabilities :: CUInt -> IO ()
diff --git a/testsuite/tests/rts/T13832.hs b/testsuite/tests/rts/T13832.hs
new file mode 100644 (file)
index 0000000..47d9ed2
--- /dev/null
@@ -0,0 +1,4 @@
+import GHC.Conc
+
+main :: IO ()
+main = setNumCapabilities 0
diff --git a/testsuite/tests/rts/T13832.stderr b/testsuite/tests/rts/T13832.stderr
new file mode 100644 (file)
index 0000000..7a552ca
--- /dev/null
@@ -0,0 +1 @@
+T13832: user error (setNumCapabilities: Capability count (0) must be positive)
index fc7363f..f32a35b 100644 (file)
@@ -372,4 +372,4 @@ test('T12497', [ unless(opsys('mingw32'), skip)
                run_command, ['$MAKE -s --no-print-directory T12497'])
 
 test('T12903', [when(opsys('mingw32'), skip)], compile_and_run, [''])
-
+test('T13832', exit_code(1), compile_and_run, ['-threaded'])