@@ -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
}
@@ -1148,6 +1138,50 @@ pyexpat_xmlparser_UseForeignDTD_impl(xmlparseobject *self, PyTypeObject *cls,
1148
1138
}
1149
1139
#endif
1150
1140
1141
+ #if XML_COMBINED_VERSION >= 20702
1142
+ static PyObject *
1143
+ set_activation_threshold (xmlparseobject * self ,
1144
+ PyTypeObject * cls ,
1145
+ unsigned long long threshold ,
1146
+ XML_Bool (* setter )(XML_Parser , unsigned long long ))
1147
+ {
1148
+ assert (self -> itself != NULL );
1149
+ if (setter (self -> itself , threshold ) == XML_TRUE ) {
1150
+ Py_RETURN_NONE ;
1151
+ }
1152
+ // The setter fails if self->itself is NULL (which is not possible here)
1153
+ // or is a non-root parser, which currently only happens for parsers
1154
+ // created by ExternalEntityParserCreate().
1155
+ pyexpat_state * state = PyType_GetModuleState (cls );
1156
+ return set_invalid_arg (state , self , "parser must be a root parser" );
1157
+ }
1158
+
1159
+ static PyObject *
1160
+ set_maximum_amplification (xmlparseobject * self ,
1161
+ PyTypeObject * cls ,
1162
+ float max_factor ,
1163
+ XML_Bool (* setter )(XML_Parser , float ))
1164
+ {
1165
+ assert (self -> itself != NULL );
1166
+ if (setter (self -> itself , max_factor ) == XML_TRUE ) {
1167
+ Py_RETURN_NONE ;
1168
+ }
1169
+ // The setter fails if self->itself is NULL (which is not possible here),
1170
+ // is a non-root parser, which currently only happens for parsers created
1171
+ // by ExternalEntityParserCreate(), or if 'max_factor' is NaN or < 1.0.
1172
+ pyexpat_state * state = PyType_GetModuleState (cls );
1173
+ // Note: Expat has no API to determine whether a parser is a root parser,
1174
+ // and since the Expat functions for defining the various maximum allowed
1175
+ // amplifcation factors fail when a bad parser or an out-of-range factor
1176
+ // is given without specifying which check failed, we check whether the
1177
+ // factor is out-of-range to improve the error message. See also gh-90949.
1178
+ const char * message = (isnan (max_factor ) || max_factor < 1.0f )
1179
+ ? "'max_factor' must be at least 1.0"
1180
+ : "parser must be a root parser" ;
1181
+ return set_invalid_arg (state , self , message );
1182
+ }
1183
+ #endif
1184
+
1151
1185
#if XML_COMBINED_VERSION >= 20702
1152
1186
/*[clinic input]
1153
1187
pyexpat.xmlparser.SetAllocTrackerActivationThreshold
@@ -1167,15 +1201,10 @@ pyexpat_xmlparser_SetAllocTrackerActivationThreshold_impl(xmlparseobject *self,
1167
1201
unsigned long long threshold )
1168
1202
/*[clinic end generated code: output=bed7e93207ba08c5 input=9c706b75c18e4ea1]*/
1169
1203
{
1170
- assert (self -> itself != NULL );
1171
- if (XML_SetAllocTrackerActivationThreshold (self -> itself , threshold ) == XML_TRUE ) {
1172
- Py_RETURN_NONE ;
1173
- }
1174
- // XML_SetAllocTrackerActivationThreshold() can only fail if self->itself
1175
- // is not a root parser (currently, this is equivalent to be created
1176
- // by ExternalEntityParserCreate()).
1177
- pyexpat_state * state = PyType_GetModuleState (cls );
1178
- return set_invalid_arg (state , self , "parser must be a root parser" );
1204
+ return set_activation_threshold (
1205
+ self , cls , threshold ,
1206
+ XML_SetAllocTrackerActivationThreshold
1207
+ );
1179
1208
}
1180
1209
#endif
1181
1210
@@ -1208,24 +1237,10 @@ pyexpat_xmlparser_SetAllocTrackerMaximumAmplification_impl(xmlparseobject *self,
1208
1237
float max_factor )
1209
1238
/*[clinic end generated code: output=6e44bd48c9b112a0 input=918b9266b490a722]*/
1210
1239
{
1211
- assert (self -> itself != NULL );
1212
- if (XML_SetAllocTrackerMaximumAmplification (self -> itself , max_factor ) == XML_TRUE ) {
1213
- Py_RETURN_NONE ;
1214
- }
1215
- // XML_SetAllocTrackerMaximumAmplification() can fail if self->itself
1216
- // is not a root parser (currently, this is equivalent to be created
1217
- // by ExternalEntityParserCreate()) or if 'max_factor' is NaN or < 1.0.
1218
- //
1219
- // Expat does not provide a way to determine whether a parser is a root
1220
- // or not, nor does it provide a way to distinguish between failures in
1221
- // XML_SetAllocTrackerMaximumAmplification() (see gh-90949), we manually
1222
- // detect the factor out-of-range issue here so that users have a better
1223
- // error message.
1224
- pyexpat_state * state = PyType_GetModuleState (cls );
1225
- const char * message = (isnan (max_factor ) || max_factor < 1.0f )
1226
- ? "'max_factor' must be at least 1.0"
1227
- : "parser must be a root parser" ;
1228
- return set_invalid_arg (state , self , message );
1240
+ return set_maximum_amplification (
1241
+ self , cls , max_factor ,
1242
+ XML_SetAllocTrackerMaximumAmplification
1243
+ );
1229
1244
}
1230
1245
#endif
1231
1246
0 commit comments