1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2024-10-26 12:25:09 +03:00
Commit Graph

333 Commits

Author SHA1 Message Date
Nick Wellnhofer
b9db3d7d02 parser: Simplify xmlStringCurrentChar
Start to move away from using this function.
2023-09-22 19:01:11 +02:00
Nick Wellnhofer
8c084ebdc7 doc: Make apibuild.py happy 2023-09-21 22:57:33 +02:00
Nick Wellnhofer
9b5cce7a71 include: Remove more unnecessary includes 2023-09-21 01:50:53 +02:00
Nick Wellnhofer
699299cae3 globals: Stop including globals.h 2023-09-20 22:07:40 +02:00
Nick Wellnhofer
11a1839ddd globals: Move remaining globals back to correct header files
This undoes a lot of damage.
2023-09-20 22:06:49 +02:00
Nick Wellnhofer
4e1c13ebfd debug: Remove debugging code
This is barely useful these days and only clutters the code base.
2023-09-19 17:35:09 +02:00
Nick Wellnhofer
e48f2695fe parser: Remove push parser debugging code 2023-08-29 18:17:09 +02:00
Nick Wellnhofer
ed3bd05284 parser: Allow to set maximum amplification factor 2023-08-20 20:49:16 +02:00
Nick Wellnhofer
f1c1f5c6b4 parser: Revert change to doc->encoding
Fixes #579.
2023-08-17 12:47:14 +02:00
Nick Wellnhofer
61b8e097b9 parser: Never use UTF-8 encoding handler 2023-08-16 19:50:36 +02:00
Nick Wellnhofer
b973ceaf2f parser: Fix mistake in xmlDetectEncoding
Short-lived regression.
2023-08-09 18:40:25 +02:00
Nick Wellnhofer
95e81a360c parser: Decode all data in xmlCharEncInput
Even with flush set to true, xmlCharEncInput didn't guarantee to decode
all data. This complicated the push parser.

Remove the flush flag and always decode all available data.

Also fix ICU code where the flush flag has a different meaning. Always
set flush to false and retry even with empty input buffers.
2023-08-08 15:21:31 +02:00
Nick Wellnhofer
834b8123ef parser: Stream data when reading from memory
Don't create a copy of the whole input buffer. Read the data chunk by
chunk to save memory.

Historically, it was probably envisioned to read data from memory
without additional copying. This doesn't work reliably with the current
design of the XML parser which requires a terminating null byte at the
end of input buffers. This lead to xmlReadMemory interfaces, which
expect pointer and size arguments, being changed to make a
zero-terminated copy of the input buffer. Interfaces based on
xmlReadDoc, which actually expect a zero-terminated string and
would make zero-copy operation work, were then simplified to rely on
xmlReadMemoryi, resulting in an unnecessary copy.

To avoid copying (possibly gigabytes) of memory temporarily, we now
stream in-memory input just like content read from files in a
chunk-by-chunk fashion (using a somewhat outdated INPUT_CHUNK size of
250 bytes). As a side effect, we also avoid another copy of the whole
input when handling non-UTF-8 data which was made possible by some
earlier commits.

Interfaces expecting zero-terminated strings now make use of strnlen
which unfortunately isn't part of the standard C library and only
mandated since POSIX 2008.
2023-08-08 15:21:28 +02:00
Nick Wellnhofer
59fa0bb383 parser: Simplify input pointer updates
The base member always points to the beginning of the buffer.
2023-08-08 15:21:14 +02:00
Nick Wellnhofer
4ee0815514 encoding: Move rawconsumed accounting to xmlCharEncInput 2023-08-08 15:19:51 +02:00
Nick Wellnhofer
ec7be50662 parser: Rework encoding detection
Introduce XML_INPUT_HAS_ENCODING flag for xmlParserInput which is set
when xmlSwitchEncoding is called. The parser can use the flag to
reliably detect whether an encoding was already set via user override,
BOM or other auto-detection. In this case, the encoding declaration
won't be used to switch the encoding.

Before, an inscrutable mix of ctxt->charset, ctxt->input->encoding
and ctxt->input->buf->encoder was used.

Introduce private helper functions to switch encodings used by both the
XML and HTML parser:

- xmlDetectEncoding which skips over the BOM, allowing to remove the
  BOM checks from other encoding functions.
- xmlSetDeclaredEncoding, replacing htmlCheckEncodingDirect, which warns
  about encoding mismatches.

If users override the encoding, store the declared instead of the actual
encoding in xmlDoc. In this case, the actual encoding is known and the
raw value from the doc is more useful.

Also use the input flags to store the ISO-8859-1 fallback state.
Restrict the fallback to cases where no encoding was specified. (The
fallback is only useful in recovery mode and these days broken UTF-8 is
probably more likely than ISO-8859-1, so it might eventually be removed
completely.)

The 'charset' member of xmlParserCtxt is now unused. The 'encoding'
member of xmlParserInput is now unused.

The 'standalone' member of xmlParserInput is renamed to 'flags'.

A new parser state XML_PARSER_XML_DECL is added for the push parser.
2023-08-08 15:19:46 +02:00
Nick Wellnhofer
131d0dc0a7 parser: Don't use 'standalone' member of xmlParserInput
The standalone declaration is only parsed in the main input stream.
2023-08-08 15:19:39 +02:00
Nick Wellnhofer
8844744772 parser: Fix typo in previous commit 2023-06-23 23:04:30 +02:00
Nick Wellnhofer
9d0541dd2f parser: Make xmlSwitchEncoding always skip the BOM
Chromium calls xmlSwitchEncoding from the start document handler and
relies on this function to skip the BOM. Commit 98840d40 changed the
behavior when switching to UTF-16 since inspecting the input buffer at
this point is fragile.

Revert part of the commit to also skip a potential (decoded UTF-8) BOM
when switching to UTF-16. Make sure that we do this only at the start of
an input stream to avoid U-FEFF characters being lost.

BOM handling should ultimately be moved to the parsing code to avoid
such bugs.

See https://bugs.chromium.org/p/chromium/issues/detail?id=1451026
2023-06-22 18:22:32 +02:00
Nick Wellnhofer
b236b7a588 parser: Halt parser when growing buffer results in OOM
Fix short-lived regression from previous commit.

