@@ -107,7 +107,7 @@ struct HandlerInfo {
107
107
static struct HandlerInfo handler_info [64 ];
108
108
109
109
static int
110
- set_error_code (PyObject * err , enum XML_Error code )
110
+ set_xml_error_attr_code (PyObject * err , enum XML_Error code )
111
111
{
112
112
PyObject * v = PyLong_FromLong ((long )code );
113
113
int ok = v != NULL && PyObject_SetAttr (err , & _Py_ID (code ), v ) != -1 ;
@@ -119,7 +119,7 @@ set_error_code(PyObject *err, enum XML_Error code)
119
119
* false on an exception.
120
120
*/
121
121
static int
122
- set_error_location (PyObject * err , const char * name , XML_Size value )
122
+ set_xml_error_attr_location (PyObject * err , const char * name , XML_Size value )
123
123
{
124
124
PyObject * v = PyLong_FromSize_t ((size_t )value );
125
125
int ok = v != NULL && PyObject_SetAttrString (err , name , v ) != -1 ;
@@ -128,42 +128,32 @@ set_error_location(PyObject *err, const char *name, XML_Size value)
128
128
}
129
129
130
130
131
- static PyObject *
132
- format_xml_error (enum XML_Error code , XML_Size lineno , XML_Size column )
133
- {
134
- const char * errmsg = XML_ErrorString (code );
135
- PyUnicodeWriter * writer = PyUnicodeWriter_Create (strlen (errmsg ) + 1 );
136
- if (writer == NULL ) {
137
- return NULL ;
138
- }
139
- if (PyUnicodeWriter_Format (writer ,
140
- "%s: line %zu, column %zu" ,
141
- errmsg , (size_t )lineno , (size_t )column ) < 0 )
142
- {
143
- PyUnicodeWriter_Discard (writer );
144
- return NULL ;
145
- }
146
- return PyUnicodeWriter_Finish (writer );
147
- }
148
-
149
131
static PyObject *
150
132
set_xml_error (pyexpat_state * state ,
151
133
enum XML_Error code , XML_Size lineno , XML_Size column ,
152
134
const char * errmsg )
153
135
{
154
- PyObject * arg = errmsg == NULL
155
- ? format_xml_error (code , lineno , column )
156
- : PyUnicode_FromStringAndSize (errmsg , strlen (errmsg ));
136
+ PyObject * arg ;
137
+ if (errmsg == NULL ) {
138
+ arg = PyUnicode_FromFormat (
139
+ "%s: line %zu, column %zu" ,
140
+ XML_ErrorString (code ),
141
+ (size_t )lineno , (size_t )column
142
+ );
143
+ }
144
+ else {
145
+ arg = PyUnicode_FromStringAndSize (errmsg , strlen (errmsg ));
146
+ }
157
147
if (arg == NULL ) {
158
148
return NULL ;
159
149
}
160
150
PyObject * res = PyObject_CallOneArg (state -> error , arg );
161
151
Py_DECREF (arg );
162
152
if (
163
153
res != NULL
164
- && set_error_code (res , code )
165
- && set_error_location (res , "lineno" , lineno )
166
- && set_error_location (res , "offset" , column )
154
+ && set_xml_error_attr_code (res , code )
155
+ && set_xml_error_attr_location (res , "lineno" , lineno )
156
+ && set_xml_error_attr_location (res , "offset" , column )
167
157
) {
168
158
PyErr_SetObject (state -> error , res );
169
159
}
@@ -1149,6 +1139,50 @@ pyexpat_xmlparser_UseForeignDTD_impl(xmlparseobject *self, PyTypeObject *cls,
1149
1139
}
1150
1140
#endif
1151
1141
1142
+ #if XML_COMBINED_VERSION >= 20702
1143
+ static PyObject *
1144
+ set_activation_threshold (xmlparseobject * self ,
1145
+ PyTypeObject * cls ,
1146
+ unsigned long long threshold ,
1147
+ XML_Bool (* setter )(XML_Parser , unsigned long long ))
1148
+ {
1149
+ assert (self -> itself != NULL );
1150
+ if (setter (self -> itself , threshold ) == XML_TRUE ) {
1151
+ Py_RETURN_NONE ;
1152
+ }
1153
+ // The setter fails if self->itself is NULL (which is not possible here)
1154
+ // or is a non-root parser, which currently only happens for parsers
1155
+ // created by ExternalEntityParserCreate().
1156
+ pyexpat_state * state = PyType_GetModuleState (cls );
1157
+ return set_invalid_arg (state , self , "parser must be a root parser" );
1158
+ }
1159
+
1160
+ static PyObject *
1161
+ set_maximum_amplification (xmlparseobject * self ,
1162
+ PyTypeObject * cls ,
1163
+ float max_factor ,
1164
+ XML_Bool (* setter )(XML_Parser , float ))
1165
+ {
1166
+ assert (self -> itself != NULL );
1167
+ if (setter (self -> itself , max_factor ) == XML_TRUE ) {
1168
+ Py_RETURN_NONE ;
1169
+ }
1170
+ // The setter fails if self->itself is NULL (which is not possible here),
1171
+ // is a non-root parser, which currently only happens for parsers created
1172
+ // by ExternalEntityParserCreate(), or if 'max_factor' is NaN or < 1.0.
1173
+ pyexpat_state * state = PyType_GetModuleState (cls );
1174
+ // Note: Expat has no API to determine whether a parser is a root parser,
1175
+ // and since the Expat functions for defining the various maximum allowed
1176
+ // amplifcation factors fail when a bad parser or an out-of-range factor
1177
+ // is given without specifying which check failed, we check whether the
1178
+ // factor is out-of-range to improve the error message. See also gh-90949.
1179
+ const char * message = (isnan (max_factor ) || max_factor < 1.0f )
1180
+ ? "'max_factor' must be at least 1.0"
1181
+ : "parser must be a root parser" ;
1182
+ return set_invalid_arg (state , self , message );
1183
+ }
1184
+ #endif
1185
+
1152
1186
#if XML_COMBINED_VERSION >= 20702
1153
1187
/*[clinic input]
1154
1188
pyexpat.xmlparser.SetAllocTrackerActivationThreshold
@@ -1168,15 +1202,10 @@ pyexpat_xmlparser_SetAllocTrackerActivationThreshold_impl(xmlparseobject *self,
1168
1202
unsigned long long threshold )
1169
1203
/*[clinic end generated code: output=bed7e93207ba08c5 input=9c706b75c18e4ea1]*/
1170
1204
{
1171
- assert (self -> itself != NULL );
1172
- if (XML_SetAllocTrackerActivationThreshold (self -> itself , threshold ) == XML_TRUE ) {
1173
- Py_RETURN_NONE ;
1174
- }
1175
- // XML_SetAllocTrackerActivationThreshold() can only fail if self->itself
1176
- // is not a root parser (currently, this is equivalent to be created
1177
- // by ExternalEntityParserCreate()).
1178
- pyexpat_state * state = PyType_GetModuleState (cls );
1179
- return set_invalid_arg (state , self , "parser must be a root parser" );
1205
+ return set_activation_threshold (
1206
+ self , cls , threshold ,
1207
+ XML_SetAllocTrackerActivationThreshold
1208
+ );
1180
1209
}
1181
1210
#endif
1182
1211
@@ -1209,24 +1238,10 @@ pyexpat_xmlparser_SetAllocTrackerMaximumAmplification_impl(xmlparseobject *self,
1209
1238
float max_factor )
1210
1239
/*[clinic end generated code: output=6e44bd48c9b112a0 input=918b9266b490a722]*/
1211
1240
{
1212
- assert (self -> itself != NULL );
1213
- if (XML_SetAllocTrackerMaximumAmplification (self -> itself , max_factor ) == XML_TRUE ) {
1214
- Py_RETURN_NONE ;
1215
- }
1216
- // XML_SetAllocTrackerMaximumAmplification() can fail if self->itself
1217
- // is not a root parser (currently, this is equivalent to be created
1218
- // by ExternalEntityParserCreate()) or if 'max_factor' is NaN or < 1.0.
1219
- //
1220
- // Expat does not provide a way to determine whether a parser is a root
1221
- // or not, nor does it provide a way to distinguish between failures in
1222
- // XML_SetAllocTrackerMaximumAmplification() (see gh-90949), we manually
1223
- // detect the factor out-of-range issue here so that users have a better
1224
- // error message.
1225
- pyexpat_state * state = PyType_GetModuleState (cls );
1226
- const char * message = (isnan (max_factor ) || max_factor < 1.0f )
1227
- ? "'max_factor' must be at least 1.0"
1228
- : "parser must be a root parser" ;
1229
- return set_invalid_arg (state , self , message );
1241
+ return set_maximum_amplification (
1242
+ self , cls , max_factor ,
1243
+ XML_SetAllocTrackerMaximumAmplification
1244
+ );
1230
1245
}
1231
1246
#endif
1232
1247
0 commit comments