Skip to content

Commit 105fe07

Browse files
committed
Update ext/libxml APIs so that non-libxml users can hook into the error mechanism
1 parent 767fa98 commit 105fe07

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

ext/libxml/libxml.c

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ static void _php_libxml_free_error(void *ptr)
598598
xmlResetError((xmlErrorPtr) ptr);
599599
}
600600

601-
static void _php_list_set_error_structure(xmlErrorPtr error, const char *msg)
601+
static void _php_list_set_error_structure(xmlErrorPtr error, const char *msg, int line, int column)
602602
{
603603
xmlError error_copy;
604604
int ret;
@@ -611,6 +611,8 @@ static void _php_list_set_error_structure(xmlErrorPtr error, const char *msg)
611611
} else {
612612
error_copy.code = XML_ERR_INTERNAL_ERROR;
613613
error_copy.level = XML_ERR_ERROR;
614+
error_copy.line = line;
615+
error_copy.int2 = column;
614616
error_copy.message = (char*)xmlStrdup((const xmlChar*)msg);
615617
ret = 0;
616618
}
@@ -620,17 +622,17 @@ static void _php_list_set_error_structure(xmlErrorPtr error, const char *msg)
620622
}
621623
}
622624

623-
static void php_libxml_ctx_error_level(int level, void *ctx, const char *msg)
625+
static void php_libxml_ctx_error_level(int level, void *ctx, const char *msg, int line)
624626
{
625627
xmlParserCtxtPtr parser;
626628

627629
parser = (xmlParserCtxtPtr) ctx;
628630

629631
if (parser != NULL && parser->input != NULL) {
630632
if (parser->input->filename) {
631-
php_error_docref(NULL, level, "%s in %s, line: %d", msg, parser->input->filename, parser->input->line);
633+
php_error_docref(NULL, level, "%s in %s, line: %d", msg, parser->input->filename, line);
632634
} else {
633-
php_error_docref(NULL, level, "%s in Entity, line: %d", msg, parser->input->line);
635+
php_error_docref(NULL, level, "%s in Entity, line: %d", msg, line);
634636
}
635637
} else {
636638
php_error_docref(NULL, E_WARNING, "%s", msg);
@@ -640,13 +642,13 @@ static void php_libxml_ctx_error_level(int level, void *ctx, const char *msg)
640642
void php_libxml_issue_error(int level, const char *msg)
641643
{
642644
if (LIBXML(error_list)) {
643-
_php_list_set_error_structure(NULL, msg);
645+
_php_list_set_error_structure(NULL, msg, 0, 0);
644646
} else {
645647
php_error_docref(NULL, level, "%s", msg);
646648
}
647649
}
648650

649-
static void php_libxml_internal_error_handler(int error_type, void *ctx, const char **msg, va_list ap)
651+
static void php_libxml_internal_error_handler_ex(int error_type, void *ctx, const char **msg, va_list ap, int line, int column)
650652
{
651653
char *buf;
652654
int len, len_iter, output = 0;
@@ -666,15 +668,15 @@ static void php_libxml_internal_error_handler(int error_type, void *ctx, const c
666668

667669
if (output == 1) {
668670
if (LIBXML(error_list)) {
669-
_php_list_set_error_structure(NULL, ZSTR_VAL(LIBXML(error_buffer).s));
671+
_php_list_set_error_structure(NULL, ZSTR_VAL(LIBXML(error_buffer).s), line, column);
670672
} else if (!EG(exception)) {
671673
/* Don't throw additional notices/warnings if an exception has already been thrown. */
672674
switch (error_type) {
673675
case PHP_LIBXML_CTX_ERROR:
674-
php_libxml_ctx_error_level(E_WARNING, ctx, ZSTR_VAL(LIBXML(error_buffer).s));
676+
php_libxml_ctx_error_level(E_WARNING, ctx, ZSTR_VAL(LIBXML(error_buffer).s), line);
675677
break;
676678
case PHP_LIBXML_CTX_WARNING:
677-
php_libxml_ctx_error_level(E_NOTICE, ctx, ZSTR_VAL(LIBXML(error_buffer).s));
679+
php_libxml_ctx_error_level(E_NOTICE, ctx, ZSTR_VAL(LIBXML(error_buffer).s), line);
678680
break;
679681
default:
680682
php_error_docref(NULL, E_WARNING, "%s", ZSTR_VAL(LIBXML(error_buffer).s));
@@ -684,6 +686,19 @@ static void php_libxml_internal_error_handler(int error_type, void *ctx, const c
684686
}
685687
}
686688

689+
static void php_libxml_internal_error_handler(int error_type, void *ctx, const char **msg, va_list ap)
690+
{
691+
int line = 0;
692+
int column = 0;
693+
xmlParserCtxtPtr parser = (xmlParserCtxtPtr) ctx;
694+
/* Context is not valid for PHP_LIBXML_ERROR, don't dereference it in that case */
695+
if (error_type != PHP_LIBXML_ERROR && parser != NULL && parser->input != NULL) {
696+
line = parser->input->line;
697+
column = parser->input->col;
698+
}
699+
php_libxml_internal_error_handler_ex(error_type, ctx, msg, ap, line, column);
700+
}
701+
687702
static xmlParserInputPtr _php_libxml_external_entity_loader(const char *URL,
688703
const char *ID, xmlParserCtxtPtr context)
689704
{
@@ -813,6 +828,14 @@ static xmlParserInputPtr _php_libxml_pre_ext_ent_loader(const char *URL,
813828
}
814829
}
815830

831+
PHP_LIBXML_API void php_libxml_pretend_ctx_error_ex(int line, int column, const char *msg,...)
832+
{
833+
va_list args;
834+
va_start(args, msg);
835+
php_libxml_internal_error_handler_ex(PHP_LIBXML_CTX_ERROR, NULL, &msg, args, line, column);
836+
va_end(args);
837+
}
838+
816839
PHP_LIBXML_API void php_libxml_ctx_error(void *ctx, const char *msg, ...)
817840
{
818841
va_list args;
@@ -831,7 +854,7 @@ PHP_LIBXML_API void php_libxml_ctx_warning(void *ctx, const char *msg, ...)
831854

832855
static void php_libxml_structured_error_handler(void *userData, xmlErrorPtr error)
833856
{
834-
_php_list_set_error_structure(error, NULL);
857+
_php_list_set_error_structure(error, NULL, 0, 0);
835858

836859
return;
837860
}

ext/libxml/php_libxml.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ PHP_LIBXML_API void php_libxml_node_free_resource(xmlNodePtr node);
129129
PHP_LIBXML_API void php_libxml_node_decrement_resource(php_libxml_node_object *object);
130130
PHP_LIBXML_API void php_libxml_error_handler(void *ctx, const char *msg, ...);
131131
PHP_LIBXML_API void php_libxml_ctx_warning(void *ctx, const char *msg, ...);
132+
PHP_LIBXML_API void php_libxml_pretend_ctx_error_ex(int line, int column, const char *msg,...);
132133
PHP_LIBXML_API void php_libxml_ctx_error(void *ctx, const char *msg, ...);
133134
PHP_LIBXML_API int php_libxml_xmlCheckUTF8(const unsigned char *s);
134135
PHP_LIBXML_API void php_libxml_switch_context(zval *context, zval *oldcontext);

0 commit comments

Comments
 (0)