It might be safer to make xmlBufSetInputBaseCur use the original buffer
even in case of errors.

Found by OSS-Fuzz.
2023-06-08 21:59:20 +02:00
Nick Wellnhofer
20f5c73457 parser: Recover more input from encoding errors
Don't halt the parser in xmlParserGrow to allow more input to be
recovered in case of encoding errors.

Fixes #543.
2023-06-07 14:05:34 +02:00
Nick Wellnhofer
e0f3016f71 parser: Fix regression when push parsing UTF-8 sequences
Partial UTF-8 sequences are allowed when push parsing.

Fixes #542.
2023-05-18 18:21:20 +02:00
Nick Wellnhofer
9dae389cee parser: Fix "huge input lookup" error with push parser
Fix parsing of larger documents without XML_PARSE_HUGE.

Should fix #538.
2023-05-09 13:30:21 +02:00
Nick Wellnhofer
320f5084cd parser: Improve handling of encoding and IO errors
Make sure that xmlCharEncInput, xmlParserInputBufferPush and
xmlParserInputBufferGrow set the correct error code in the
xmlParserInputBuffer. Handle errors when calling these functions.
2023-04-30 21:31:54 +02:00
Nick Wellnhofer
fc69cf568b parser: Move xmlFatalErr to parserInternals.c 2023-04-30 17:51:29 +02:00
Nick Wellnhofer
3ffcc03b16 parser: Deprecate more internal functions 2023-04-26 20:23:23 +02:00
Nick Wellnhofer
9282b08431 parser: Fix regression in memory pull parser with encoding
Revert another change from commit 98840d40.

Decode the whole buffer when reading from memory and switching to the
initial encoding. Add some comments about potential improvements.
2023-04-19 22:32:19 +02:00
Nick Wellnhofer
a19fa11e1d parser: Fix regression when switching input encodings
Revert some changes from commit 98840d40.

WebKit/Chromium can actually switch from ISO-8859-1 to UTF-16 in the
middle of parsing. This is a bad idea, but we have to keep supporting
this use case.
2023-04-13 15:20:56 +02:00
Nick Wellnhofer
921796b06b parser: Don't grow push parser buffers
This should fix a short-lived regression when push parsing with
encodings.
2023-04-12 13:56:33 +02:00
Nick Wellnhofer
0e42adce77 parser: Halt parser if switching encodings fails
Avoids buffer overread in htmlParseHTMLAttribute.

Found by OSS-Fuzz.
2023-03-30 14:09:15 +02:00
Nick Wellnhofer
3660229219 parser: Fix buffer overread in xmlDetectEBCDIC
Short-lived regression found by OSS-Fuzz.
2023-03-26 14:11:31 +02:00
Nick Wellnhofer
7fbd454d9f parser: Grow input buffer earlier when reading characters
Make more bytes available after invoking CUR_CHAR or NEXT.
2023-03-21 21:35:53 +01:00
Nick Wellnhofer
98840d40da parser: Rework EBCDIC code page detection
To detect EBCDIC code pages, we used to switch the encoding twice and
had to be very careful not to decode data after the XML declaration
before the second switch. This relied on a hard-coded expected size of
the XML declaration and was complicated and unreliable.

Now we convert the first 200 bytes to EBCDIC-US and parse the encoding
declaration manually.
2023-03-21 21:35:15 +01:00
Nick Wellnhofer
04d1bedd8c parser: Rework shrinking of input buffers
Don't try to grow the input buffer in xmlParserShrink. This makes sure
that no memory allocations are made and the function always succeeds.

Remove unnecessary invocations of SHRINK. Invoke SHRINK at the end of
DTD parsing loops.

Shrink before growing.
2023-03-21 13:19:18 +01:00
Nick Wellnhofer
1a91392c62 parser: More fixes to xmlParserGrow
xmlHaltParser must be called after reporting an error. Switch to
xmlBufSetInputBaseCur.
2023-03-16 17:48:57 +01:00
Nick Wellnhofer
ca2bfecea9 malloc-fail: Fix buffer overread when reading from input
Found by OSS-Fuzz, see #344.
2023-03-15 17:34:32 +01:00
Nick Wellnhofer
b167c73144 parser: Fix short-lived regression causing infinite loops
Fix 3eb6bf03. We really have to halt the parser, so the input buffer
gets reset.
2023-03-14 15:16:04 +01:00
Nick Wellnhofer
e7c3a4ca1b parser: Deprecate some parser input functions 2023-03-13 19:19:46 +01:00
Nick Wellnhofer
2099441f32 parser: Stop calling xmlParserInputShrink
Introduce xmlParserShrink which takes a parser context to simplify error
handling.
2023-03-13 17:51:13 +01:00
Nick Wellnhofer
457fc622d5 malloc-fail: Fix null deref in xmlParserInputShrink
Found by OSS-Fuzz.
2023-03-13 16:54:16 +01:00
Nick Wellnhofer
3eb6bf0386 parser: Stop calling xmlParserInputGrow
Introduce xmlParserGrow which takes a parser context to simplify error
handling.
2023-03-12 17:05:51 +01:00
Nick Wellnhofer
2355eac59e malloc-fail: Fix null deref if growing input buffer fails
Also add some error checks.

Found with libFuzzer, see #344.
2023-01-24 11:32:15 +01:00
Nick Wellnhofer
077df27eb1 parser: Fix integer overflow of input ID
Applies a patch from Chromium. Also stop incrementing input ID of
subcontexts. This isn't necessary.

Fixes #465.
2022-12-22 15:22:01 +01:00
Nick Wellnhofer
ce76ebfd13 entities: Stop counting entities
This was only used in the old version of xmlParserEntityCheck.
2022-12-21 20:19:10 +01:00
Nick Wellnhofer
463bbeeca1 entities: Rework entity amplification checks
This commit implements robust detection of entity amplification attacks,
better known as the "billion laughs" attack.

We now limit the size of the document after substitution of entities to
10 times the size before expansion. This guarantees linear behavior by
definition. There already was a similar check before, but the accounting
of "sizeentities" (size of external entities) and "sizeentcopy" (size of
all copies created by entity references) wasn't accurate.

We also need saturation arithmetic since we're historically limited to
"unsigned long" which is 32-bit on many platforms.

A maximum of 10 MB of substitutions is always allowed. This should make
use cases like DITA work which have caused problems in the past.

The old checks based on the number of entities were removed. This is
accounted for by adding a fixed cost to each entity reference.

Entity amplification checks are now enabled even if XML_PARSE_HUGE is
set. This option is mainly used to allow larger text nodes. Most users
were unaware that it also disabled entity expansion checks.

Some of the limits might be adjusted later. If this change turns out to
affect legitimate use cases, we can add a separate parser option to
disable the checks.

Fixes #294.
Fixes #345.
2022-12-21 20:19:10 +01:00
Nick Wellnhofer
a8b31e68c2 parser: Fix progress check when parsing character data
Skip over zero bytes to guarantee progress. Short-lived regression.
2022-11-21 21:39:10 +01:00
Nick Wellnhofer
691a771956 parser: Fix 'consumed' accounting when switching encodings 2022-11-20 21:27:59 +01:00
Nick Wellnhofer
249cee4b2a io: Fix a few integer overflows in I/O statistics
There are still many places where arithmetic on "consumed" stats isn't
checked for overflow, affecting platforms with a 32-bit long type.
2022-11-20 21:16:03 +01:00
Nick Wellnhofer
6b57061909 io: Rearrange code in xmlSwitchInputEncodingInt
No functional change.
2022-11-20 21:16:03 +01:00
Nick Wellnhofer
46cd7d224e io: Remove xmlInputReadCallbackNop
In some cases, for example when using encoders, the read callback was
set to NULL, in other cases it was set to xmlInputReadCallbackNop.
xmlGROW only tested for xmlInputReadCallbackNop, resulting in errors
when parsing large encoded content from memory.

Always use a NULL callback for memory buffers to avoid ambiguities.

Fixes #262.
2022-11-20 21:12:18 +01:00
Nick Wellnhofer
9feafbc5c5 io: Check for memory buffer early in xmlParserInputGrow 2022-11-13 18:08:34 +01:00
Nick Wellnhofer
6843fc726f Remove or annotate char casts 2022-09-01 04:31:30 +02:00
Nick Wellnhofer
ad338ca737 Remove explicit integer casts
Remove explicit integer casts as final operation

- in assignments
- when passing arguments
- when returning values

Remove casts

- to the same type
- from certain range-bound values

The main motivation is that these explicit casts don't change the result
of operations and only render UBSan's implicit-conversion checks
useless. Removing these casts allows UBSan to detect cases where
truncation or sign-changes occur unexpectedly.

Document some explicit casts as truncating and add a few missing ones.
2022-09-01 02:33:57 +02:00
Nick Wellnhofer
65dc8a63ac Make xmlNewSAXParserCtx take a const sax handler
Also improve documentation.
2022-09-01 00:17:45 +02:00
Nick Wellnhofer
0f568c0b73 Consolidate private header files
Private functions were previously declared

- in header files in the root directory
- in public headers guarded with IN_LIBXML
- in libxml.h
- redundantly in source files that used them.

Consolidate all private header files in include/private.
2022-08-26 02:11:56 +02:00
Nick Wellnhofer
ca3807d946 Mark more functions setting globals as deprecated 2022-08-24 16:16:09 +02:00
Nick Wellnhofer
fd85b566f7 Mark more parser functions as deprecated
No compiler warnings generated yet.
2022-08-24 15:12:24 +02:00
Nick Wellnhofer
9a82b94a94 Introduce xmlNewSAXParserCtxt and htmlNewSAXParserCtxt
Add API functions to create a parser context with a custom SAX handler
without having to mess with ctxt->sax manually.
2022-08-24 14:07:55 +02:00
Nick Wellnhofer
c21e9cd5d9 Use xmlStrlen in xmlNewStringInputStream
xmlStrlen handles buffers larger than INT_MAX more gracefully.
2022-08-20 17:03:10 +02:00
Nick Wellnhofer
b1b654171e Create stream with buffer in xmlNewStringInputStream
Create an input stream with a buffer in xmlNewStringInputStream.
Otherwise, switching encodings won't work.

See #34.
2022-08-20 16:34:08 +02:00
Nick Wellnhofer
aab584dc31 Clean up encoding switching code
- Remove xmlSwitchToEncodingInt which was basically just a wrapper
  around xmlSwitchInputEncodingInt.
- Simplify xmlSwitchEncoding.
- Improve error handling in xmlSwitchInputEncodingInt.
- Deprecate xmlSwitchInputEncoding.
2022-04-02 19:09:12 +02:00
Nick Wellnhofer
92bff86614 Fix calls to deprecated init/cleanup functions
Only use xmlInitParser/xmlCleanupParser.
2022-03-29 14:18:31 +02:00
Nick Wellnhofer
4951c462ea Avoid arithmetic on freed pointers 2022-03-06 02:29:00 +01:00
Nick Wellnhofer
ebb1797030 Remove unneeded #includes 2022-03-04 22:11:49 +01:00
Nick Wellnhofer
776d15d383 Don't check for standard C89 headers
Don't check for

- ctype.h
- errno.h
- float.h
- limits.h
- math.h
- signal.h
- stdarg.h
- stdlib.h
- string.h
- time.h

Stop including non-standard headers

- malloc.h
- strings.h
2022-03-02 00:43:54 +01:00
Nick Wellnhofer
2489c1d024 Remove useless __CYGWIN__ checks
From what I can tell, some really early Cygwin versions from around
1998-2000 used to erroneously define _WIN32. This was eventually fixed,
but these days, the `defined(_WIN32) && !defined(__CYGWIN__)` idiom is
unnecessary.

Now, we only check for __CYGWIN__ in xmlexports.h when deciding whether
to use __declspec.
2022-02-28 22:58:35 +01:00
Nick Wellnhofer
346c3a930c Remove elfgcchack.h
The same optimization can be enabled with -fno-semantic-interposition
since GCC 5. clang has always used this option by default.
2022-02-20 21:49:04 +01:00
Nick Wellnhofer
d7cb33cf44 Rework validation context flags
Use a bitmask instead of magic values to

- keep track whether the validation context is part of a parser context
- keep track whether xmlValidateDtdFinal was called

This allows to add addtional flags later.

