Skip to content

Commit 110e44e

Browse files
committed
SAX2: Fix xmlSAX2ResolveEntity if systemId is NULL
Passing a NULL systemId results in snprintf("%s", NULL) which crashes on some platforms. Regressed with commit 4ff2dcc. Note that systemId should never be NULL during normal parsing. It can only be NULL if API functions are called with a NULL systemId. Should fix #825.
1 parent 2b542ee commit 110e44e

File tree

1 file changed

+36
-30
lines changed

1 file changed

+36
-30
lines changed

SAX2.c

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -404,42 +404,48 @@ xmlSAX2ResolveEntity(void *ctx, const xmlChar *publicId, const xmlChar *systemId
404404
{
405405
xmlParserCtxtPtr ctxt = (xmlParserCtxtPtr) ctx;
406406
xmlParserInputPtr ret = NULL;
407-
xmlChar *URI;
408-
const xmlChar *base = NULL;
409-
int res;
407+
xmlChar *URI = NULL;
410408

411409
if (ctx == NULL) return(NULL);
412-
if (ctxt->input != NULL)
413-
base = BAD_CAST ctxt->input->filename;
414410

415-
/*
416-
* We don't really need the 'directory' struct member, but some
417-
* users set it manually to a base URI for memory streams.
418-
*/
419-
if (base == NULL)
420-
base = BAD_CAST ctxt->directory;
411+
if (systemId != NULL) {
412+
const xmlChar *base = NULL;
413+
int res;
421414

422-
if ((xmlStrlen(systemId) > XML_MAX_URI_LENGTH) ||
423-
(xmlStrlen(base) > XML_MAX_URI_LENGTH)) {
424-
xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, "URI too long");
425-
return(NULL);
426-
}
427-
res = xmlBuildURISafe(systemId, base, &URI);
428-
if (URI == NULL) {
429-
if (res < 0)
430-
xmlSAX2ErrMemory(ctxt);
431-
else
432-
xmlWarnMsg(ctxt, XML_ERR_INVALID_URI,
433-
"Can't resolve URI: %s\n", systemId);
434-
return(NULL);
435-
}
436-
if (xmlStrlen(URI) > XML_MAX_URI_LENGTH) {
437-
xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, "URI too long");
438-
} else {
439-
ret = xmlLoadExternalEntity((const char *) URI,
440-
(const char *) publicId, ctxt);
415+
if (ctxt->input != NULL)
416+
base = BAD_CAST ctxt->input->filename;
417+
418+
/*
419+
* We don't really need the 'directory' struct member, but some
420+
* users set it manually to a base URI for memory streams.
421+
*/
422+
if (base == NULL)
423+
base = BAD_CAST ctxt->directory;
424+
425+
if ((xmlStrlen(systemId) > XML_MAX_URI_LENGTH) ||
426+
(xmlStrlen(base) > XML_MAX_URI_LENGTH)) {
427+
xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, "URI too long");
428+
return(NULL);
429+
}
430+
res = xmlBuildURISafe(systemId, base, &URI);
431+
if (URI == NULL) {
432+
if (res < 0)
433+
xmlSAX2ErrMemory(ctxt);
434+
else
435+
xmlWarnMsg(ctxt, XML_ERR_INVALID_URI,
436+
"Can't resolve URI: %s\n", systemId);
437+
return(NULL);
438+
}
439+
if (xmlStrlen(URI) > XML_MAX_URI_LENGTH) {
440+
xmlFatalErr(ctxt, XML_ERR_RESOURCE_LIMIT, "URI too long");
441+
xmlFree(URI);
442+
return(NULL);
443+
}
441444
}
442445

446+
ret = xmlLoadExternalEntity((const char *) URI,
447+
(const char *) publicId, ctxt);
448+
443449
xmlFree(URI);
444450
return(ret);
445451
}

0 commit comments

Comments
 (0)