LibXML2 - crash on read source

New to FreeBASIC? Post your questions here.
Post Reply
Iczer
Posts: 99
Joined: Jul 04, 2017 18:09

LibXML2 - crash on read source

Post by Iczer »

I'm trying to make RSS XML parser using LibXML2, but have strange crashes:
if i get RSS XML from feed (with curl function or manually from firefox) and write it to file on hdd - then libxml reader working as expected, without any problems. BUT if i pass same source (zstring ptr) directly from curl function to libxml reader - i'm getting crash.

I tried 5 different libxml functions to get xmlDocPtr and they all crash on source from i-net and work OK on source from hdd...

Code: Select all

Function XMLRSSParse(sXMLRawData As ZString Ptr, aRSSItemsArray() As RSSItem) As Integer
	
	Print "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
	Print *sXMLRawData
	Print "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
	Print "Len(*sXMLRawData) = ";Len(*sXMLRawData)
	Print "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
	Print " sXMLRawData Ptr = ";sXMLRawData
	Print "$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$"
	
	' ----------------------------------------------------------------------------------------
	xmlInitParser()
	xmlXPathInit()
	xmlKeepBlanksDefault(0)
	' variant 1 (crash) ----------------------------------------------------------------------------------------
	'Dim As xmlParserCtxtPtr ctxt_ptr = xmlNewParserCtxt()
	'Dim As xmlDocPtr RSS = xmlCtxtReadMemory( ctxt_ptr, *sXMLRawData, Len(*sXMLRawData), "RSS Feed.xml", NULL, XML_PARSE_NOBLANKS)
	' variant 2 (crash) ----------------------------------------------------------------------------------------
	'Dim As xmlDocPtr RSS = xmlRecoverDoc(sXMLRawData)
	' variant 3 (crash) ----------------------------------------------------------------------------------------
	'Dim As xmlDocPtr RSS = xmlParseMemory(sXMLRawData, Len(*sXMLRawData))
	' variant 4 (crash) ----------------------------------------------------------------------------------------
	'Dim As xmlDocPtr RSS = htmlReadMemory(sXMLRawData, Len(*sXMLRawData), "RSS Feed.xml", NULL, HTML_PARSE_RECOVER + HTML_PARSE_NOBLANKS)
	' variant 5 (crash) ----------------------------------------------------------------------------------------
	Dim As xmlDocPtr RSS = xmlReadDoc(sXMLRawData, "RSS Feed.xml", NULL, XML_PARSE_NOBLANKS)
	' ----------------------------------------------------------------------------------------
	If RSS = NULL Then
		Return 404001
	EndIf
	' ----------------------------------------------------------------------------------------
	Dim As xmlXPathContextPtr xpathCtx = xmlXPathNewContext(RSS)
	If xpathCtx = NULL Then
		xmlFreeDoc(RSS)
		Return 404002
	EndIf
	' ----------------------------------------------------------------------------------------
	' ... xml parsing
	' ----------------------------------------------------------------------------------------
	Function = 0
End Function
XML is readen from hdd like this:

Code: Select all

Dim As Integer RetValue, FileInstance
Dim aRSSItemsArray() As RSSItem
Dim As ZString Ptr sXMLRawData

FileInstance = FreeFile
Open sPathToXML For Input As FileInstance
If Err <> 0 Then
	Return 4040101
EndIf

sXMLRawData = Callocate(Lof(FileInstance) + 1,1)
*sXMLRawData = String(Lof(FileInstance)+1,32)

RetValue = Get (#FileInstance, 1, *sXMLRawData)
Close #FileInstance
If RetValue <> 0 Then
	Return 4040102
EndIf

RetValue = XMLRSSParse(sXMLRawData, aRSSItemsArray())
If RetValue <> 0 Then
	DeAllocate(sXMLRawData)
	Return RetValue
EndIf
curl callback function:

Code: Select all

Type tagCallBackBuffer2
	strbuf As zString Ptr'""
	size As size_t = 0
End Type


Function CallbackFunc_HTMLSource2 Cdecl(ByVal buffer As any Ptr, ByVal size As size_t, ByVal nitems As size_t, ByVal zBuffer As tagCallBackBuffer2 Ptr) As size_t
	
	Dim As size_t bytes = size * nitems
	
	zBuffer->strbuf = Reallocate(zBuffer->strbuf, zBuffer->size + bytes + 1 )
	
	Clear(zBuffer->strbuf[zBuffer->size],32,bytes + 1)
	
	memcpy(zBuffer->strbuf + zBuffer->size, buffer, bytes)
	zBuffer->size += bytes
	zBuffer->strbuf[zBuffer->size] = 0
	
	Return bytes
End Function
maybe someone knowledgeable can share some light on libxml behavior...
Post Reply