Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix String.foldr bug that causes infinite loop #1137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

arowM
Copy link

@arowM arowM commented Apr 30, 2023

String.all, String.any, and functions using String.foldr have a bug that can cause infinite loop under certain conditions.

SSCCE

-- `String.toList` is implemented with `String.foldr`
String.right 1 "foobar😈" |> String.toList
--> (causes infinite loop)
-- The following code, which determines whether the last character of a string is a space, is not uncommon:
String.right 1 "foobar😈" |> String.any (\c -> c == ' ')
--> (causes infinite loop)
String.right 1 "foobar😈" |> String.all (\c -> c /= ' ')
--> (causes infinite loop)

Cause of the bug

Functions such as foldr do not expect that only part of a surrogate pair is given as an argument.

var _String_foldr = F3(function(func, state, string)
{
	var i = string.length;
	while (i--)
	{
		var char = string[i];
		var word = string.charCodeAt(i);
		if (0xDC00 <= word && word <= 0xDFFF)
		{
			// This is harmful.
			i--;
			char = string[i] + char;
		}
		state = A2(func, __Utils_chr(char), state);
	}
	return state;
});

Hence, i in the above code jumps over 0 and becomes a negative value, resulting in an infinite loop.

@github-actions
Copy link

Thanks for suggesting these code changes. To set expectations:

  • Pull requests are reviewed in batches, so it can take some time to get a response.
  • Smaller pull requests are easier to review. To fix nine typos, nine specific issues will always go faster than one big one. Learn why here.
  • Reviewers may not know as much as you about certain situations, so add links to supporting evidence for important claims, especially regarding standards for CSS, HTTP, URI, etc.

Finally, please be patient with the core team. They are trying their best with limited resources.

arowM added a commit to arowM/elm-markdown-ast that referenced this pull request Apr 30, 2023
marc136 pushed a commit to elm-janitor/core that referenced this pull request Nov 23, 2023
Fix bug that causes infinite loop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant