Submitted By:            Xi Ruoyao <xry111 at xry111 dot site>
Date:                    2024-08-19
Initial Package Version: 2.13.3
Upstream Status:         Applied
Origin:                  Upstream Git repo (see the From line below)
Description:             Fix a bogus warning from "xmlcatalog --create
                         --noout" when the target file does not exist.

From 84666581c217e81332e115b3932f449469a4a76f Mon Sep 17 00:00:00 2001
From: Nick Wellnhofer <wellnhofer@aevum.de>
Date: Sat, 15 Jun 2024 20:34:07 +0200
Subject: [PATCH] catalog: Fix initialization

Initialize mutex via xmlInitParser.

Fix some other initialization calls.
---
 catalog.c                   | 56 ++++++++++++++-----------------------
 include/private/Makefile.am |  1 +
 include/private/cata.h      | 13 +++++++++
 threads.c                   |  5 ++++
 4 files changed, 40 insertions(+), 35 deletions(-)
 create mode 100644 include/private/cata.h

diff --git a/catalog.c b/catalog.c
index ef0eef57..a83e13f2 100644
--- a/catalog.c
+++ b/catalog.c
@@ -38,6 +38,7 @@
 #include <libxml/xmlerror.h>
 #include <libxml/threads.h>
 
+#include "private/cata.h"
 #include "private/buf.h"
 #include "private/error.h"
 
@@ -169,7 +170,7 @@ static xmlCatalogPtr xmlDefaultCatalog = NULL;
 static xmlRMutexPtr xmlCatalogMutex = NULL;
 
 /*
- * Whether the catalog support was initialized.
+ * Whether the default system catalog was initialized.
  */
 static int xmlCatalogInitialized = 0;
 
