问题
The following code crashes when using GHC on Windows. It works perfectly on Linux.
Does this make any sense or is there a bug?
module Main where
import qualified Text.Regex as Re -- from regex-compat
import Debug.Trace
main :: IO ()
main = do
putStr $ cleanWord "jan"
putStr $ cleanWord "dec"
putStr $ cleanWord "jun" -- crashes here
cleanWord :: String -> String
cleanWord word_ =
let word = trace (show word_) word_ in
let re = Re.mkRegexWithOpts "(jun|jul|aug|sep|oct|nov|dec)" True False in
Re.subRegex re word ""
Some additional details
- I'm building with
stack
- It crashes in both GHCi and running the compiled executable
- I tried to enable profiling but can't seem to figure out how to get that to work correctly.
回答1:
Shuffling the regex around avoids the bug in this case.
This works fine for me now
let re = Re.mkRegexWithOpts "(jul|aug|sep|oct|jun|nov|dec)" True False in
I.e. moving the jun
towards the end of the regex. Not particularly satisfactory but it works and it means I can continue.
@Zeta's comment suggest that this is a bug with the underlying library
It's a bug, probably in regex-posix, due to its difference in the inclusion of the underlying BSD regex library from 1994. In sslow, an invalid memory address (-1(%rdx) with rdx = 0) will be accessed.
来源:https://stackoverflow.com/questions/39047317/simple-regular-expression-substitution-crashes-on-windows-using-regex-compat