Skip to content

Non-deterministic regex result between app launches #81427

Open
@lufinkey

Description

@lufinkey

Description

When trying to use regex to match against the output of a command, I found that it would randomly fail to match the output. After investigating, I found that it was due to the character class I was using to match newlines: [(?:\r\n)\n\r]. After writing a test case to loop my matching 1000 times to see if there would be a single failure, I found that if it failed the first time (after app launch), it would always fail, and if it succeeded the first time, it would always succeed. My guess is that this is due to how swift builds regex character classes under the hood. I'm not entirely familiar with it, but this seems to point to some sort of hashing being involved, which would explain the non-deterministic output between launches of the process. I was able to fix my regex by replacing my character class with (?:\r\n|\n|\r), which seems to work every time.

Reproduction

You'll need to run this code as a new process multiple times in order to see the issue, as it doesn't happen every time.

func wtfTest() {
	let output = """
	gdown @5.2.0 (net, python)
	Google Drive Public File Downloader when Curl/Wget Fails

	gwget @1.0.4_4 (gnome, net)
	Gwget is a Download Manager for Gnome 2. It uses wget as a backend.

	wget @1.25.0_1 (net, www)
	internet file retriever

	wget2 @2.2.0 (net, www)
	GNU Wget2 is the successor of GNU Wget, a file and recursive website downloader.

	wgetpro @0.1.3 (net, www)
	advanced internet file retriever

	wput @0.6.2_2 (net)
	wput is like wget but is for uploading files to ftp-servers

	zget @1.1.11 (net, www)
	zack's wget

	Found 7 ports.
	"""
	
	print(output)

	let regex = /(?m)^(\S+)[^\r\n\S]*(?:@(\S+))(?:[^\(\r\n]+\([^\r\n\S]*((?:(?:[^\r\n,]+),?)+)\))[^\r\n\S]*[(?:\r\n)\n\r][^\r\n\S]*([^\r\n]*)[(?:\r\n)\n\r]+/

	if output.firstMatch(of: regex) == nil {
		print("❌ Run didn't match")
		exit(1)
	}
	
	print("✅ Run matched correctly")
}

wtfTest()

Expected behavior

The same regular expression used on the same string should always give the same result.

Environment

swift-driver version: 1.120.5 Apple Swift version 6.1 (swiftlang-6.1.0.110.21 clang-1700.0.13.3)
Target: arm64-apple-macosx15.0

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugA deviation from expected or documented behavior. Also: expected but undesirable behavior.standard libraryArea: Standard library umbrella

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions