X Tutup
Skip to content

Commit 36577e4

Browse files
committed
Issue python#15893: frozenmain.c now handles PyMem_Malloc() failure
1 parent 70c94e7 commit 36577e4

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

Modules/python.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,19 @@ wmain(int argc, wchar_t **argv)
1818
int
1919
main(int argc, char **argv)
2020
{
21-
wchar_t **argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*(argc+1));
21+
wchar_t **argv_copy;
2222
/* We need a second copy, as Python might modify the first one. */
23-
wchar_t **argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*(argc+1));
23+
wchar_t **argv_copy2;
2424
int i, res;
2525
char *oldloc;
26+
27+
argv_copy = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*(argc+1));
28+
argv_copy2 = (wchar_t **)PyMem_Malloc(sizeof(wchar_t*)*(argc+1));
29+
if (!argv_copy || !argv_copy2) {
30+
fprintf(stderr, "out of memory\n");
31+
return 1;
32+
}
33+
2634
/* 754 requires that FP exceptions run in "no stop" mode by default,
2735
* and until C vendors implement C99's ways to control FP exceptions,
2836
* Python requires non-stop mode. Alas, some platforms enable FP
@@ -34,10 +42,6 @@ main(int argc, char **argv)
3442
m = fpgetmask();
3543
fpsetmask(m & ~FP_X_OFL);
3644
#endif
37-
if (!argv_copy || !argv_copy2) {
38-
fprintf(stderr, "out of memory\n");
39-
return 1;
40-
}
4145
oldloc = strdup(setlocale(LC_ALL, NULL));
4246
setlocale(LC_ALL, "");
4347
for (i = 0; i < argc; i++) {

Python/frozenmain.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,16 @@ Py_FrozenMain(int argc, char **argv)
2020
int inspect = 0;
2121
int unbuffered = 0;
2222
char *oldloc;
23-
wchar_t **argv_copy = PyMem_Malloc(sizeof(wchar_t*)*argc);
23+
wchar_t **argv_copy;
2424
/* We need a second copies, as Python might modify the first one. */
25-
wchar_t **argv_copy2 = PyMem_Malloc(sizeof(wchar_t*)*argc);
25+
wchar_t **argv_copy2;
26+
27+
argv_copy = PyMem_Malloc(sizeof(wchar_t*)*argc);
28+
argv_copy2 = PyMem_Malloc(sizeof(wchar_t*)*argc);
29+
if (!argv_copy || !argv_copy2) {
30+
fprintf(stderr, "out of memory\n");
31+
return 1;
32+
}
2633

2734
Py_FrozenFlag = 1; /* Suppress errors from getpath.c */
2835

0 commit comments

Comments
 (0)
X Tutup