Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit bf43e8a

Browse files
committed
io: Fix return value of xmlFileRead
This broke in commit 6d27c54. Fixes #766.
1 parent e30cb63 commit bf43e8a

3 files changed

Lines changed: 33 additions & 2 deletions

File tree

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ if(LIBXML2_WITH_TESTS)
528528
endif()
529529
add_test(NAME testchar COMMAND testchar)
530530
add_test(NAME testdict COMMAND testdict)
531-
add_test(NAME testparser COMMAND testparser)
531+
add_test(NAME testparser COMMAND testparser WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
532532
add_test(NAME testrecurse COMMAND testrecurse WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
533533
add_test(NAME testThreads COMMAND testThreads WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
534534
endif()

testparser.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
* See Copyright for the status of this software.
55
*/
66

7+
#define XML_DEPRECATED
8+
79
#include <libxml/parser.h>
810
#include <libxml/uri.h>
911
#include <libxml/xmlreader.h>
@@ -95,6 +97,34 @@ testNodeGetContent(void) {
9597
return err;
9698
}
9799

100+
static int
101+
testCFileIO(void) {
102+
xmlDocPtr doc;
103+
int err = 0;
104+
105+
/* Deprecated FILE-based API */
106+
xmlRegisterInputCallbacks(xmlFileMatch, xmlFileOpen, xmlFileRead,
107+
xmlFileClose);
108+
doc = xmlReadFile("test/ent1", NULL, 0);
109+
110+
if (doc == NULL) {
111+
err = 1;
112+
} else {
113+
xmlNodePtr root = xmlDocGetRootElement(doc);
114+
115+
if (root == NULL || !xmlStrEqual(root->name, BAD_CAST "EXAMPLE"))
116+
err = 1;
117+
}
118+
119+
xmlFreeDoc(doc);
120+
xmlPopInputCallbacks();
121+
122+
if (err)
123+
fprintf(stderr, "xmlReadFile failed with FILE input callbacks\n");
124+
125+
return err;
126+
}
127+
98128
#ifdef LIBXML_SAX1_ENABLED
99129
static int
100130
testBalancedChunk(void) {
@@ -540,6 +570,7 @@ main(void) {
540570
err |= testStandaloneWithEncoding();
541571
err |= testUnsupportedEncoding();
542572
err |= testNodeGetContent();
573+
err |= testCFileIO();
543574
#ifdef LIBXML_SAX1_ENABLED
544575
err |= testBalancedChunk();
545576
#endif

xmlIO.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -776,7 +776,7 @@ xmlFileRead(void * context, char * buffer, int len) {
776776
if ((bytes < (size_t) len) && (ferror(file)))
777777
return(-xmlIOErr(0, "fread()"));
778778

779-
return(len);
779+
return(bytes);
780780
}
781781

782782
#ifdef LIBXML_OUTPUT_ENABLED

0 commit comments

Comments
 (0)