Note that this deliberately changes the name of a public struct member,
assuming that this was always private data never to be used by client
code.
2022-02-20 21:49:04 +01:00
David King
328456bf29 Fix memory leak in xmlNewInputFromFile
Found by Coverity.

https://bugzilla.redhat.com/show_bug.cgi?id=1938806
2022-01-16 14:15:09 +01:00
Nick Wellnhofer
dcb80b92da 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.
2021-02-20 21:28:56 +01:00
Nick Wellnhofer
438e595a8c Stop counting nbChars in parser context
The value was inaccurate and never used.
2020-08-09 15:01:45 +02:00
Nick Wellnhofer
20c60886e4 Fix typos
Resolves #133.
2020-03-08 17:41:53 +01:00
Jared Yanovich
2a350ee9b4 Large batch of typo fixes
Closes #109.
2019-09-30 18:04:38 +02:00
Nick Wellnhofer
3776cb4745 Fix memory leak in xmlSwitchInputEncodingInt error path
Found by OSS-Fuzz.
2018-11-22 16:28:46 +01:00
Nick Wellnhofer
7a1bd7f649 Revert "Change calls to xmlCharEncInput to set flush false"
This reverts commit 6e6ae5daa6 which
broke decoding of larger documents with ICU.

See https://bugs.chromium.org/p/chromium/issues/detail?id=820163
2018-03-17 00:03:24 +01:00
Joel Hockey
6e6ae5daa6 Change calls to xmlCharEncInput to set flush false when not final call. Having flush incorrectly set to true causes errors for ICU. 2018-01-08 19:57:53 +01:00
Nick Wellnhofer
cb5541c9f3 Fix libz and liblzma detection
If libz or liblzma are detected with pkg-config, AC_CHECK_HEADERS must
not be run because the correct CPPFLAGS aren't set. It is actually not
required have separate checks for LIBXML_ZLIB_ENABLED and HAVE_ZLIB_H.
Only check for LIBXML_ZLIB_ENABLED and remove HAVE_ZLIB_H macro.

Fixes bug 764657, bug 787041.
2017-11-27 14:33:37 +01:00
Nick Wellnhofer
e03f0a199a Fix hash callback signatures
Make sure that all parameters and return values of hash callback
functions exactly match the callback function type. This is required
to pass clang's Control Flow Integrity checks and to allow compilation
to asm.js with Emscripten.

Fixes bug 784861.
2017-11-09 16:42:47 +01:00
Nick Wellnhofer
e3890546d7 Fix the Windows header mess
Don't include windows.h and wsockcompat.h from config.h but only when
needed.

Don't define _WINSOCKAPI_ manually. This was apparently done to stop
windows.h from including winsock.h which is a problem if winsock2.h
wasn't included first. But on MinGW, this causes compiler warnings.
Define WIN32_LEAN_AND_MEAN instead which has the same effect.

Always use the compiler-defined _WIN32 macro instead of WIN32.
2017-10-09 14:35:40 +02:00
Nick Wellnhofer
69936b129f Revert "Print error messages for truncated UTF-8 sequences"
This reverts commit 79c8a6b which caused a serious regression in
streaming mode.

Also reverts part of commit 52ceced "Fix infinite loops with push
parser in recovery mode".

Fixes bug 786554.
2017-08-30 14:19:06 +02:00
Daniel Veillard
f19385a589 Fix a couple of misleading indentation errors
Raised by gcc as potential error, no semantic change needed but
fixed the indentation
2017-08-28 20:40:19 +02:00
Nick Wellnhofer
3aca7f31cb Fix unwanted warnings when switching encodings
Revert part of commit 46dc989 "Don't switch encoding for internal
parameter entities" that caused spurious warnings.

Fixes bug 786267.
2017-08-21 13:09:33 +02:00
Nick Wellnhofer
453dff1e3b Remove unnecessary calls to xmlPopInput
It's enough if xmlPopInput is called from xmlSkipBlankChars. Since the
replacement text of a parameter entity is surrounded with space
characters, that's the only place where the replacement can end in a
well-formed document.

This is also required to get rid of the "blanks wrapper" hack.
2017-06-20 13:19:47 +02:00
Nick Wellnhofer
aa267cd127 Simplify handling of parameter entity references
There are only two places where parameter entity references must be
handled. For the internal subset in xmlParseInternalSubset. For the
external subset or content from other external PEs in xmlSkipBlankChars.

Make sure that xmlSkipBlankChars skips over sequences of PEs and
whitespace. Rely on xmlSkipBlankChars instead of calling
xmlParsePEReference directly when in the external subset or a
conditional section.

xmlParserHandlePEReference is unused now.
2017-06-20 13:19:47 +02:00
Nick Wellnhofer
46dc989080 Don't switch encoding for internal parameter entities
This is only needed for external entities. Trying to switch the encoding
for internal entities could also cause a memory leak in recovery mode.
2017-06-17 13:23:40 +02:00
Nick Wellnhofer
79c8a6b105 Print error messages for truncated UTF-8 sequences
Before, truncated UTF-8 sequences at the end of a file were treated as
EOF. Create an error message containing the offending bytes.

xmlStringCurrentChar would also print characters from the input stream,
not the string it's working on.
2017-06-10 18:11:58 +02:00
Nick Wellnhofer
f9e7997e80 Reset parser input pointers on encoding failure
Call xmlBufResetInput before bailing out if switching the encoding
fails. Otherwise, the input pointers are left in an invalid state.
This would typically lead to an internal error in xmlGROW but could also
cause other unforeseen problems.
2017-06-10 17:50:27 +02:00
Nick Wellnhofer
45ce1ee399 Add TODO comment in xmlSwitchEncoding
It would be nice if we could recover from unsupported encodings in
external entities.
2017-06-10 17:32:44 +02:00
Nick Wellnhofer
0db8dc9ddc Stop parser on unsupported encodings
Otherwise, the push parser can loop infinitely in recover mode.

Found with libFuzzer.
2017-06-07 19:30:56 +02:00
Pranjal Jumde
0bcd05c5cd Heap-based buffer overread in htmlCurrentChar
For https://bugzilla.gnome.org/show_bug.cgi?id=758606

