gh-133157: remove usage of _Py_NO_SANITIZE_UNDEFINED in pyexpat (#135346)

This was the last usage, so the macro is removed as well.

Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
This commit is contained in:
Petr Viktorin 2025-07-01 10:54:08 +02:00 committed by GitHub
parent 23caccf74c
commit 845263adc6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 96 additions and 29 deletions

View file

@ -98,7 +98,11 @@ typedef struct {
#define CHARACTER_DATA_BUFFER_SIZE 8192
typedef const void *xmlhandler;
// A generic function type for storage.
// To avoid undefined behaviors, a handler must be cast to the correct
// function type before it's called; see SETTER_WRAPPER below.
typedef void (*xmlhandler)(void);
typedef void (*xmlhandlersetter)(XML_Parser self, xmlhandler handler);
struct HandlerInfo {
@ -110,9 +114,7 @@ struct HandlerInfo {
static struct HandlerInfo handler_info[64];
// gh-111178: Use _Py_NO_SANITIZE_UNDEFINED, rather than using the exact
// handler API for each handler.
static inline void _Py_NO_SANITIZE_UNDEFINED
static inline void
CALL_XML_HANDLER_SETTER(const struct HandlerInfo *handler_info,
XML_Parser xml_parser, xmlhandler xml_handler)
{
@ -1365,7 +1367,7 @@ xmlparse_handler_setter(PyObject *op, PyObject *v, void *closure)
elaborate system of handlers and state could remove the
C handler more effectively. */
if (handlernum == CharacterData && self->in_callback) {
c_handler = noop_character_data_handler;
c_handler = (xmlhandler)noop_character_data_handler;
}
v = NULL;
}
@ -2222,13 +2224,84 @@ clear_handlers(xmlparseobject *self, int initial)
}
}
/* To avoid undefined behaviors, a function must be *called* via a function
* pointer of the correct type.
* So, for each `XML_Set*` function, we define a wrapper that calls `XML_Set*`
* with its argument cast to the appropriate type.
*/
typedef void (*parser_only)(void *);
typedef int (*not_standalone)(void *);
typedef void (*parser_and_data)(void *, const XML_Char *);
typedef void (*parser_and_data_and_int)(void *, const XML_Char *, int);
typedef void (*parser_and_data_and_data)(
void *, const XML_Char *, const XML_Char *);
typedef void (*start_element)(void *, const XML_Char *, const XML_Char **);
typedef void (*element_decl)(void *, const XML_Char *, XML_Content *);
typedef void (*xml_decl)(
void *, const XML_Char *, const XML_Char *, int);
typedef void (*start_doctype_decl)(
void *, const XML_Char *, const XML_Char *, const XML_Char *, int);
typedef void (*notation_decl)(
void *,
const XML_Char *, const XML_Char *, const XML_Char *, const XML_Char *);
typedef void (*attlist_decl)(
void *,
const XML_Char *, const XML_Char *, const XML_Char *, const XML_Char *,
int);
typedef void (*unparsed_entity_decl)(
void *,
const XML_Char *, const XML_Char *,
const XML_Char *, const XML_Char *, const XML_Char *);
typedef void (*entity_decl)(
void *,
const XML_Char *, int,
const XML_Char *, int,
const XML_Char *, const XML_Char *, const XML_Char *, const XML_Char *);
typedef int (*external_entity_ref)(
XML_Parser,
const XML_Char *, const XML_Char *, const XML_Char *, const XML_Char *);
#define SETTER_WRAPPER(NAME, TYPE) \
static inline void \
pyexpat_Set ## NAME (XML_Parser parser, xmlhandler handler) \
{ \
(void)XML_Set ## NAME (parser, (TYPE)handler); \
}
SETTER_WRAPPER(StartElementHandler, start_element)
SETTER_WRAPPER(EndElementHandler, parser_and_data)
SETTER_WRAPPER(ProcessingInstructionHandler, parser_and_data_and_data)
SETTER_WRAPPER(CharacterDataHandler, parser_and_data_and_int)
SETTER_WRAPPER(UnparsedEntityDeclHandler, unparsed_entity_decl)
SETTER_WRAPPER(NotationDeclHandler, notation_decl)
SETTER_WRAPPER(StartNamespaceDeclHandler, parser_and_data_and_data)
SETTER_WRAPPER(EndNamespaceDeclHandler, parser_and_data)
SETTER_WRAPPER(CommentHandler, parser_and_data)
SETTER_WRAPPER(StartCdataSectionHandler, parser_only)
SETTER_WRAPPER(EndCdataSectionHandler, parser_only)
SETTER_WRAPPER(DefaultHandler, parser_and_data_and_int)
SETTER_WRAPPER(DefaultHandlerExpand, parser_and_data_and_int)
SETTER_WRAPPER(NotStandaloneHandler, not_standalone)
SETTER_WRAPPER(ExternalEntityRefHandler, external_entity_ref)
SETTER_WRAPPER(StartDoctypeDeclHandler, start_doctype_decl)
SETTER_WRAPPER(EndDoctypeDeclHandler, parser_only)
SETTER_WRAPPER(EntityDeclHandler, entity_decl)
SETTER_WRAPPER(XmlDeclHandler, xml_decl)
SETTER_WRAPPER(ElementDeclHandler, element_decl)
SETTER_WRAPPER(AttlistDeclHandler, attlist_decl)
#if XML_COMBINED_VERSION >= 19504
SETTER_WRAPPER(SkippedEntityHandler, parser_and_data_and_int)
#endif
#undef SETTER_WRAPPER
static struct HandlerInfo handler_info[] = {
// The cast to `xmlhandlersetter` is needed as the signature of XML
// handler functions is not compatible with `xmlhandlersetter` since
// their second parameter is narrower than a `const void *`.
#define HANDLER_INFO(name) \
{#name, (xmlhandlersetter)XML_Set##name, my_##name},
{#name, (xmlhandlersetter)pyexpat_Set##name, (xmlhandler)my_##name},
HANDLER_INFO(StartElementHandler)
HANDLER_INFO(EndElementHandler)