1From 250faf3c832d998baa559ca1a1c61935235aba20 Mon Sep 17 00:00:00 2001 2From: Nick Wellnhofer <wellnhofer@aevum.de> 3Date: Thu, 20 Apr 2023 12:35:21 +0200 4Subject: [PATCH] parser: Fix regression in xmlParserNodeInfo accounting 5 6Commit 62150ed2 broke begin_pos and begin_line when extra node info was 7recorded. 8 9Fixes #523. 10 11Reference:https://github.com/GNOME/libxml2/commit/250faf3c832d998baa559ca1a1c61935235aba20 12Conflict:NA 13 14--- 15 SAX2.c | 20 ++------------------ 16 parser.c | 53 +++++++++++++++++++++++++---------------------------- 17 2 files changed, 27 insertions(+), 46 deletions(-) 18 19diff --git a/SAX2.c b/SAX2.c 20index 916e974..822b975 100644 21--- a/SAX2.c 22+++ b/SAX2.c 23@@ -1783,13 +1783,6 @@ xmlSAX2EndElement(void *ctx, const xmlChar *name ATTRIBUTE_UNUSED) 24 xmlGenericError(xmlGenericErrorContext, "SAX.xmlSAX2EndElement(%s)\n", name); 25 #endif 26 27- /* Capture end position and add node */ 28- if (cur != NULL && ctxt->record_info) { 29- ctxt->nodeInfo->end_pos = ctxt->input->cur - ctxt->input->base; 30- ctxt->nodeInfo->end_line = ctxt->input->line; 31- ctxt->nodeInfo->node = cur; 32- xmlParserAddNodeInfo(ctxt, ctxt->nodeInfo); 33- } 34 ctxt->nodemem = -1; 35 36 #ifdef LIBXML_VALID_ENABLED 37@@ -2433,24 +2426,15 @@ xmlSAX2EndElementNs(void *ctx, 38 const xmlChar * URI ATTRIBUTE_UNUSED) 39 { 40 xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx; 41- xmlParserNodeInfo node_info; 42- xmlNodePtr cur; 43 44 if (ctx == NULL) return; 45- cur = ctxt->node; 46- /* Capture end position and add node */ 47- if ((ctxt->record_info) && (cur != NULL)) { 48- node_info.end_pos = ctxt->input->cur - ctxt->input->base; 49- node_info.end_line = ctxt->input->line; 50- node_info.node = cur; 51- xmlParserAddNodeInfo(ctxt, &node_info); 52- } 53 ctxt->nodemem = -1; 54 55 #ifdef LIBXML_VALID_ENABLED 56 if (ctxt->validate && ctxt->wellFormed && 57 ctxt->myDoc && ctxt->myDoc->intSubset) 58- ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, cur); 59+ ctxt->valid &= xmlValidateOneElement(&ctxt->vctxt, ctxt->myDoc, 60+ ctxt->node); 61 #endif /* LIBXML_VALID_ENABLED */ 62 63 /* 64diff --git a/parser.c b/parser.c 65index a4c9fb2..94a6298 100644 66--- a/parser.c 67+++ b/parser.c 68@@ -10025,7 +10025,7 @@ xmlParseElementStart(xmlParserCtxtPtr ctxt) { 69 const xmlChar *URI = NULL; 70 xmlParserNodeInfo node_info; 71 int line, tlen = 0; 72- xmlNodePtr ret; 73+ xmlNodePtr cur; 74 int nsNr = ctxt->nsNr; 75 76 if (((unsigned int) ctxt->nameNr > xmlParserMaxDepth) && 77@@ -10067,7 +10067,7 @@ xmlParseElementStart(xmlParserCtxtPtr ctxt) { 78 return(-1); 79 } 80 nameNsPush(ctxt, name, prefix, URI, line, ctxt->nsNr - nsNr); 81- ret = ctxt->node; 82+ cur = ctxt->node; 83 84 #ifdef LIBXML_VALID_ENABLED 85 /* 86@@ -10100,17 +10100,23 @@ xmlParseElementStart(xmlParserCtxtPtr ctxt) { 87 spacePop(ctxt); 88 if (nsNr != ctxt->nsNr) 89 nsPop(ctxt, ctxt->nsNr - nsNr); 90- if ( ret != NULL && ctxt->record_info ) { 91- node_info.end_pos = ctxt->input->consumed + 92- (CUR_PTR - ctxt->input->base); 93- node_info.end_line = ctxt->input->line; 94- node_info.node = ret; 95- xmlParserAddNodeInfo(ctxt, &node_info); 96+ if (cur != NULL && ctxt->record_info) { 97+ node_info.node = cur; 98+ node_info.end_pos = ctxt->input->consumed + 99+ (CUR_PTR - ctxt->input->base); 100+ node_info.end_line = ctxt->input->line; 101+ xmlParserAddNodeInfo(ctxt, &node_info); 102 } 103 return(1); 104 } 105 if (RAW == '>') { 106 NEXT1; 107+ if (cur != NULL && ctxt->record_info) { 108+ node_info.node = cur; 109+ node_info.end_pos = 0; 110+ node_info.end_line = 0; 111+ xmlParserAddNodeInfo(ctxt, &node_info); 112+ } 113 } else { 114 xmlFatalErrMsgStrIntStr(ctxt, XML_ERR_GT_REQUIRED, 115 "Couldn't find end of Start Tag %s line %d\n", 116@@ -10124,17 +10130,6 @@ xmlParseElementStart(xmlParserCtxtPtr ctxt) { 117 spacePop(ctxt); 118 if (nsNr != ctxt->nsNr) 119 nsPop(ctxt, ctxt->nsNr - nsNr); 120- 121- /* 122- * Capture end position and add node 123- */ 124- if ( ret != NULL && ctxt->record_info ) { 125- node_info.end_pos = ctxt->input->consumed + 126- (CUR_PTR - ctxt->input->base); 127- node_info.end_line = ctxt->input->line; 128- node_info.node = ret; 129- xmlParserAddNodeInfo(ctxt, &node_info); 130- } 131 return(-1); 132 } 133 134@@ -10149,8 +10144,7 @@ xmlParseElementStart(xmlParserCtxtPtr ctxt) { 135 */ 136 static void 137 xmlParseElementEnd(xmlParserCtxtPtr ctxt) { 138- xmlParserNodeInfo node_info; 139- xmlNodePtr ret = ctxt->node; 140+ xmlNodePtr cur = ctxt->node; 141 142 if (ctxt->nameNr <= 0) 143 return; 144@@ -10168,14 +10162,17 @@ xmlParseElementEnd(xmlParserCtxtPtr ctxt) { 145 #endif /* LIBXML_SAX1_ENABLED */ 146 147 /* 148- * Capture end position and add node 149+ * Capture end position 150 */ 151- if ( ret != NULL && ctxt->record_info ) { 152- node_info.end_pos = ctxt->input->consumed + 153- (CUR_PTR - ctxt->input->base); 154- node_info.end_line = ctxt->input->line; 155- node_info.node = ret; 156- xmlParserAddNodeInfo(ctxt, &node_info); 157+ if (cur != NULL && ctxt->record_info) { 158+ xmlParserNodeInfoPtr node_info; 159+ 160+ node_info = (xmlParserNodeInfoPtr) xmlParserFindNodeInfo(ctxt, cur); 161+ if (node_info != NULL) { 162+ node_info->end_pos = ctxt->input->consumed + 163+ (CUR_PTR - ctxt->input->base); 164+ node_info->end_line = ctxt->input->line; 165+ } 166 } 167 } 168 169-- 1702.27.0 171 172