* parserInternals.c:
(xmlNextChar): Add an test to catch other issues on ctxt->input
corruption proactively.
For non-UTF-8 charsets, xmlNextChar() failed to check for the end
of the input buffer and would continuing reading.  Fix this by
pulling out the check for the end of the input buffer into common
code, and return if we reach the end of the input buffer
prematurely.
* result/HTML/758606.html: Added.
* result/HTML/758606.html.err: Added.
* result/HTML/758606.html.sax: Added.
* result/HTML/758606_2.html: Added.
* result/HTML/758606_2.html.err: Added.
* result/HTML/758606_2.html.sax: Added.
* test/HTML/758606.html: Added test case.
* test/HTML/758606_2.html: Added test case.
2016-05-23 15:01:07 +08:00
David Kilzer
4472c3a5a5 Fix some format string warnings with possible format string vulnerability
For https://bugzilla.gnome.org/show_bug.cgi?id=761029

Decorate every method in libxml2 with the appropriate
LIBXML_ATTR_FORMAT(fmt,args) macro and add some cleanups
following the reports.
2016-05-23 15:01:07 +08:00
David Kilzer
d433ea6c83 Integer signed/unsigned type mismatch in xmlParserInputGrow()
For https://bugzilla.gnome.org/show_bug.cgi?id=766635

* parserInternals.c:
(xmlParserInputGrow): Change 'ret' type to 'int' to match the
return type of xmlParserInputBufferGrow().
2016-05-22 09:49:50 +08:00
Daniel Veillard
fdfeecc1b7 Bug on creating new stream from entity
sometimes the entity could have a lenght of 0, i.e. it wasn't
parsed or used yet, and we ended up with an incoherent input state
2015-11-20 15:07:38 +08:00
Daniel Veillard
afd27c21f6 Avoid processing entities after encoding conversion failures
For https://bugzilla.gnome.org/show_bug.cgi?id=756527
and was also raised by Chromium team in the past

When we hit a convwersion failure when switching encoding
it is bestter to stop parsing there, this was treated as a
fatal error but the parser was continuing to process to extract
more errors, unfortunately that makes little sense as the data
is obviously corrupt and can potentially lead to unexpected behaviour.
2015-11-09 18:07:18 +08:00
Daniel Veillard
c35af8b18d Fixes for xmlInitParserCtxt
let's make sure that parser options are updated too when a corrsponding
global variable or other field of the context is set.
2014-06-11 17:00:39 +08:00
Daniel Veillard
ff76eb28c7 Clear up a potential NULL dereference
https://bugzilla.gnome.org/show_bug.cgi?id=705399

if ctxt->node_seq.buffer is null then ctxt->node_seq.maximum ought
to be zero but it's better to clarify the check in the code directly.
2013-08-03 22:25:13 +08:00
Daniel Veillard
23f05e0c33 Detect excessive entities expansion upon replacement
If entities expansion in the XML parser is asked for,
it is possble to craft relatively small input document leading
to excessive on-the-fly content generation.
This patch accounts for those replacement and stop parsing
after a given threshold. it can be bypassed as usual with the
HUGE parser option.
2013-02-19 10:21:49 +08:00
Daniel Veillard
bf058dce13 Fix the flushing out of raw buffers on encoding conversions
https://bugzilla.gnome.org/show_bug.cgi?id=692915

the new set of converting functions tried to limit the encoding
conversion of the raw buffer to the consumption one to work in
a more progressive fashion. Unfortunately this was bad for
performances and led to errors on progressive parsing when
a very large chunk was close to the end of the document. Fix
the new internal function and switch back to the old way of
converting. Fix another bug in the process.
2013-02-13 18:19:42 +08:00
Michael Wood
fb27e2cd20 Fix spelling of "length". 2012-10-30 10:18:49 +08:00
Daniel Veillard
f8e3db0445 Big space and tab cleanup
Remove all space before tabs and space and tabs at end of lines.
2012-09-11 13:26:36 +08:00
Daniel Veillard
52d8ade7a7 Introduce some default parser limits
Those can be overrided by the XML_PARSE_HUGE option, they
are just default limits for Name lenght, dictionary size limits
and maximum amount of parser lookup.
* include/libxml/parserInternals.h: define the limits
* include/libxml/xmlerror.h: add a new error
* parser.c parserInternals.c: implements the new limits
2012-07-30 10:08:45 +08:00
Daniel Veillard
61551a1eb7 Cleanup function xmlBufResetInput() to set input from Buffer
This was scattered in a number of modules, xmlParserInputPtr
have usually their base, cur and end pointer set from an
xmlBuf used as input.
* buf.c buf.h: add a new function implementing this setup
* parser.c HTMLparser.c catalog.c parserInternals.c xmlreader.c
  use the new function instead of digging into the buffer in
  all those modules
2012-07-23 14:24:27 +08:00
Daniel Veillard
768eb3b82d Convert XML parser to the new input buffers
The main changes are when the internal of the buffers structure
were adressed directly, we now use routines coming from buf.h
The routine xmlParserInputRead() which wasn't used anywhere is
deprecated too.
2012-07-23 14:24:26 +08:00
Daniel Veillard
0d51cfebc9 Fix a race in xmlNewInputStream
For https://bugzilla.gnome.org/show_bug.cgi?id=643148
Reported by Bill Clarke <llib@computer.org>, it used a global variable
as a counter for the input id and this was not thread safe. To avoid the
race without adding unneeded locking in the parser path, move the id to
the parser context instead.
2012-05-15 11:18:40 +08:00
Eugene Pimenov
615904f582 Switch the HTML parser to be non-recursive
* HTMLparser.c: new htmlParseElementInternal non recursive, with
  htmlParseContentInternal and new function to handle node info
  and element end.
* include/libxml/parser.h: add new stack for element info in parser
  context
* parserInternals.c: fee element info stack
2010-03-15 15:16:02 +01:00
Daniel Veillard
7e385bd4e2 566012 autodetected encoding and encoding conflict
* encoding.c parser.c parserInternals.c: when we autodetect an encoding
  but it's actually not completely compatible with the one declared
  great care must be taken to not convert more than just the first line.
  Led to some refactoring, more private functions and a bit of cleanup.
