When compiling against libxml 2.12.6, I get:
CXX extract_xml-extract_xml.o
/home/simark/src/lttng-tools/tests/utils/xml-utils/extract_xml.cpp: In function ‘int main(int, char**)’:
/home/simark/src/lttng-tools/tests/utils/xml-utils/extract_xml.cpp:256:29: error: ‘int xmlKeepBlanksDefault(int)’ is deprecated [-Werror=deprecated-declarations]
256 | xmlKeepBlanksDefault(0);
| ~~~~~~~~~~~~~~~~~~~~^~~
In file included from /home/simark/src/lttng-tools/tests/utils/xml-utils/extract_xml.cpp:29:
/usr/include/libxml2/libxml/parser.h:957:17: note: declared here
957 | xmlKeepBlanksDefault (int val);
| ^~~~~~~~~~~~~~~~~~~~
The documentation[1] suggests moving to "the modern options API with
XML_PARSE_NOBLANKS", do that.
Add a new `xml_parser_ctx_uptr` RAII object to manage the lifetime of
the `xmlParserCtxt` object.
[1] https://gnome.pages.gitlab.gnome.org/libxml2/devhelp/libxml2-parser.html#xmlKeepBlanksDefault
Change-Id: I7f9e70d2245fa333e01296aa2b4e3efa4e7fbefa
Signed-off-by: Simon Marchi <simon.marchi@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
# SPDX-License-Identifier: GPL-2.0-only
# SPDX-License-Identifier: GPL-2.0-only
+EXTRA_DIST = common.hpp
+
noinst_PROGRAMS = validate_xml extract_xml pretty_xml
validate_xml_SOURCES = validate_xml.cpp
validate_xml_CPPFLAGS = $(libxml2_CFLAGS) $(AM_CPPFLAGS)
noinst_PROGRAMS = validate_xml extract_xml pretty_xml
validate_xml_SOURCES = validate_xml.cpp
validate_xml_CPPFLAGS = $(libxml2_CFLAGS) $(AM_CPPFLAGS)
--- /dev/null
+/*
+ * Copyright (C) 2024 EfficiOS Inc.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ */
+
+#ifndef TESTS_UTILS_XML_UTILS_COMMON_HPP
+#define TESTS_UTILS_XML_UTILS_COMMON_HPP
+
+#include "common/make-unique-wrapper.hpp"
+
+#include <libxml/parser.h>
+#include <memory>
+
+using xml_parser_ctx_uptr = std::unique_ptr<
+ xmlParserCtxt,
+ lttng::memory::create_deleter_class<xmlParserCtxt, xmlFreeParserCtxt>::deleter>;
+
+#endif /* TESTS_UTILS_XML_UTILS_COMMON_HPP */
#include <common/defaults.hpp>
#include <libxml/parser.h>
#include <common/defaults.hpp>
#include <libxml/parser.h>
LTTNG_ASSERT(xml_path);
LTTNG_ASSERT(xpath);
LTTNG_ASSERT(xml_path);
LTTNG_ASSERT(xpath);
+ xml_parser_ctx_uptr parserCtx{ xmlNewParserCtxt() };
+
+ if (!parserCtx) {
+ fprintf(stderr, "ERR: could not allocate an XML parser context\n");
+ return -1;
+ }
+
- doc = xmlParseFile(xml_path);
+ doc = xmlCtxtReadFile(parserCtx.get(), xml_path, nullptr, XML_PARSE_NOBLANKS);
if (!doc) {
fprintf(stderr, "ERR parsing: xml file invalid \"%s\"\n", xml_path);
return -1;
if (!doc) {
fprintf(stderr, "ERR parsing: xml file invalid \"%s\"\n", xml_path);
return -1;
/* Init libxml */
xmlInitParser();
/* Init libxml */
xmlInitParser();
- xmlKeepBlanksDefault(0);
if (access(argv[optind], F_OK)) {
fprintf(stderr, "ERR:%s\n", "Xml path not valid");
return -1;
if (access(argv[optind], F_OK)) {
fprintf(stderr, "ERR:%s\n", "Xml path not valid");
return -1;
* This allows a more human friendly format for xml testing when problems occur.
*/
* This allows a more human friendly format for xml testing when problems occur.
*/
#include <libxml/parser.h>
#include <libxml/parser.h>
{
xmlDocPtr doc = NULL;
/* Init libxml. */
xmlInitParser();
{
xmlDocPtr doc = NULL;
/* Init libxml. */
xmlInitParser();
- xmlKeepBlanksDefault(0);
- /* Parse the XML document from stdin. */
- doc = xmlParseFile("-");
- if (!doc) {
- fprintf(stderr, "ERR parsing: xml input invalid");
- return -1;
- }
+ {
+ xml_parser_ctx_uptr parserCtx{ xmlNewParserCtxt() };
+
+ /* Parse the XML document from stdin. */
+ doc = xmlCtxtReadFd(
+ parserCtx.get(), STDIN_FILENO, nullptr, nullptr, XML_PARSE_NOBLANKS);
+ if (!doc) {
+ fprintf(stderr, "ERR parsing: xml input invalid");
+ return -1;
+ }
- xmlDocFormatDump(stdout, doc, 1);
+ xmlDocFormatDump(stdout, doc, 1);
+
+ xmlFreeDoc(doc);
+ }
/* Shutdown libxml. */
xmlCleanupParser();
/* Shutdown libxml. */
xmlCleanupParser();