Skip to content

Commit

Permalink
registry: Start using libxml2 contextual API
Browse files Browse the repository at this point in the history
Contextual functions are safer because they do not rely on a global
state.
  • Loading branch information
wismill committed Oct 13, 2024
1 parent a47961b commit c6032ce
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
7 changes: 7 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,13 @@ endif
# libxkbregistry
if get_option('enable-xkbregistry')
dep_libxml = dependency('libxml-2.0')
if cc.has_header_symbol(
'libxml/xmlerror.h',
'xmlCtxtSetErrorHandler',
prefix: system_ext_define
)
configh_data.set10('HAVE_XML_CTXT_SET_ERRORHANDLER', true)
endif
deps_libxkbregistry = [dep_libxml]
libxkbregistry_sources = [
'src/registry.c',
Expand Down
27 changes: 24 additions & 3 deletions src/registry.c
Original file line number Diff line number Diff line change
Expand Up @@ -1167,6 +1167,14 @@ xml_error_func(void *ctx, const char *msg, ...)
}
}

#ifdef HAVE_XML_CTXT_SET_ERRORHANDLER
static void
xml_structured_error_func(void *userData, const xmlError * error)
{
xmlFormatError(error, xml_error_func, userData);
}
#endif

static bool
validate(struct rxkb_context *ctx, xmlDoc *doc)
{
Expand Down Expand Up @@ -1211,7 +1219,7 @@ validate(struct rxkb_context *ctx, xmlDoc *doc)
/* Note: do not use xmlParserInputBufferCreateStatic, it generates random
* DTD validity errors for unknown reasons */
buf = xmlParserInputBufferCreateMem(dtdstr, sizeof(dtdstr),
XML_CHAR_ENCODING_UTF8);
XML_CHAR_ENCODING_NONE);
if (!buf)
return false;

Expand Down Expand Up @@ -1246,9 +1254,19 @@ parse(struct rxkb_context *ctx, const char *path,

LIBXML_TEST_VERSION

xmlParserCtxtPtr xmlCtxt = xmlNewParserCtxt();
if (!xmlCtxt)
return false;

#ifdef HAVE_XML_CTXT_SET_ERRORHANDLER
/* Prefer contextual handler whenever possible. It takes precedence over
* the global generic handler. */
xmlCtxtSetErrorHandler(xmlCtxt, xml_structured_error_func, ctx);
#endif
/* This is still unconditionnally needed for the DTD validation (for now) */
xmlSetGenericErrorFunc(ctx, xml_error_func);

doc = xmlParseFile(path);
doc = xmlCtxtReadFile(xmlCtxt, path, NULL, 0);
if (!doc)
goto parse_error;

Expand All @@ -1274,8 +1292,11 @@ parse(struct rxkb_context *ctx, const char *path,
* rxkb_context_parse();
* rxkb_context_unref();
*/
/* TODO: use the new API xmlCtxtSetErrorHandler */
xmlSetGenericErrorFunc(NULL, NULL);
#ifdef HAVE_XML_CTXT_SET_ERRORHANDLER
xmlCtxtSetErrorHandler(xmlCtxt, NULL, NULL);
#endif
xmlFreeParserCtxt(xmlCtxt);

return success;
}

0 comments on commit c6032ce

Please sign in to comment.