2009-08-26 11:38:49 +02:00
Daniel Veillard
33c76c8312 Fix end of buffer char being split in XML parser
* parserInternals.c: similar patch to previous, reset cur on GROW
  in xmlNextChar and xmlCurrentChar
2009-08-25 11:30:34 +02:00
Nick Wellnhofer
2f522dc68f Fix xmlKeepBlanksDefault to not break indent
* parserInternals.c: the old compatibility function xmlKeepBlanksDefault()
  should not reset xmlIndentTreeOutput to 0 because the default is 1
2009-08-20 12:11:17 +02:00
Daniel Veillard
4bf899bf1b fix for CVE-2008-3281 Daniel
* include/libxml/parser.h include/libxml/entities.h entities.c
  parserInternals.c parser.c: fix for CVE-2008-3281
Daniel

svn path=/trunk/; revision=3772
2008-08-20 17:04:30 +00:00
Daniel Veillard
87303e3c7c applied patch from Ashwin to avoid a potential double-free Daniel
* parserInternals.c: applied patch from Ashwin to avoid a potential
  double-free
Daniel

svn path=/trunk/; revision=3741
2008-04-28 18:07:29 +00:00
Daniel Veillard
b3edafd72d avoid a warning on 64bits introduced earlier make more checking on the
* parser.c: avoid a warning on 64bits introduced earlier
* parserInternals.c: make more checking on the UTF-8 input
Daniel

svn path=/trunk/; revision=3676
2008-01-11 08:00:57 +00:00
Daniel Veillard
5addfebd06 applied patch from Marius Konitzer to avoid leaking in
* parserInternals.c: applied patch from Marius Konitzer to avoid
  leaking in xmlNewInputFromFile() in case of HTTP redirection
Daniel
2006-10-17 20:32:22 +00:00
Daniel Veillard
30e7607b7a a bunch of small cleanups based on coverity reports. Daniel
* HTMLparser.c parser.c parserInternals.c pattern.c uri.c: a bunch
  of small cleanups based on coverity reports.
Daniel
2006-03-09 14:13:55 +00:00
Daniel Veillard
6a0baa0cd8 fixed a number of warnings shown by HP-UX compiler and reported by Rick
* HTMLparser.c configure.in parserInternals.c runsuite.c runtest.c
  testapi.c xmlschemas.c xmlschemastypes.c xmlstring.c: fixed a number
  of warnings shown by HP-UX compiler and reported by Rick Jones
Daniel
2005-12-10 11:11:12 +00:00
Daniel Veillard
c19d535e5e removed unreachable code pointed out by Oleksandr Kononenko, fixes bug
* parserInternals.c: removed unreachable code pointed out by
  Oleksandr Kononenko, fixes bug #321695
Daniel
2005-11-17 13:12:16 +00:00
Daniel Veillard
6e84bb28dd fix a problem in some error case on Solaris when passed a NULL filename,
* parserInternals.c: fix a problem in some error case on Solaris
  when passed a NULL filename, pointed by Albert Chin.
Daniel
2005-10-26 09:00:29 +00:00
Daniel Veillard
2e7598cb06 avoid passing a char[] as snprintf first argument. implemented
* encoding.c parserInternals.c: avoid passing a char[] as snprintf
  first argument.
* threads.c include/libxml/threads.h: implemented xmlIsThreadsEnabled()
  based on Andrew W. Nosenko idea.
* doc/* elfgcchack.h: regenerated the API
Daniel
2005-09-02 12:28:34 +00:00
Daniel Veillard
75e389d4e0 more cleanups based on sparse reports, added "make sparse" Daniel
* Makefile.am globals.c parserInternals.c xmlreader.c xmlunicode.c
  xmlwriter.c: more cleanups based on sparse reports, added
  "make sparse"
Daniel
2005-07-29 22:02:24 +00:00
Daniel Veillard
304e78c6b4 fix bug raised by zamez on IRC regenerated, seems to pop-up leaks in new
* parserInternals.c: fix bug raised by zamez on IRC
* testapi.c: regenerated, seems to pop-up leaks in new tree functions
* tree.c: added comments missing.
* doc/*: regenerated
Daniel
2005-07-03 16:19:41 +00:00
Daniel Veillard
5d4644ef6e revamped the elfgcchack.h format to cope with gcc4 change of aliasing
* doc/apibuild.py doc/elfgcchack.xsl: revamped the elfgcchack.h
  format to cope with gcc4 change of aliasing allowed scopes, had
  to add extra informations to doc/libxml2-api.xml to separate
  the header from the c module source.
* *.c: updated all c library files to add a #define bottom_xxx
  and reimport elfgcchack.h thereafter, and a bit of cleanups.
* doc//* testapi.c: regenerated when rebuilding the API
Daniel
2005-04-01 13:11:58 +00:00
Aleksey Sanin
8fdc32abfe fixing col information in xmlParserInput and propagating column into xmlError 2005-01-05 15:37:55 +00:00
William M. Brack
1d8c9b291e fixed to skip (if necessary) the BOM for encoding 'utf-16'. Completes the
* parserInternals.c: fixed to skip (if necessary) the BOM for
  encoding 'utf-16'.  Completes the fix for bug #152286.
* tree.c, parser.c: minor warning cleanup, no change to logic
2004-12-25 10:14:57 +00:00
Daniel Veillard
a521d28751 better handling of conditional features more testing on parser contexts
* gentest.py testapi.c: better handling of conditional features
* HTMLparser.c SAX2.c parserInternals.c xmlwriter.c: more testing
  on parser contexts closed leaks, error messages
Daniel
2004-11-09 14:59:59 +00:00
Daniel Veillard
f2a36f98e1 more types. more fixes Daniel
* testapi.c: more types.
* parserInternals.c xpath.c: more fixes
Daniel
2004-11-08 17:55:01 +00:00
Daniel Veillard
2a4fb5ac07 more coverage more fixes Daniel
* gentest.py testapi.c: more coverage
* SAX2.c parser.c parserInternals.c: more fixes
Daniel
2004-11-08 14:02:18 +00:00
Daniel Veillard
4259532303 more types, more coverage more problems fixed Daniel
* gentest.py testapi.c: more types, more coverage
* parser.c parserInternals.c relaxng.c valid.c xmlIO.c
  xmlschemastypes.c: more problems fixed
Daniel
2004-11-08 10:52:06 +00:00
Daniel Veillard
ce682bc24b autogenerate a minimal NULL value sequence for unknown pointer types This
* gentest.py testapi.c: autogenerate a minimal NULL value sequence
  for unknown pointer types
* HTMLparser.c SAX2.c chvalid.c encoding.c entities.c parser.c
  parserInternals.c relaxng.c valid.c xmlIO.c xmlreader.c
  xmlsave.c xmlschemas.c xmlschemastypes.c xmlstring.c xpath.c
  xpointer.c: This uncovered an impressive amount of entry points
  not checking for NULL pointers when they ought to, closing all
  the open gaps.
Daniel
2004-11-05 17:22:25 +00:00
Daniel Veillard
36e5cd5064 adding xmlMemBlocks() work on generator of an automatic API regression
* xmlmemory.c include/libxml/xmlmemory.h: adding xmlMemBlocks()
* Makefile.am gentest.py testapi.c: work on generator of an
  automatic API regression test tool.
* SAX2.c nanoftp.c parser.c parserInternals.c tree.c xmlIO.c
  xmlstring.c: various API hardeing changes as a result of running
  teh first set of automatic API regression tests.
* test/slashdot16.xml: apparently missing from CVS, commited it
Daniel
2004-11-02 14:52:23 +00:00
Daniel Veillard
eff45a92da register xmlSchemaSetValidErrors, patch from Brent Hendricks in the
* python/libxml.c: register xmlSchemaSetValidErrors, patch from
  Brent Hendricks in the mailing-list
* include/libxml/valid.h HTMLparser.c SAX2.c valid.c
  parserInternals.c: fix #156626 and more generally how to find out
  if a validation contect is part of a parsing context or not. This
  can probably be improved to make 100% sure that vctxt->userData
  is the parser context too. It's a bit hairy because we can't
  change the xmlValidCtxt structure without breaking the ABI since
  this change xmlParserCtxt information indexes.
Daniel
2004-10-29 12:10:55 +00:00
Daniel Veillard
29b1748205 small typo pointed out by Mike Hommey slightly improved the --c14n
* xmlIO.c: small typo pointed out by Mike Hommey
* doc/xmllint.xml, xmllint.html, xmllint.1: slightly improved
  the --c14n description, c.f. #144675 .
* nanohttp.c nanoftp.c: applied a first simple patch from
  Mike Hommey for $no_proxy, c.f. #133470
* parserInternals.c include/libxml/parserInternals.h
  include/libxml/xmlerror.h: cleanup to avoid 'error' identifier
  in includes #
* parser.c SAX2.c debugXML.c include/libxml/parser.h:
  first version of the inplementation of parsing within
  the context of a node in the tree #142359, new function
  xmlParseInNodeContext(), added support at the xmllint --shell
  level as the "set" function
* test/scripts/set* result/scripts/* Makefile.am: extended
  the script based regression tests to instrument the new function.
Daniel
2004-08-16 00:39:03 +00:00
Daniel Veillard
3671190b54 added xmlByteConsumed() interface updated the benchmark rebuilt the docs
* parserInternals.c xmlIO.c encoding.c include/libxml/parser.h
  include/libxml/xmlIO.h: added xmlByteConsumed() interface
* doc/*: updated the benchmark rebuilt the docs
* python/tests/Makefile.am python/tests/indexes.py: added a
  specific regression test for xmlByteConsumed()
* include/libxml/encoding.h rngparser.c tree.c: small cleanups
Daniel
2004-02-11 13:25:26 +00:00
Daniel Veillard
5bb9ccd56a remove the warning on the 2001 namespace remove some warnings when
* xinclude.c: remove the warning on the 2001 namespace
* parser.c parserInternals.c xpath.c: remove some warnings
  when compiling with MSVC6
* nanohttp.c: applied a patch when using _WINSOCKAPI_
Daniel
2004-02-09 12:39:02 +00:00
Daniel Veillard
157fee019d previous fix for #124044 was broken, correct fix provided. fix
* python/libxml.c: previous fix for #124044 was broken, correct
  fix provided.
* HTMLparser.c parser.c parserInternals.c xmlIO.c: fix xmlStopParser()
  and the error handlers to address #125877
Daniel
2003-10-31 10:36:03 +00:00
Daniel Veillard
e8039dfec7 make sure we report errors if xmlNewInputFromFile() fails. avoid using
* libxml.h parserInternals.c xmlIO.c: make sure we report errors
  if xmlNewInputFromFile() fails.
* xmlreader.c: avoid using _private for the node or document
  elements.
Daniel
2003-10-27 11:25:13 +00:00
Daniel Veillard
a840b69261 Fixed the HTTP<->parser interraction, which should fix 2 long standing
* include/libxml/nanohttp.h include/libxml/parserInternals.h
  include/libxml/xmlIO.h nanohttp.c parserInternals.c xmlIO.c:
  Fixed the HTTP<->parser interraction, which should fix 2 long
  standing bugs #104790 and #124054 , this also fix the fact that
  HTTP error code (> 400) should not generate data, we usually
  don't want to parse the HTML error information instead of the
  resource looked at.
Daniel
2003-10-19 13:35:37 +00:00
William M. Brack
68aca051a6 new files for a different method for doing range validation of character
* genChRange.py, chvalid.def, chvalid.c, include/libxml/chvalid.h:
  new files for a different method for doing range validation
  of character data.
* Makefile.am, parserInternals.c, include/libxml/Makefile.am,
  include/libxml/parserInternals.h: modified for new range method.
* catalog.c: small enhance for warning message (using one
  of the new range routines)
2003-10-11 15:22:13 +00:00
Daniel Veillard
659e71ec24 Setting up the framework for structured error reporting, touches a lot of
* HTMLparser.c c14n.c catalog.c error.c globals.c parser.c
  parserInternals.c relaxng.c valid.c xinclude.c xmlIO.c xmlregexp.c
  xmlschemas.c xpath.c xpointer.c include/libxml/globals.h
  include/libxml/parser.h include/libxml/valid.h
  include/libxml/xmlerror.h: Setting up the framework for structured
  error reporting, touches a lot of modules, but little code now
  the error handling trail has been cleaned up.
Daniel
2003-10-10 14:10:40 +00:00
Daniel Veillard
ce9457f3aa more cleanup of error handling in parserInternals, sharing the routine for
* parserInternals.c parser.c valid.c include/libxml/parserInternals.h:
  more cleanup of error handling in parserInternals, sharing the
  routine for memory errors.
Daniel
2003-10-05 21:33:18 +00:00
Daniel Veillard
f403d298c3 more code cleanup, especially around error messages, the HTML parser has
* HTMLparser.c Makefile.am legacy.c parser.c parserInternals.c
  include/libxml/xmlerror.h: more code cleanup, especially around
  error messages, the HTML parser has now been upgraded to the new
  handling.
* result/HTML/*: a few changes in the resulting error messages
Daniel
2003-10-05 13:51:35 +00:00
Daniel Veillard
2b8c4a151b changed 'make tests' to use a concise output, scrolling to see where thing
* Makefile.am: changed 'make tests' to use a concise output,
  scrolling to see where thing broke wasn't pleasant
* configure.in: some beta4 preparation, but not ready yet
* error.c globals.c include/libxml/globals.h include/libxml/xmlerror.h:
  new error handling code, last error informations are stored
  in the parsing context or a global variable, new APIs to
  handle the xmlErrorPtr type.
* parser.c parserInternals.c valid.c : started migrating to the
  new error handling code, it's a royal pain.
* include/libxml/parser.h include/libxml/parserInternals.h:
  moved the definition of xmlNewParserCtxt()
* parser.c: small potential buffer access problem in push code
  provided by Justin Fletcher
* result/*.sax result/VC/PENesting* result/namespaces/*
  result/valid/*.err: some error messages were sligthly changed.
Daniel
2003-10-02 22:28:19 +00:00
Daniel Veillard
73b013fc17 added a new configure option --with-push, some cleanups, chased code size
* HTMLparser.c Makefile.am configure.in legacy.c parser.c
  parserInternals.c testHTML.c xmllint.c include/libxml/HTMLparser.h
  include/libxml/parser.h include/libxml/parserInternals.h
  include/libxml/xmlversion.h.in: added a new configure
  option --with-push, some cleanups, chased code size anomalies.
  Now a library configured --with-minimum is around 150KB,
  sounds good enough.
Daniel
2003-09-30 12:36:01 +00:00
Daniel Veillard
8127390f03 added 2 new configure option: --with-reader --with-sax1 to allow removing
* Makefile.am SAX.c SAX2.c configure.in globals.c parser.c
  parserInternals.c testReader.c testSAX.c xmlIO.c xmllint.c
  xmlreader.c example/gjobread.c include/libxml/xmlversion.h.in:
  added 2 new configure option: --with-reader --with-sax1
  to allow removing the reader or non-xmlReadxxx() interfaces.
Daniel
2003-09-30 00:43:48 +00:00
Daniel Veillard
4432df239b cleanup, creating a new legacy.c module, made sure make tests ran in
* Makefile.am: cleanup, creating a new legacy.c module,
  made sure make tests ran in reduced conditions
* SAX.c SAX2.c configure.in entities.c globals.c parser.c
  parserInternals.c tree.c valid.c xlink.c xmlIO.c xmlcatalog.c
  xmlmemory.c xpath.c xmlmemory.c include/libxml/xmlversion.h.in:
  increased the modularization, allow to configure out
  validation code and legacy code, added a configuration
  option --with-minimum compiling only the mandatory code
  which then shrink to 200KB.
Daniel
2003-09-28 18:58:27 +00:00
Daniel Veillard
11476b4eac fixed a backward compatibility problem when formatting "deprecated SAXv1
* parserInternals.c: fixed a backward compatibility problem
  when formatting "deprecated SAXv1 function ignorableWhitespace"
  could be reproduced by xmllint --format
Daniel
2003-09-26 14:51:39 +00:00
Daniel Veillard
092643b52d preparing a beta3 solving the ABI problems make sure the global variables
* configure.in: preparing a beta3 solving the ABI problems
* globals.c parser.c parserInternals.c testHTML.c HTMLparser.c SAX.c
  include/libxml/globals.h include/libxml/SAX.h: make sure the
  global variables for the default SAX handler are V1 ones to
  avoid ABI compat problems.
* xmlreader.c: cleanup of uneeded code
* hash.c: fix a comment
Daniel
2003-09-25 14:29:29 +00:00
Daniel Veillard
a66b1d1a7d avoid a leak with previous patch Daniel
* parserInternals.c: avoid a leak with previous patch
Daniel
2003-09-17 20:54:38 +00:00
Daniel Veillard
8d8bf2c5f6 small fix from Rob Richards for input filename fixes for --repeat and
* parserInternals.c: small fix from Rob Richards for input filename
* xmllint.c: fixes for --repeat and --memory/--stream for speed tests
* xmlIO: adding a guard in one function
Daniel
2003-09-17 19:36:25 +00:00
Daniel Veillard
9f7eb0bba3 started messing seriously with per-document dict and element and attribute
* SAX2.c parser.c parserInternals.c xmlreader.c: started messing
  seriously with per-document dict and element and attribute nodes
  reuse in the xmlReader. This seems to lead to an interesting
  speedup of the xmlReader already.
Daniel
2003-09-17 10:26:25 +00:00
Daniel Veillard
bdbe0d4e78 factoring of more error handling code, serious size reduction and more
* parser.c include/libxml/xmlerror.h: factoring of more
  error handling code, serious size reduction and more lisibility
  of the resulting code.
* parserInternals.c parser.c include/libxml/parserInternals.h
  include/libxml/parser.h: changing the way VC:Proper Group/PE Nesting
  checks are done, use a counter for entities. Entities where freed and
  reallocated at the same address failing the check.
* tree.c: avoid a warning
* result/valid/* result/VC/*: this slightly changes some validation
  error messages.
Daniel
2003-09-14 19:56:14 +00:00
Daniel Veillard
877a7bd6a3 removing the broken pseudo SGML DocBook parser code. Daniel
* Makefile.am DOCBparser.c parserInternals.c testDocbook.c
  xmllint.c doc/xmllint.xml doc/xmllint.1: removing the
  broken pseudo SGML DocBook parser code.
Daniel
2003-09-13 00:16:32 +00:00