0
0
mirror of https://github.com/zeux/pugixml.git synced 2025-01-14 01:47:55 +08:00

740 Commits

Author SHA1 Message Date
Arseny Kapoulkine
8cece4b9fe Fix a bug in move construction when move source is empty
Previously when copying the allocator state we would copy an incorrect
root pointer into the document's current state; while this had a minimal
impact on the allocation state due to the fact that any new allocation
would need to create a new page, this used a potentially stale field of
the moved document when setting up new pages, which could create issues
in future uses of the pages.

This change fixes the core problem and also removes the use of the
_root->allocator from allocate_page since it's not clear why we need it
there in the first place.
2021-05-11 22:53:54 -07:00
Arseny Kapoulkine
56c9afa7c8 XPath: Improve recursion limit for deep chains of //
Since foo//bar//baz adds two nodes for each //, we need to increment the
depth by 2 on each iteration to limit the AST correctly.

Fixes the stack overflow found by cluster-fuzz (I suspect the issue
there is a bit deeper, but this part is definitely a bug and as such I'd
rather wait for the next test case for now).
2021-05-11 22:27:53 -07:00
Rosen Penev
c167259e60 add empty method
Simple and allows to avoid using std::distance.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-04-27 13:55:02 -07:00
Rosen Penev
ef257796db remove const from operator++/--
This prevents usage with C++20 ranges since it does not satisfy
std::weakly_incrementable.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2021-04-26 14:06:19 -07:00
Arseny Kapoulkine
fe4bc946b2 Update copyright year to 2020 everywhere 2020-11-26 01:00:27 -08:00
Arseny Kapoulkine
70bd6a6b0a Update version to 1.11 and update documentation 2020-11-25 10:18:42 -08:00
Arseny Kapoulkine
5f97d5d66f Fix -Wshadow in remove_children()
child variable was shadowing xml_node::child
2020-11-25 09:28:26 -08:00
Arseny Kapoulkine
28aebf2b22
Merge pull request #382 from zeux/TheNicker-master
Fix MSVC deprecation warnings when using clang-cl
2020-11-25 09:19:24 -08:00
Arseny Kapoulkine
df42668e18 Cleanup code and feature detection
We now use open_file similarly to open_file_wide, and activate the
workaround for MSVC 2005+ since that's when the _s versions were added
in the first place.
2020-11-25 08:38:22 -08:00
Arseny Kapoulkine
8e5b8e0f46 XPath: Fix stack overflow in functions with long argument lists
Function call arguments are stored in a list which is processed
recursively during optimize(). We now limit the depth of this construct
as well to make sure optimize() doesn't run out of stack space.
2020-09-11 09:50:41 -07:00
Arseny Kapoulkine
20aef1cd4b Fix stack overflow in tests on MSVC x64
The default stack on MSVC/x64/debug is sufficient for 1692 nested
invocations only, whereas on clang/linux it's ~8K...

For now set the limit to be conservative.
2020-09-10 09:11:46 -07:00
Arseny Kapoulkine
1f84db837b XPath: Restrict AST depth to prevent stack overflow
XPath parser and execution engine isn't stackless; the depth of the
query controls the amount of C stack space required.

This change instruments places in the parser where the control flow can
recurse, requiring too much C stack space to produce an AST, or where a
stackless parse is used to produce arbitrarily deep AST which will
create issues for downstream processing.

As a result XPath parser should now be fuzz safe for malicious inputs.
2020-09-10 00:55:26 -07:00
Lior Lahav
c258fba6f1 Replaced fopen and _wfopen deprecated functions with the safer fopen_s and _wfopen_s 2020-07-21 22:37:16 +03:00
Arseny Kapoulkine
23ca940487 Work around a false positive in MSVC debug runtime checker
In some MSVC versions on x64 configurations, the hashing function
triggers this failure:

Run-Time Check Failure #1 - A cast to a smaller data type has caused a
loss of data.  If this was intentional, you should mask the source of
the cast with the appropriate bitmask.

This is similar to the integer sanitizer - this code is valid C++ but
MSVC decides to warn about this nonetheless. Masking the pointer's low
32 bits fixes the issue.

Fixes #357.
2020-06-13 08:41:22 -07:00
Jan Niklas Hasse
c2d90c49f9 Use nullptr in pugixml.hpp if available, see #91 2020-05-10 17:28:30 +02:00
Arseny Kapoulkine
5e64076af9 Adjust node_copy_tree to be more explicit about invariants
The loop traverses the source tree and simultaneously builds up a copy
of it at destination. Short of race conditions, this code is safe -
however, it's not obvious that dit stays inside the destination tree.

This change adds a few assertions to help enforce/document these
invariants. One particular subtlety is that dit can actually *become*
null after we exit out of the loop, but it's guaranteed to only do so
once sit goes back to sn.

This is only possible when doing a full document copy - for some reason
we weren't using this for that (in reset(xml_document)), but we are now.

Fixes #314.
2020-02-19 21:02:33 -08:00
Arseny Kapoulkine
76c3914484 XPath: Remove the use of fallthrough switch cases
We were previously relying on non-standard comment detection that is
supported by gcc/clang to avoid warnings about implicit fallthrough.

This can be solved using attributes but using them requires a lot of
compiler-specific detection logic because not all versions of gcc/clang
support them.

We don't *really* need to rely on fallthrough here - the type conversion
block can be located *after* the AST type switch instead, which means
that any AST type that has type ambiguity can fall back to that in the
future.

Fixes #331.
2020-02-19 07:50:59 -08:00
Arseny Kapoulkine
546cafefa3 XPath: Add a clarification comment for ast_variable fallthrough 2020-02-01 07:48:45 -08:00
Arseny Kapoulkine
53a30c6571 Cleanup first_element_by_path
Instead of performing a late null check that is redundant and only
needed to silence clang static analysis warning, we pick the context as
a root / self node. This way the code is a bit less redundant and the
static analyzer is happy.
2019-12-03 21:23:15 -08:00
Valeriy Van
cda8623468 Fixing possible null pointer dereference 2019-12-02 15:02:54 +01:00
Lucas Amoudruz
78e98c446d explicit casts for -Wconversions warnings in gcc 2019-11-27 15:56:05 +01:00
Tuan Anh Tran
b9b2aeecaf Custom precision (#300)
Fixes #285
2019-09-22 08:42:41 -07:00
Arseny Kapoulkine
6934b123d4 Move pugixml.rc to scripts/
This keeps src/ folder clean of auxiliary files only required for
special builds; note that CMakeLists.txt already depends on scripts/
(specifically for pkgconfig setup).
2019-09-21 09:21:13 -07:00
lady mesh
6fb765ca9d Add: Windows DLL version info header (#298)
Closes #294
2019-09-19 07:27:40 -07:00
Arseny Kapoulkine
6202519ca6 tests: Add memory safety tests for remove_children/attributes
The newly added tests make sure that during node/attribute destruction
we deallocate a few memory pages; this makes sure that we don't read
node data after it's being destroyed.

Also clean up formatting/style in the remove_* implementation a bit.
2019-09-17 20:34:40 -07:00
Tuan Anh Tran
fd7326fb91 feat: add remove_attributes() and remove_children() (#296)
These functions remove all attributes / child nodes in bulk which is faster than removing them one at a time.
2019-09-17 20:18:41 -07:00
Arseny Kapoulkine
c6607740a0 Never escape > in attribute values
According to XML spec, > sometimes needs to be escaped in PCDATA (when
it occurs as a ]]> pattern), but it doesn't need to be escaped in
attribute values.

Contributes to #272.
2019-09-11 21:35:03 -07:00
Arseny Kapoulkine
946de603b1 Don't escape attribute quotation symbol
When using double quotes for attributes, we don't need to escape '; when
using single quotes, we don't need to escape ".

This changes behavior to match 1.9 by default (where we don't escape ').

Contributes to #272.
2019-09-11 21:27:20 -07:00
Arseny Kapoulkine
44e4d7e40b Update version to 1.10
Note: this chang also updates PUGIXML_VERSION macro to allow for
double-digit minor versions; this preserves the continuity of versions
so PUGIXML_VERSION >= 190 will still work.
2019-09-11 21:09:50 -07:00
m-naumann
5a867cb1e3 Add support for using single quotes to enclose attribute values
This change adds format_attribute_single_quote flag that uses single quotes (`'`) instead of double quotes (`"`) for formatting attribute values.

Internal quotation marks are escaped using `&quot;` and `&apos;`.
2019-06-18 19:51:10 -07:00
Arseny Kapoulkine
7247a823b7 XPath: Add missing inline/PUGI__FN specifiers
Also split one-liner if/for loops for better readability and coverage.
2019-03-17 07:14:11 -07:00
Yan Pashkovsky
66e1b4e03e format_skip_control_chars 2019-03-07 17:54:30 +03:00
Arseny Kapoulkine
c55ea3bc1e XPath: Make remove_duplicates generate stable order
Given an unsorted sequence, remove_duplicates would sort it using the
pointer value of attributes/nodes and then remove consecutive
duplicates.

This was problematic because it meant that the result of XPath queries
was dependent on the memory allocation pattern. While it's technically
incorrect to rely on the order, this results in easy to miss bugs.

This is particularly common when XPath queries use union operators -
although we also will call remove_duplicates in other cases.

This change reworks the code to use a hash set instead, using the same
hash function we use for compact storage. To make sure it performs well,
we allocate enough buckets for count * 1.5 (assuming all elements are
unique); since each bucket is a single pointer unlike xpath_node which
is two pointers, we need somewhere between size * 0.75 and size * 1.5
temporary storage.

The resulting filtering is stable - we remove elements that we have seen
before but we don't change the order - and is actually significantly
faster than sorting was.

With a large union operation, before this change it took ~56 ms per 100
query invocations to remove duplicates, and after this change it takes
~20ms.

Fixes #254.
2019-02-26 23:57:58 -08:00
Arseny Kapoulkine
93c7bacb29 XPath: Create set for a|b in order before duplicate filtering
This does not change the result of a union operation [substantially], but
it means that we now give a list to remove_duplicates that has more natural
ordering.

If remove_duplicates didn't sort the array, we'd have union operations
resulting in a consistent predictable order.

Contributes to #254.
2019-02-26 21:39:52 -08:00
Arseny Kapoulkine
9fcae40bb5 Happy New Year! 2019-01-01 23:05:04 +03:00
Arseny Kapoulkine
f9a2a7d19e Fix Wdouble-promotion warnings
We had a few places in test code and library source where we used an
implicit float->double cast; while it should preserve the value exactly,
gcc/clang implement this warning to make sure uses of double are intentional.

This change also adds the warning to Makefile to make sure we don't
regress on this warning.

Fixes #243.
2018-11-23 23:39:22 -08:00
Arseny Kapoulkine
aac75cd299 Escape TAB character in attribute values with &#09;
This change modifies the table entries for ctx_special_attr to treat TAB
character as special, which makes the output code escape it.

Before this change, trying to use TAB in an attribute value would output
it verbatim; during subsequent parsing, pugixml - and other compliant
parsers - would apply attribute-value normalization, turning the TAB
into a space and losing the original value.

Using &#09; fixes this; if an input document has &#09; in an attribute
value, that gets unescaped into \t during parsing and escaped back into
&#09; during output, which means we can now roundtrip values like this.

Fixes #242.
2018-11-19 22:26:21 -08:00
cecilios
7d2436ec2f Add coverity annotations 2018-11-16 15:25:12 -08:00
Arseny Kapoulkine
fa686f004a Update all URLs to pugixml.org to https:// 2018-11-12 12:28:54 -08:00
Arseny Kapoulkine
d9fadc7464 XPath: Workaround Coverity false positive
Coverity hits a similar false positive to what clang static analyzer hit
- it assumes that since optimize() checks _right for being nullptr,
optimize_self() might hit _right=nullptr in the ast_op_equal case which
is impossible.

Contributes to #236.
2018-10-23 21:40:18 -07:00
Dan Lipsa
273fa0ab26 Remove warning in Visual Studio (#235)
The following warning is removed:
Visual Studio 14.0
1. warning C4275: non dll-interface class 'std::exception' used as
   base for dll-interface class 'vtkpugixml::xpath_exception'
2018-10-16 08:43:32 -07:00
Arseny Kapoulkine
81c82588bd Work around clang --analyze warnings
clang doesn't understand the invariants guaranteed for specific AST node
types and, when seeing null pointer checks in optimize(), assumes any
pointers in the node might be null. Work around this by adding explicit
- redundant - null pointer checks.
2018-09-24 20:38:09 -07:00
Arseny Kapoulkine
e3b5e9ce3c XPath: Refactor xpath_node_set short buffer optimization
This change replaces xpath_node_set single element storage with a
single-element array in hopes that this would silence Coverity false
positive about getting a singleton pointer.

Additionally, it refactors _assign member to unify small and large
buffer codepaths since they are basically identical.

Fixes #233 (hopefully)
2018-09-24 20:19:31 -07:00
Arseny Kapoulkine
514478d1f8 Fix version comment; 190 = 1.9.0 (major.minor.patch)
Fixes #224.
2018-07-28 08:08:45 -07:00
Arseny Kapoulkine
c53fdab93a Clarify comment in get_strconv_pcdata as well 2018-04-14 23:45:23 -07:00
Arseny Kapoulkine
43b0a6a0a3 Clarify comment in get_strconv_attribute
This might have helped a bit with confusion in #195.
2018-04-14 23:27:59 -07:00
Arseny Kapoulkine
0c74e117b8 Update version to 1.9 2018-04-02 21:46:14 -07:00
Arseny Kapoulkine
4f9af79850 Work around gcc-8 warning
gcc-8 produces "attribute directive ignored" warning for
no_sanitize("unsigned-integer-overflow"); at some point gcc will
introduce integer sanitizer support and we'll have to do this all over
again but for now just don't emit the attribute.
2018-04-02 21:06:32 -07:00
Arseny Kapoulkine
951f1ed4ed Add a comment to clarify subtle branch in node_copy_tree 2018-03-29 08:27:56 -07:00
Arseny Kapoulkine
e50672cf37 ubsan: Fix undefined behavior for signed left shift in compact mode
We were using << compact_alignment_log2 instead of * compact_alignment
for symmetry with the encoding where >> is crucial to keep code fast and
round to negative infinity.

For decoding, the results are the same and any reasonable compiler
should convert *4 into <<2 so just use a multiplication - that doesn't
trigger UB on negative numbers.
2018-03-16 21:25:12 -07:00
Arseny Kapoulkine
9540016f6d ubsan: Fix type mismatch for xml_extra_buffer in compact mode
We were using allocate_memory to allocate struct xml_extra_buffer that
contains pointers; with compact mode, this allocation can be misaligned
by 4b with 8b pointers; fix this by manually realigning the pointer.
2018-03-15 23:10:10 -07:00
Arseny Kapoulkine
15fdb838c7 ubsan: Fix type mismatch in compact mode for document data
We were misaligning document data on 64-bit platforms by placing 8b
pointers at 4b offsets; fix this by reserving a full pointer worth of
bytes for page marker.
2018-03-15 23:09:35 -07:00
Arseny Kapoulkine
951c8f15d2 Refactor noexcept macros
Define noexcept using _MSC_VER instead of _MSC_FULL_VER (first release
of MSVC 2015 should have it), remove redundant PUGIXML_HAS_NOEXCEPT and
define PUGIXML_NOEXCEPT_IF_NOT_COMPACT in terms of PUGIXML_NOEXCEPT.
2018-03-15 22:31:13 -07:00
Matthäus Brandl
9187e6500c Adds noexcept specifiers to the move special members of xml_document,… (#185)
* Adds noexcept specifiers to the move special members of xml_document, but only #ifndef PUGIXML_COMPACT
2018-03-03 10:19:37 -08:00
Matthäus Brandl
8284dbf61d Add noexcept specifiers to move special members where possible (#183)
* Adds a macro definition to be able to use noexcept with supporting compilers

* Adds noexcept specifier to move special members of xpath_node_set, xpath_variable_set and xpath_query, but not of xml_document as it has a throwing implementation
2018-03-01 20:13:43 -08:00
Arseny Kapoulkine
41219a5a20
Merge pull request #184 from brandl-muc/override_for_msvc
Enables usage of override specifier for MSVC compilers
2018-02-27 14:22:52 -08:00
Brandl, Matthäus (MBR)
b8d1d07ad8 Enables usage of override specifier for MSVC compilers (beginning with 17.0 which is the compiler of Visual Studio 2012) 2018-02-27 22:27:15 +01:00
Arseny Kapoulkine
b127cfb18e Fix Texas Instruments compiler warning
Texas Instruments compiler produces this warning for unused template
member functions:

	"pugixml.cpp", line 253: warning #179-D: function
	"pugi::impl::<unnamed>::auto_deleter<T>::release [with
	T=pugi::impl::<unnamed>::xml_stream_chunk<char>]" was declared but
	never referenced

As far as I can tell, this is a compiler issue - these functions should
not be instantiated in the first place; while it's possible to rework
the code to work around this, the changes would be fragile. It seems
best to just disable this warning - we've seen something similar on SNC
(which appears to use the same frontend!..).

Fixes #182.
2018-02-27 08:49:16 -08:00
Arseny Kapoulkine
2ec3579f29 Work around gcc issues with limits.h not defining LLONG_MIN
It looks like there are several cases where this might happen:

- In some MinGW distributions, the LLONG_MIN/etc defines are guarded
with:

	#if !defined(__STRICT_ANSI__) && defined(__GNUC__)

Which means that you don't get them in strict ANSI mode. The previous
workaround was specifically targeted towards this.

- In some GCC distributions (notably GCC 6.3.0 in some configurations),
LLONG_MIN/etc. defines are guarded with:

	#if (defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L)

But __STDC_VERSION__ isn't defined as C99 even if you use -std=c++14 -
which is probably technically valid, but not useful.

To work around this, redefine the symbols whenever we are building with
GCC and we need them and they aren't defined - doing this is better than
not building. Instead of hard-coding the constants, use GCC-specific
__LONG_LONG_MAX__ to compute them.

Fixes #181.
2018-02-22 08:15:53 -08:00
Arseny Kapoulkine
3f28a5d97a Update all copyright notices to specify year 2018 2018-01-07 20:28:42 -08:00
Arseny Kapoulkine
257fbb4e1b Use raw pointers in xml_node::traverse implementation
This makes it a bit faster and matches other internal code better.
2017-11-13 19:29:42 -08:00
Arseny Kapoulkine
344c74a74c XPath: Always allocate xpath_strings on temporary stack for concat
The static_buffer optimization seems to come from the time where the
on-heap buffer was allocated using global memory operations. At this
point the temporary buffer and temporary string storage all come from
the evaluation stack (that can be partially allocated on heap...), so
the extra logic isn't relevant for performance.
2017-11-13 19:10:36 -08:00
Arseny Kapoulkine
3860b5076f Fix -Wshadow warning 2017-11-13 09:27:38 -08:00
Arseny Kapoulkine
4bd8771c2f Implement correct move error handling for compact mode
In compact mode, we currently can not support zero-allocation moves
since some pointer assignments required during the move need to allocate
hash table slots.

This is mostly applicable to xml_document_struct::first_child, since the
pointer to this element is used as a hash table key, but there are some
contrived cases where parents of root's children need a hash slot and
didn't have it before.

These cases can be fixed by changing the compact encoding to be a bit
more move friendly, but for now it's easier to handle the error and
throw/return during move.

When this happens, the source document doesn't change.
2017-11-13 08:57:16 -08:00
Arseny Kapoulkine
91a3c28862 Add count argument to compact_hash_table::rehash/reserve
This allows us to do a single reserve for a known amount of assignments
that is larger than the default minimum per reserve (16).
2017-11-13 08:37:34 -08:00
Arseny Kapoulkine
3af93a39d7 Clarify a note about compact hash behavior during move
After move some nodes in the hash table can have keys that point to
other; this makes the table somewhat larger but this does not impact
correctness.

The reason is that for us to access a key in the hash table, there
should be a compact_pointer/string object with the state indicating that
it is stored in a hash table, and with the address matching the key. For
this to happen, we had to have put this object into this state which
would mean that we'd overwrite the hash entry with the new, correct
value.

When nodes/pages are being removed, we do not clean up keys from the
hash table - it's safe for the same reason, and thus move doesn't
introduce additional contracts here.
2017-10-20 21:57:14 -07:00
Arseny Kapoulkine
febf25d1af Fix -Wshadow warning 2017-09-25 21:48:37 -07:00
Arseny Kapoulkine
a567f12d76 Implement move support for xml_document
This change implements the initial version of move construction and
assignment support for documents.

When moving a document to another document, we always make sure move
target is in "clean" state (empty document), and proceed by relocating
all structures in the most efficient way possible.

Complications arise from the fact that the root (document) node is
embedded into xml_document object, so all pointers to it have to change;
this includes parent pointers of all first-level children as well as
allocator pointers in all memory pages and previous pointer in the first
on-heap memory page.

Additionally, compact mode makes everything even more complicated
because some of the pointers we need to update are stored in the hash
table (in fact, document first_child pointer is very likely to be there;
some parent pointers in first-level children will be using
compact_shared_parent but some won't be) which requires allocating a new
hash table which can fail.

Some details of this process are not fully fleshed out, especially for
compact mode; and this definitely requires many tests.
2017-09-25 19:31:18 -07:00
Arseny Kapoulkine
77d7e60379 Fix Clang/C2 compatibility
Clang/C2 does not implement __builtin_expect; additionally we need to
work around deprecation warnings for fopen by disabling them.
2017-07-17 22:15:35 -07:00
Arseny Kapoulkine
853333cd70 Use PUGI__MSVC_CRT_VERSION instead of _MSC_VER
It's not clear whether we still need PUGI__MSVC_CRT_VERSION, but it's
more consistent for now to use it for _snprintf_s since this is relying
on a CRT extension, not on a compiler feature.
2017-06-22 20:28:06 -07:00
Arseny Kapoulkine
2252927c04 Deprecate xml_document::load(const char*) and xml_node::select_single_node
These functions were deprecated via comments in 1.5 but never got the
deprecated attribute; now is the time!

Using deprecated functions produces a warning; to silence it, this
change moves the relevant tests to a separate translation unit that has
deprecation disabled.
2017-06-22 09:13:10 -07:00
Arseny Kapoulkine
208e2cf043 Change PUGI__SNPRINTF to use _countof for MSVC
The macro only works correctly when the input argument is an array with
a statically known size - pointers or arrays decayed to pointers won't
work silently.

While this is unlikely to surface issues that aren't caught in
tests/code review, use _countof for MSVC to prevent such code from
compiling.
2017-06-19 07:06:47 -07:00
Arseny Kapoulkine
b6995f06b9 Fix BorlandC compilation
Rename partition to partition3 to resolve conflicts with std::partition.
2017-06-16 00:32:01 -07:00
Arseny Kapoulkine
95f013ba80 Refactor snprintf support
Instead of branching code at each invocation site, use variadic macros
to create a wrapping macro that use snprintf for the buffer of a
statically known size.

Variadic macros are supported by all C++11 compilers, as is snprintf;
on MSVC 2005+ we don't necessarily have snprintf, but we can use
_snprintf_s with _TRUNCATE to get the same behavior. In all other cases
we fall back to sprintf, that (theoretically) can lead to a stack buffer
overflow.

In practice all snprintfs used in pugixml use buffers that should be
large enough to never be overflown but snprintf is safe even if this is
not the case.
2017-06-15 23:35:20 -07:00
Arseny Kapoulkine
207bc788e9 Use buffer with a static size in convert_number_to_mantissa_exponent
We use references to arrays elsewhere in the codebase and there's just
one caller for this function so it's easier to fix the size.

This will simplify snprintf refactoring.
2017-06-15 22:58:46 -07:00
Arseny Kapoulkine
cd2804d3ee Merge pull request #145 from noresources/snprintf
use snprintf instead of sprintf
2017-06-15 21:34:04 -07:00
Arseny Kapoulkine
b3b44841f0 Mark all assert(false) statements as unreachable
Now we can exclude these from code coverage since it's logically
impossible to hit them in tests.
2017-06-15 09:26:23 -07:00
Renaud Guillard
0d8022eced use snprintf if available, _snprintf or sprintf otherwise 2017-06-11 18:33:28 +02:00
Renaud Guillard
810f1f600d use _snprintf if MSVC 2017-06-05 13:31:58 +02:00
Renaud Guillard
b5e9d933ad use snprintf instead of sprintf 2017-06-04 21:10:19 +02:00
Arseny Kapoulkine
38edf255ae Work around -fsanitize=integer issues
Integer sanitizer is flagging unsigned integer overflow in several
functions in pugixml; unsigned integer overflow is well defined but it
may not necessarily be intended.

Apart from hash functions, both string_to_integer and integer_to_string
use unsigned overflow - string_to_integer uses it to perform
two-complement negation so that the bulk of the operation can run using
unsigned integers. This makes it possible to simplify overflow checking.
Similarly integer_to_string negates the number before generating a
decimal representation, but negating is impossible without unsigned
overflow or special-casing certain integer limits.

For now just silence the integer overflow using a special attribute;
also move unsigned overflow into string_to_integer from get_value_* so
that we have fewer functions marked with the attribute.

Fixes #133.
2017-04-03 23:35:24 -07:00
Arseny Kapoulkine
101f32884f Add missing PUGI__FN to string_to_integer 2017-03-21 22:06:19 -07:00
Arseny Kapoulkine
956be4ca4b Revert "Fix gcc-4.8 compilation warning when using -Wstrict-overflow"
This reverts commit 79109a8546f963d17522d75112cffcfd8cbe35fc.

This warning does not happen on gcc-4.8.4; the workaround introduces an
unsigned integer overflow which results in a runtime error when compiled
with integer sanitizer.
2017-03-21 21:57:16 -07:00
Stephan Beyer
87fc170cdf Silence g++ 7.0.1 -Wimplicit-fallthrough warnings
This is accomplished by putting a // fallthrough
comment at the right place.
This seems to be more portable than an attribute-based
solution like [[fallthrough]] or __attribute__((fallthrough)).
2017-03-05 22:12:10 +01:00
Arseny Kapoulkine
8ce4592e15 Simplify compact_hash_table implementation
Instead of a separate implementation for find/insert, use just one that
can do both. This reduces the code size and simplifies code coverage;
the resulting code is close to what we had in terms of performance and
since hash table is a fall back should not affect any real workloads.
2017-03-03 07:11:22 -08:00
Arseny Kapoulkine
0991c1d283 Add invalid type assertion for offset_debug
This will make sure we don't forget to implement offset_debug for new
node types if they ever happen (really it's mostly for consistency).
2017-02-07 20:34:49 -08:00
Arseny Kapoulkine
2162a0d80c XPath: Simplify sorting implementation
Instead of a complicated partitioning scheme that tries to maintain the
equal area in the middle, use a scheme where we keep the equal area in
the left part of the array and then move it to the middle.

Since generally sorted arrays don't contain many duplicates this extra
copy is not too expensive, and it significantly simplifies the logic and
maintains good complexity for sorting arrays with many equal elements
nonetheless (unlike Hoare partitioning).

Instead of a median of 9 just use a median of 3 - it performs pretty
much identically on some internal performance tests, despite having a
bit more comparisons in some cases.

Finally, change the insertion sort threshold to 16 elements since that
appears to have slightly better performance.
2017-02-07 00:05:50 -08:00
Arseny Kapoulkine
774d5fe9df XPath: Optimize insertion_sort
The previous implementation opted for doing two comparisons per element
in the sorted case in order to remove one iterator bounds check per
moved element when we actually need to copy. In our case however the
comparator is pretty expensive (except for remove_duplicates which is
fast as it is) so an extra object comparison hurts much more than an
iterator comparison saves.

This makes sorting by document order up to 3% faster for random
sequences.
2017-02-06 19:28:33 -08:00
Arseny Kapoulkine
8cc3144e7b XPath: Remove redundant calls from xml_node::select_nodes et al
Instead of delegating to a method that just forwards the call to
xpath_query call the relevant method directly.
2017-02-05 21:52:30 -08:00
Arseny Kapoulkine
00e39c581a XPath: Remove evaluate_string_impl
It adds one stack frame to string query evaluation and does not really
simplify the code.
2017-02-05 21:50:13 -08:00
Arseny Kapoulkine
bcc7ed57a2 XPath: Simplify evaluation error flow
Instead of having two checks for out-of-memory when exceptions are
enabled, do just one and decide what to do based on whether we can
throw.
2017-02-03 20:33:40 -08:00
Arseny Kapoulkine
33159924b1 XPath: Clean up out-of-memory parse error handling
Instead of relying on a specific string in the parse result, use
allocator error state to report the error and then convert it to a
string if necessary.

We currently have to manually trigger the OOM error in two places
because we use global allocator in rare cases; we don't really need to
do this so this will be cleaned up later.
2017-02-02 18:40:20 -08:00
Arseny Kapoulkine
0e3ccc7396 Remove redundant branch from xml_node::path()
The code works fine regardless of the *j->name check, and omitting this
makes the code more symmetric between the "count" and "write" stage;
additionally this improves coverage - due to how strcpy_insitu works
it's not really possible to get an empty non-NULL name in the node.
2017-02-01 21:05:37 -08:00
Arseny Kapoulkine
9c7897b8d2 Remove null pointer test from first_element_by_path
All other functions treat null pointer inputs as invalid; now this
function does as well.
2017-01-30 23:55:31 -08:00
Arseny Kapoulkine
f500435cb4 XPath: Remove (re)allocate_throw and setjmp
Now error handling in XPath implementation relies on explicit error
propagation and is converted to an appropriate result at the end.
2017-01-30 22:31:57 -08:00
Arseny Kapoulkine
9e40c58532 XPath: Replace all (re)allocate_throw with (re)allocate_nothrow
This generates some out-of-memory code paths that are not covered by
existing tests, which will need to be resolved later.
2017-01-30 22:28:57 -08:00
Arseny Kapoulkine
c370d1190d XPath: Fix reallocate_nothrow to preserve existing state
Instead of rolling back the allocation and trying to allocate again,
explicitly handle inplace reallocate if possible, and allocate a new
block otherwise.

This is going to be important once we use reallocate_nothrow from a
non-throwing context.
2017-01-30 22:10:13 -08:00
Arseny Kapoulkine
1a2e4b88ee XPath: Use nonthrowing allocations in duplicate_string
This requires explicit error handling for xpath_string::data calls.
2017-01-30 21:58:53 -08:00
Arseny Kapoulkine
ac150d504e XPath: Throw std::bad_alloc if we got an out-of-memory error
This allows us to gradually convert exception handling of out-of-memory
during evaluation to a non-throwing approach without changing the
observable behavior.
2017-01-30 21:58:53 -08:00
Arseny Kapoulkine
1b3e8614e7 XPath: Reword brace mismatch errors for clarity 2017-01-30 11:51:07 -08:00