1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
From b6089f11bd7534c9aa38b875ef1e830fc3b898b4 Mon Sep 17 00:00:00 2001
From: Jeremy Tan <jtanx@outlook.com>
Date: Wed, 13 Sep 2017 17:22:18 +0800
Subject: [PATCH] python: Use PyMem_Free wherever PyArg_ParseTuple+es is used
https://docs.python.org/2.0/ext/parseTuple.html
---
fontforge/python.c | 62 +++++++++++++++++++++++++++-------------------
1 file changed, 38 insertions(+), 27 deletions(-)
diff --git a/fontforge/python.c b/fontforge/python.c
index 5d6820c40..c6d7aca2b 100644
--- a/fontforge/python.c
+++ b/fontforge/python.c
@@ -607,7 +607,13 @@ return( NULL );
}
}
- if ( !SetPrefs((char *) prefname,&val,NULL)) {
+ bool succeeded = SetPrefs((char *) prefname,&val,NULL);
+ if (val.type == v_str && val.u.sval) {
+ PyMem_Free(val.u.sval);
+ val.u.sval = NULL;
+ }
+
+ if (!succeeded) {
PyErr_Format(PyExc_NameError, "Unknown preference item in SetPrefs: %s", prefname );
return( NULL );
}
@@ -728,7 +734,7 @@ static PyObject *PyFF_UnicodeFromName(PyObject *UNUSED(self), PyObject *args) {
return( NULL );
ret = Py_BuildValue("i", UniFromName((char *) name, ui_none,&custom));
- free(name);
+ PyMem_Free(name);
return( ret );
}
@@ -1361,12 +1367,14 @@ Py_RETURN_NONE; /* Well, that was pointless */
if ( import==Py_None )
import=NULL;
else if ( !PyCallable_Check(import) ) {
+ PyMem_Free(name);
PyErr_Format(PyExc_TypeError, "First argument is not callable" );
return( NULL );
}
if ( export==Py_None )
export=NULL;
else if ( !PyCallable_Check(export) ) {
+ PyMem_Free(name);
PyErr_Format(PyExc_TypeError, "Second argument is not callable" );
return( NULL );
}
@@ -1472,7 +1480,7 @@ static PyObject *PyFF_logError(PyObject *UNUSED(self), PyObject *args) {
if ( !PyArg_ParseTuple(args,"es","UTF-8", &msg) )
return( NULL );
LogError(msg);
- free(msg);
+ PyMem_Free(msg);
Py_RETURN_NONE;
}
@@ -1486,6 +1494,8 @@ static PyObject *PyFF_postError(PyObject *UNUSED(self), PyObject *args) {
return( NULL );
if( showPythonErrors )
ff_post_error(title,msg); /* Prints to stderr if no ui */
+ PyMem_Free(title);
+ PyMem_Free(msg);
Py_RETURN_NONE;
}
@@ -1494,6 +1504,8 @@ static PyObject *PyFF_postNotice(PyObject *UNUSED(self), PyObject *args) {
if ( !PyArg_ParseTuple(args,"eses","UTF-8", &title, "UTF-8", &msg) )
return( NULL );
ff_post_notice(title,msg); /* Prints to stderr if no ui */
+ PyMem_Free(title);
+ PyMem_Free(msg);
Py_RETURN_NONE;
}
@@ -1511,8 +1523,8 @@ return( NULL );
return( NULL );
ret = ff_open_filename(title,def,filter);
- free(title);
- free(def);
+ PyMem_Free(title);
+ PyMem_Free(def);
if ( ret==NULL )
Py_RETURN_NONE;
reto = DECODE_UTF8(ret,strlen(ret),NULL);
@@ -1534,8 +1546,8 @@ return( NULL );
return( NULL );
ret = ff_save_filename(title,def,filter);
- free(title);
- free(def);
+ PyMem_Free(title);
+ PyMem_Free(def);
if ( ret==NULL )
Py_RETURN_NONE;
reto = DECODE_UTF8(ret,strlen(ret),NULL);
@@ -1558,8 +1570,8 @@ return( NULL );
return( NULL );
if ( !PySequence_Check(answero) || STRING_CHECK(answero)) {
PyErr_Format(PyExc_TypeError, "Expected a tuple of strings for the third argument");
- free(title);
- free(quest);
+ PyMem_Free(title);
+ PyMem_Free(quest);
return( NULL );
}
cnt = PySequence_Size(answero);
@@ -1568,16 +1580,16 @@ return( NULL );
cancel = cnt-1;
if ( cancel<0 || cancel>=cnt || def<0 || def>=cnt ) {
PyErr_Format(PyExc_ValueError, "Value out of bounds for 4th or 5th argument");
- free(title);
- free(quest);
+ PyMem_Free(title);
+ PyMem_Free(quest);
free(answers);
return( NULL );
}
for ( i=0; i<cnt; ++i ) {
PyObject *utf8_name = PYBYTES_UTF8(PySequence_GetItem(answero,i));
if ( utf8_name==NULL ) {
- free(title);
- free(quest);
+ PyMem_Free(title);
+ PyMem_Free(quest);
FreeStringArray( i, answers );
return( NULL );
}
@@ -1587,8 +1599,8 @@ return( NULL );
answers[cnt] = NULL;
ret = ff_ask(title,(const char **) answers,def,cancel,quest);
- free(title);
- free(quest);
+ PyMem_Free(title);
+ PyMem_Free(quest);
FreeStringArray( cnt, answers );
return( Py_BuildValue("i",ret));
}
@@ -1608,23 +1620,23 @@ return( NULL );
return( NULL );
if ( !PySequence_Check(answero) || STRING_CHECK(answero)) {
PyErr_Format(PyExc_TypeError, "Expected a tuple of strings for the third argument");
- free(title);
- free(quest);
+ PyMem_Free(title);
+ PyMem_Free(quest);
return( NULL );
}
cnt = PySequence_Size(answero);
answers = calloc(cnt+1, sizeof(char *));
if ( def<0 || def>=cnt ) {
PyErr_Format(PyExc_ValueError, "Value out of bounds for 4th argument");
- free(title);
- free(quest);
+ PyMem_Free(title);
+ PyMem_Free(quest);
return( NULL );
}
for ( i=0; i<cnt; ++i ) {
PyObject *utf8_name = PYBYTES_UTF8(PySequence_GetItem(answero,i));
if ( utf8_name==NULL ) {
- free(title);
- free(quest);
+ PyMem_Free(title);
+ PyMem_Free(quest);
FreeStringArray( i, answers );
return( NULL );
}
@@ -1634,8 +1646,8 @@ return( NULL );
answers[cnt] = NULL;
ret = ff_choose(title,(const char **) answers,cnt,def,quest);
- free(title);
- free(quest);
+ PyMem_Free(title);
+ PyMem_Free(quest);
FreeStringArray( cnt, answers );
return( Py_BuildValue("i",ret));
}
@@ -1654,8 +1666,8 @@ return( NULL );
return( NULL );
ret = ff_ask_string(title,def,quest);
- free(title);
- free(quest);
+ PyMem_Free(title);
+ PyMem_Free(quest);
free(def);
if ( ret==NULL )
Py_RETURN_NONE;
|