GCC Code Coverage Report


Directory: ./
File: src/msrproto/expat_wrapper_impl.h
Date: 2025-01-07 12:25:12
Exec Total Coverage
Lines: 43 54 79.6%
Branches: 24 62 38.7%

Line Branch Exec Source
1 /*****************************************************************************
2 *
3 * Copyright (C) 2021 Bjarne von Horn (vh at igh dot de)
4 *
5 * This file is part of the PdCom library.
6 *
7 * The PdCom library is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or (at your
10 * option) any later version.
11 *
12 * The PdCom library is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
15 * License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with the PdCom library. If not, see <http://www.gnu.org/licenses/>.
19 *
20 *****************************************************************************/
21
22 #ifndef MSRPROTO_EXPAT_WRAPPER_IMPL_H
23 #define MSRPROTO_EXPAT_WRAPPER_IMPL_H
24
25 #include "expat_wrapper.h"
26
27 #include <expat.h>
28 #include <expat_external.h>
29 #include <pdcom5/Exception.h>
30 #include <stdexcept>
31
32 namespace PdCom { namespace impl { namespace MsrProto {
33 136 inline void XmlParserDeleter::operator()(XML_ParserStruct *s) noexcept
34 {
35
1/2
✓ Branch 0 taken 136 times.
✗ Branch 1 not taken.
136 if (s)
36 136 XML_ParserFree(s);
37 136 }
38
39
40 template <typename Handler, bool useExpatBuffer>
41 struct ExpatWrapper<Handler, useExpatBuffer>::CallbackWrapper
42 {
43 // TODO(ighvh): use template<auto> when C++17 is mandatory (so in about 10
44 // years)
45 template <typename T, T, int = 0>
46 struct Impl;
47
48 // We use a partial specialisation of the struct above
49 // to extract the argument types (Args) from the Member function.
50 template <
51 typename... Args,
52 void (Handler::*callback)(Args...),
53 int level_change>
54 struct Impl<void (Handler::*)(Args...), callback, level_change>
55 {
56 1684 static void XMLCALL call(void *userdata, Args... args)
57 {
58 1684 auto &This =
59 *reinterpret_cast<ExpatWrapper<Handler, useExpatBuffer> *>(
60 userdata);
61 // increment (start handler only)
62 1684 This.level_ += level_change == 1;
63
3/4
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 610 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1072 times.
1684 if (This.pending_exception_)
64 2 return;
65 try {
66
3/4
✓ Branch 4 taken 610 times.
✗ Branch 5 not taken.
✓ Branch 10 taken 1070 times.
✓ Branch 11 taken 2 times.
1682 (static_cast<Handler &>(This).*callback)(args...);
67 }
68 4 catch (...) {
69 2 This.pending_exception_ = std::current_exception();
70
1/4
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
2 XML_StopParser(This.parser_.get(), false);
71 }
72 // decrement (end handler only)
73 1682 This.level_ -= level_change == -1;
74 }
75 };
76 };
77
78 template <typename Handler, bool useExpatBuffer>
79 128 inline ExpatWrapper<Handler, useExpatBuffer>::ExpatWrapper(const char *encoding)
80 {
81
1/2
✓ Branch 2 taken 128 times.
✗ Branch 3 not taken.
128 reset(encoding);
82 128 }
83
84 template <typename Handler, bool useExpatBuffer>
85 136 inline void ExpatWrapper<Handler, useExpatBuffer>::reset(const char *encoding)
86 {
87 136 pending_exception_ = nullptr;
88 136 level_ = 0;
89 136 parser_.reset(XML_ParserCreate(encoding));
90
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 136 times.
136 if (!parser_)
91 throw PdCom::Exception("Could not create XML parser");
92
93 #if HAVE_XML_SetReparseDeferralEnabled
94 if (XML_SetReparseDeferralEnabled(parser_.get(), XML_FALSE) != XML_TRUE)
95 throw PdCom::Exception("Could not set ReparseDeferralEnabled attribute "
96 "of XML parser.");
97 #endif
98
99 using W1 = typename CallbackWrapper::template Impl<
100 decltype(&Handler::startElement), &Handler::startElement, 1>;
101 using W2 = typename CallbackWrapper::template Impl<
102 decltype(&Handler::endElement), &Handler::endElement, -1>;
103
104 136 XML_SetUserData(parser_.get(), this);
105 136 XML_SetStartElementHandler(parser_.get(), &W1::call);
106 136 XML_SetEndElementHandler(parser_.get(), &W2::call);
107 136 }
108
109 template <typename Handler, bool useExpatBuffer>
110 template <typename T>
111 inline typename std::
112 enable_if<!useExpatBuffer && std::is_same<T, char>::value, bool>::type
113 6974 ExpatWrapper<Handler, useExpatBuffer>::parse(
114 const T *s,
115 std::size_t n,
116 bool final)
117 {
118
1/2
✓ Branch 3 taken 6974 times.
✗ Branch 4 not taken.
6974 if (XML_STATUS_OK == XML_Parse(parser_.get(), s, n, final))
119 6974 return true;
120
121 const XML_Error err_code = XML_GetErrorCode(parser_.get());
122 if (err_code == XML_ERROR_ABORTED && pending_exception_) {
123 auto pe = pending_exception_;
124 pending_exception_ = nullptr;
125 std::rethrow_exception(pe);
126 }
127
128 static_cast<Handler *>(this)->xmlError(XML_ErrorString(err_code));
129 return false;
130 }
131
132
133 template <typename Handler, bool useExpatBuffer>
134 template <std::size_t bufsize>
135 436 ExpatWrapper<Handler, useExpatBuffer>::ExpatBuffer<bufsize>::ExpatBuffer(
136 ExpatWrapper<Handler, useExpatBuffer> &wrapper)
137 {
138 static_assert(bufsize > 0, "Buffer size of 0 makes no sense.");
139 static_assert(useExpatBuffer, "Don't make a buffer when not using it");
140 436 buffer_ = XML_GetBuffer(wrapper.parser_.get(), bufsize);
141
2/4
✗ Branch 1 not taken.
✓ Branch 2 taken 369 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 67 times.
436 if (!buffer_)
142 throw std::bad_alloc();
143 436 }
144
145 template <typename Handler, bool useExpatBuffer>
146 template <std::size_t bufsize>
147 inline typename std::enable_if<(useExpatBuffer && bufsize > 0), bool>::type
148 425 ExpatWrapper<Handler, useExpatBuffer>::parse(
149 ExpatWrapper<Handler, useExpatBuffer>::ExpatBuffer<
150 bufsize> /* buffer */,
151 std::size_t n,
152 bool final)
153 {
154
2/4
✗ Branch 0 not taken.
✓ Branch 1 taken 358 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 67 times.
425 if (n > bufsize)
155 throw PdCom::InvalidArgument(
156 "buffer overflow, wrote more bytes than buffer size.");
157
158
3/4
✓ Branch 3 taken 355 times.
✓ Branch 4 taken 3 times.
✓ Branch 8 taken 67 times.
✗ Branch 9 not taken.
425 if (XML_STATUS_OK == XML_ParseBuffer(parser_.get(), n, final))
159 422 return true;
160
161 3 const XML_Error err_code = XML_GetErrorCode(parser_.get());
162
5/12
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
3 if (err_code == XML_ERROR_ABORTED && pending_exception_) {
163 4 auto pe = pending_exception_;
164 2 pending_exception_ = nullptr;
165 4 std::rethrow_exception(pe);
166 }
167
168
1/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
1 static_cast<Handler *>(this)->xmlError(XML_ErrorString(err_code));
169 return false;
170 }
171
172 }}} // namespace PdCom::impl::MsrProto
173
174 #endif // MSRPROTO_EXPAT_WRAPPER_H
175