diff options
author | Nick Wellnhofer <wellnhofer@aevum.de> | 2021-02-20 20:30:43 +0100 |
---|---|---|
committer | Nick Wellnhofer <wellnhofer@aevum.de> | 2021-02-20 21:28:56 +0100 |
commit | dcb80b92da0417bc5b3d97ab8a61381973f1711b (patch) | |
tree | 39bd9c841048d67e00f16a5024c77e7e2e026da2 /parserInternals.c | |
parent | 02bee4c41444fa5c50e3b9ad5f851656b6d0b086 (diff) | |
download | libxml2-dcb80b92da0417bc5b3d97ab8a61381973f1711b.tar.gz |
Fix slow parsing of HTML with encoding errors
Under certain circumstances, the HTML parser would try to guess and
switch input encodings multiple times, leading to slow processing of
documents with encoding errors. The repeated scanning of the input
buffer when guessing encodings could even lead to quadratic behavior.
The code htmlCurrentChar probably assumed that if there's an encoding
handler, it is guaranteed to produce valid UTF-8. This holds true in
general, but if the detected encoding was "UTF-8", the UTF8ToUTF8
encoding handler simply invoked memcpy without checking for invalid
UTF-8. This still must be fixed, preferably by not using this handler
at all.
Also leave a note that switching encodings twice seems impossible to
implement correctly. Add a check when handling UTF-8 encoding errors
in htmlCurrentChar to avoid this situation, even if encoders produce
invalid UTF-8.
Found by OSS-Fuzz.
Diffstat (limited to 'parserInternals.c')
-rw-r--r-- | parserInternals.c | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/parserInternals.c b/parserInternals.c index b0629ef3..cbcfde0e 100644 --- a/parserInternals.c +++ b/parserInternals.c @@ -1153,6 +1153,11 @@ xmlSwitchInputEncodingInt(xmlParserCtxtPtr ctxt, xmlParserInputPtr input, * Note: this is a bit dangerous, but that's what it * takes to use nearly compatible signature for different * encodings. + * + * FIXME: Encoders might buffer partial byte sequences, so + * this probably can't work. We should return an error and + * make sure that callers never try to switch the encoding + * twice. */ xmlCharEncCloseFunc(input->buf->encoder); input->buf->encoder = handler; |