@@ -3056,41 +3057,31 @@ xmlCatalogIsEmpty(xmlCatalogPtr catal) {
  ************************************************************************/
 
 /**
- * xmlInitializeCatalogData:
+ * xmlInitCatalogInternal:
  *
  * Do the catalog initialization only of global data, doesn't try to load
  * any catalog actually.
- * this function is not thread safe, catalog initialization should
- * preferably be done once at startup
  */
-static void
-xmlInitializeCatalogData(void) {
-    if (xmlCatalogInitialized != 0)
-	return;
-
+void
+xmlInitCatalogInternal(void) {
     if (getenv("XML_DEBUG_CATALOG"))
 	xmlDebugCatalogs = 1;
     xmlCatalogMutex = xmlNewRMutex();
-
-    xmlCatalogInitialized = 1;
 }
+
 /**
  * xmlInitializeCatalog:
  *
- * Do the catalog initialization.
- * this function is not thread safe, catalog initialization should
- * preferably be done once at startup
+ * Load the default system catalog.
  */
 void
 xmlInitializeCatalog(void) {
     if (xmlCatalogInitialized != 0)
 	return;
 
-    xmlInitializeCatalogData();
-    xmlRMutexLock(xmlCatalogMutex);
+    xmlInitParser();
 
-    if (getenv("XML_DEBUG_CATALOG"))
-	xmlDebugCatalogs = 1;
+    xmlRMutexLock(xmlCatalogMutex);
 
     if (xmlDefaultCatalog == NULL) {
 	const char *catalogs;
@@ -3152,8 +3143,7 @@ xmlLoadCatalog(const char *filename)
     int ret;
     xmlCatalogPtr catal;
 
-    if (!xmlCatalogInitialized)
-	xmlInitializeCatalogData();
+    xmlInitParser();
 
     xmlRMutexLock(xmlCatalogMutex);
 
@@ -3228,9 +3218,6 @@ xmlLoadCatalogs(const char *pathss) {
  */
 void
 xmlCatalogCleanup(void) {
-    if (xmlCatalogInitialized == 0)
-        return;
-
     xmlRMutexLock(xmlCatalogMutex);
     if (xmlDebugCatalogs)
 	fprintf(stderr,
@@ -3244,6 +3231,15 @@ xmlCatalogCleanup(void) {
     xmlDebugCatalogs = 0;
     xmlCatalogInitialized = 0;
     xmlRMutexUnlock(xmlCatalogMutex);
+}
+
+/**
+ * xmlCleanupCatalogInternal:
+ *
+ * Free global data.
+ */
+void
+xmlCleanupCatalogInternal(void) {
     xmlFreeRMutex(xmlCatalogMutex);
 }
 
@@ -3365,7 +3361,7 @@ xmlCatalogAdd(const xmlChar *type, const xmlChar *orig, const xmlChar *replace)
     int res = -1;
 
     if (!xmlCatalogInitialized)
-	xmlInitializeCatalogData();
+	xmlInitializeCatalog();
 
     xmlRMutexLock(xmlCatalogMutex);
     /*
@@ -3552,9 +3548,6 @@ void
 xmlCatalogFreeLocal(void *catalogs) {
     xmlCatalogEntryPtr catal;
 
-    if (!xmlCatalogInitialized)
-	xmlInitializeCatalog();
-
     catal = (xmlCatalogEntryPtr) catalogs;
     if (catal != NULL)
 	xmlFreeCatalogEntryList(catal);
@@ -3574,8 +3567,7 @@ void *
 xmlCatalogAddLocal(void *catalogs, const xmlChar *URL) {
     xmlCatalogEntryPtr catal, add;
 
-    if (!xmlCatalogInitialized)
-	xmlInitializeCatalog();
+    xmlInitParser();
 
     if (URL == NULL)
 	return(catalogs);
@@ -3617,9 +3609,6 @@ xmlCatalogLocalResolve(void *catalogs, const xmlChar *pubID,
     xmlCatalogEntryPtr catal;
     xmlChar *ret;
 
-    if (!xmlCatalogInitialized)
-	xmlInitializeCatalog();
-
     if ((pubID == NULL) && (sysID == NULL))
 	return(NULL);
 
@@ -3661,9 +3650,6 @@ xmlCatalogLocalResolveURI(void *catalogs, const xmlChar *URI) {
     xmlCatalogEntryPtr catal;
     xmlChar *ret;
 
-    if (!xmlCatalogInitialized)
-	xmlInitializeCatalog();
-
     if (URI == NULL)
 	return(NULL);
 
diff --git a/include/private/Makefile.am b/include/private/Makefile.am
index 29c0bdd6..08f047ea 100644
--- a/include/private/Makefile.am
+++ b/include/private/Makefile.am
@@ -1,5 +1,6 @@
 EXTRA_DIST = \
 	buf.h \
+	cata.h \
 	dict.h \
 	enc.h \
 	entities.h \
diff --git a/include/private/cata.h b/include/private/cata.h
new file mode 100644
index 00000000..789808aa
--- /dev/null
+++ b/include/private/cata.h
@@ -0,0 +1,13 @@
+#ifndef XML_CATA_H_PRIVATE__
+#define XML_CATA_H_PRIVATE__
+
+#ifdef LIBXML_CATALOG_ENABLED
+
+XML_HIDDEN void
+xmlInitCatalogInternal(void);
+XML_HIDDEN void
+xmlCleanupCatalogInternal(void);
+
+#endif /* LIBXML_CATALOG_ENABLED */
+
+#endif /* XML_CATA_H_PRIVATE__ */
diff --git a/threads.c b/threads.c
index 36347c2f..240aee43 100644
--- a/threads.c
+++ b/threads.c
@@ -27,6 +27,7 @@
 #include <note.h>
 #endif
 
+#include "private/cata.h"
 #include "private/dict.h"
 #include "private/enc.h"
 #include "private/globals.h"
@@ -592,6 +593,9 @@ xmlInitParser(void) {
         xmlInitXPathInternal();
 #endif
         xmlInitIOCallbacks();
+#ifdef LIBXML_CATALOG_ENABLED
+        xmlInitCatalogInternal();
+#endif
 
         xmlParserInnerInitialized = 1;
     }
@@ -632,6 +636,7 @@ xmlCleanupParser(void) {
     xmlCleanupCharEncodingHandlers();
 #ifdef LIBXML_CATALOG_ENABLED
     xmlCatalogCleanup();
+    xmlCleanupCatalogInternal();
 #endif
 #ifdef LIBXML_SCHEMAS_ENABLED
     xmlSchemaCleanupTypes();
-- 
2.46.0

