Skip to content

Commit 42b51bb

Browse files
committed
Fixes and improvements suggested by reviewer
1 parent 13c57b7 commit 42b51bb

File tree

4 files changed

+28
-26
lines changed

4 files changed

+28
-26
lines changed

src/manage/aliasutils.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ def replace(self, **kwargs):
6666
@property
6767
def script_code(self):
6868
if self.mod and self.func:
69+
if not self.mod.isidentifier():
70+
LOGGER.warn("Alias %s has an entrypoint with invalid module "
71+
"%r.", self.name, self.mod)
72+
return None
73+
if not self.func.isidentifier():
74+
LOGGER.warn("Alias %s has an entrypoint with invalid function "
75+
"%r.", self.name, self.func)
76+
return None
6977
return SCRIPT_CODE.format(mod=self.mod, func=self.func)
7078

7179

@@ -183,7 +191,8 @@ def _create_alias(cmd, *, name, target, plat=None, windowed=0, script_code=None,
183191
except OSError:
184192
LOGGER.error("Failed to clean up existing alias. Re-run with -v "
185193
"or check the install log for details.")
186-
LOGGER.info("Failed to remove %s.", p_script, exc_info=True)
194+
LOGGER.info("Failed to remove %s.", p_script)
195+
LOGGER.debug("TRACEBACK", exc_info=True)
187196

188197

189198
def _parse_entrypoint_line(line):

src/pymanager/_launch.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,35 +47,35 @@ launch(
4747
STARTUPINFOW si;
4848
PROCESS_INFORMATION pi;
4949
int lastError = 0;
50-
const wchar_t *cmdLine = NULL;
50+
const wchar_t *cmd_line = NULL;
5151

5252
if (orig_cmd_line[0] == L'"') {
53-
cmdLine = wcschr(orig_cmd_line + 1, L'"');
53+
cmd_line = wcschr(orig_cmd_line + 1, L'"');
5454
} else {
55-
cmdLine = wcschr(orig_cmd_line, L' ');
55+
cmd_line = wcschr(orig_cmd_line, L' ');
5656
}
5757

58-
size_t n = wcslen(executable) + wcslen(orig_cmd_line) + wcslen(insert_args) + 6;
59-
wchar_t *newCmdLine = (wchar_t *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, n * sizeof(wchar_t));
60-
if (!newCmdLine) {
58+
size_t n = wcslen(executable) + wcslen(orig_cmd_line) + (insert_args ? wcslen(insert_args) : 0) + 6;
59+
wchar_t *new_cmd_line = (wchar_t *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, n * sizeof(wchar_t));
60+
if (!new_cmd_line) {
6161
lastError = GetLastError();
6262
goto exit;
6363
}
6464

6565
// Skip any requested args, deliberately leaving any trailing spaces
66-
// (we'll skip one later one and add our own space, and preserve multiple)
67-
while (skip_argc-- > 0) {
66+
// (we'll skip one later on and add our own space, and preserve multiple)
67+
while (cmd_line && *cmd_line && skip_argc-- > 0) {
6868
wchar_t c;
69-
while (*++cmdLine && *cmdLine == L' ') { }
70-
while (*++cmdLine && *cmdLine != L' ') { }
69+
while (*++cmd_line && *cmd_line == L' ') { }
70+
while (*++cmd_line && *cmd_line != L' ') { }
7171
}
7272

73-
swprintf_s(newCmdLine, n, L"\"%s\"%s%s%s%s",
73+
swprintf_s(new_cmd_line, n, L"\"%s\"%s%s%s%s",
7474
executable,
7575
(insert_args && *insert_args) ? L" ": L"",
7676
(insert_args && *insert_args) ? insert_args : L"",
77-
(cmdLine && *cmdLine) ? L" " : L"",
78-
(cmdLine && *cmdLine) ? cmdLine + 1 : L"");
77+
(cmd_line && *cmd_line) ? L" " : L"",
78+
(cmd_line && *cmd_line) ? cmd_line + 1 : L"");
7979

8080
#if defined(_WINDOWS)
8181
/*
@@ -122,7 +122,7 @@ launch(
122122
}
123123

124124
si.dwFlags |= STARTF_USESTDHANDLES;
125-
if (!CreateProcessW(executable, newCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
125+
if (!CreateProcessW(executable, new_cmd_line, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi)) {
126126
lastError = GetLastError();
127127
goto exit;
128128
}
@@ -134,8 +134,8 @@ launch(
134134
lastError = GetLastError();
135135
}
136136
exit:
137-
if (newCmdLine) {
138-
HeapFree(GetProcessHeap(), 0, newCmdLine);
137+
if (new_cmd_line) {
138+
HeapFree(GetProcessHeap(), 0, new_cmd_line);
139139
}
140140
return lastError ? HRESULT_FROM_WIN32(lastError) : 0;
141141
}

src/pymanager/launcher.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ get_script(wchar_t **result_path)
172172
}
173173
}
174174

175-
wcscpy_s(&path[len], path_len, SUFFIX);
175+
wcscpy_s(&path[len], path_len - len, SUFFIX);
176176

177177
// Check that we have a script file. FindFirstFile should be fastest.
178178
WIN32_FIND_DATAW fd;
@@ -267,7 +267,7 @@ wmain(int argc, wchar_t **argv)
267267
{
268268
int exit_code;
269269
wchar_t executable[MAXLEN];
270-
wchar_t *script;
270+
wchar_t *script = NULL;
271271

272272
int err = get_executable(executable, MAXLEN);
273273
if (err) {

tests/test_alias.py

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import json
2-
import os
31
import pytest
42
import secrets
5-
from pathlib import Path, PurePath
63

74
from manage import aliasutils as AU
85
from manage.exceptions import NoLauncherTemplateError
@@ -298,10 +295,6 @@ def test_cleanup_aliases(fake_config, tmp_path):
298295
target = tmp_path / "target.exe"
299296
target.write_bytes(b"")
300297

301-
created = []
302-
def _on_create(cmd, **kwargs):
303-
created.append(kwargs)
304-
305298
aliases = [
306299
AU.AliasInfo(install=dict(prefix=tmp_path), name="A", target=target),
307300
AU.AliasInfo(install=dict(prefix=tmp_path), name="B.exe", target=target),

0 commit comments

Comments
 (0)