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

Add string.split function #500

Merged
merged 2 commits into from
Jun 18, 2023
Merged

Conversation

Lejo1
Copy link
Contributor

@Lejo1 Lejo1 commented Dec 15, 2019

Adds the default minetest string.split function.
I know it's just one helper function but it were also asked in #470 and should be pretty safe.
A player could anyway add it to his controller:

--  Copied from mt code: https://github.com/minetest/minetest/blob/0df646e0689b5324c7bdb13205c2a63e42ebe8e8/builtin/common/misc_helpers.lua#L169
function string.split(str, delim, include_empty, max_splits, sep_is_pattern)
	delim = delim or ","
	max_splits = max_splits or -2
	local items = {}
	local pos, len = 1, #str
	local plain = not sep_is_pattern
	max_splits = max_splits + 1
	repeat
		local np, npe = string.find(str, delim, pos, plain)
		np, npe = (np or (len+1)), (npe or (len+1))
		if (not np) or (max_splits == 1) then
			np = len + 1
			npe = np
		end
		local s = string.sub(str, pos, np - 1)
		if include_empty or (s ~= "") then
			max_splits = max_splits - 1
			items[#items + 1] = s
		end
		pos = npe + 1
	until (max_splits == 0) or (pos > (len + 1))
	return items
end

We could also set the max_splits to a specific value to be 100 % sure.

SmallJoker
SmallJoker previously approved these changes Dec 25, 2019
Copy link
Member

@SmallJoker SmallJoker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, why not?

@Lejo1
Copy link
Contributor Author

Lejo1 commented Dec 25, 2019

Just one side note:
We have a secure string.find function but string.split uses the normal one. Is that insecure?

I’m pretty unsure about this cause I don’t know why we need a secure find function

@SmallJoker
Copy link
Member

Right. string.split requires a custom safe implementation according to

-- string.find with a pattern can be used to DoS the server.
-- Therefore, limit string.find to patternless matching.
local function safe_string_find(...)
if (select(4, ...)) ~= true then
debug.sethook() -- Clear hook
error("string.find: 'plain' (fourth parameter) must always be true in a Luacontroller")

@Desour
Copy link
Contributor

Desour commented Dec 26, 2019

Just do the same as for string.find:

-- do not allow pattern matching in string.split (see string.find for details)
local function safe_string_split(...)
	if select(5, ...) then
		debug.sethook() -- Clear hook
		error("string.split: 'sep_is_pattern' (fifth parameter) may not be used in a Luacontroller")
	end

	return string.split(...)
end

I wonder what other helper functions it might be worth adding to the luacontroller.

@Lejo1
Copy link
Contributor Author

Lejo1 commented Jan 3, 2020

Just do the same as for string.find

Thanks, did that!

@Lejo1
Copy link
Contributor Author

Lejo1 commented Mar 30, 2020

Included in #507

@Lejo1 Lejo1 closed this Mar 30, 2020
@numberZero numberZero reopened this Jun 18, 2023
@numberZero numberZero merged commit fef402e into minetest-mods:master Jun 18, 2023
BuckarooBanzay added a commit to mt-mods/mooncontroller that referenced this pull request Jun 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants