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

feat(process): allow commands and envs on proces_start #1477

Merged
merged 10 commits into from
Nov 30, 2023

Conversation

takase1121
Copy link
Member

This would allow something like this:

local cmd = { "bash", "-c", "echo $#", "--" }
for i = 1, 1000 do
  table.insert(cmd, "wow" .. i)
end
process.start(cmd, { stdout = process.REDIRECT_PARENT })

To run correctly.

@takase1121
Copy link
Member Author

Currently, supplying any commands more than 156 causes stack corruption due to Lua freeing the pointer to the string.
With enough luck, you might get to run the program, but it will cause a double free error.

Refactors the code to use an arglist type which is just lpCmdline on Windows
and a list in Linux.
The function automatically escapes the command when it is needed, avoiding
a second copy.

This also allows UTF-8 commands btw.
@takase1121
Copy link
Member Author

With the latest commit, the parent's environment variables are passed to the child process
when the env table is specified. This matches POSIX's behavior.

@adamharrison
Copy link
Member

Will test this on windows in the near future, but otherwise, even if it's a bit longer/larger than I would hope for, looks good.

@adamharrison
Copy link
Member

On windows, running the following code causes an issue: cannot add environment variable: unknown error (-1) in the lite-xl error log.

local p1 = process.start({ "./a.exe" }, { env = { TEST = "Keaops" } })
while p1:running() do
  local output = p1:read_stdout()
  if output ~= "" and output ~= nil then
    print("OUTPUT", output)
  end
end
print(p1:returncode())

It didn't before; runs fine on the original code.

@takase1121
Copy link
Member Author

On windows, running the following code causes an issue: cannot add environment variable: unknown error (-1) in the lite-xl error log.

local p1 = process.start({ "./a.exe" }, { env = { TEST = "Keaops" } })
while p1:running() do
  local output = p1:read_stdout()
  if output ~= "" and output ~= nil then
    print("OUTPUT", output)
  end
end
print(p1:returncode())

It didn't before; runs fine on the original code.

I cannot replicate this issue; but I believe I found the cause and committed a fix. Please test it.

@adamharrison
Copy link
Member

Yes, this now runs. Confirmed. Will do some more testing, and if it all looks good, will merge.

@adamharrison
Copy link
Member

OK, reviewed code a bit more; bit weird about the allocations being very different on Windows vs. everywhere else, but Windows is windows, and I seem to remember something about needing contiguous blocks or whatever; so I'm sure that's fine. Merging.

@adamharrison adamharrison merged commit 142f0a1 into lite-xl:master Nov 30, 2023
10 checks passed
@takase1121
Copy link
Member Author

OK, reviewed code a bit more; bit weird about the allocations being very different on Windows vs. everywhere else, but Windows is windows, and I seem to remember something about needing contiguous blocks or whatever; so I'm sure that's fine. Merging.

Yeah, Windows environment block expects a long "string" of each environment variables delimited by NUL.

takase1121 added a commit to takase1121/lite-xl that referenced this pull request Dec 1, 2023
* feat(process): allow commands and envs on proces_start

* refactor(process): copy process arguments once whenever possible

Refactors the code to use an arglist type which is just lpCmdline on Windows
and a list in Linux.
The function automatically escapes the command when it is needed, avoiding
a second copy.

This also allows UTF-8 commands btw.

* fix(process): fix invalid dereference

* refactor(process): mark xstrdup as potentially unused

* feat(process): add parent process environment when launching process

* fix(process): fix operator precedence with array operators

* fix(process): fix segfault when freeing random memory

* fix(process): fix wrong check for setenv()

* fix(process): fix accidentally initializing an array by assignment

* fix(process): clear return value if success
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants