Skip to content

Commit 7dbf9da

Browse files
committed
Fix xmlNode clearing
IB-8475 Signed-off-by: Raul Metsma <[email protected]>
1 parent 83fcde4 commit 7dbf9da

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

src/XMLDocument.h

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static std::vector<unsigned char> from_base64(std::string_view data)
4545
std::vector<unsigned char> result(EVP_DECODE_LENGTH(data.size()), 0);
4646
size_t dataPos = 0;
4747
int size = 0;
48-
auto ctx = make_unique_ptr(EVP_ENCODE_CTX_new(), EVP_ENCODE_CTX_free);
48+
auto ctx = make_unique_ptr<EVP_ENCODE_CTX_free>(EVP_ENCODE_CTX_new());
4949
EVP_DecodeInit(ctx.get());
5050

5151
for(auto pos = data.find_first_of(whitespace);
@@ -71,7 +71,7 @@ static std::vector<unsigned char> from_base64(std::string_view data)
7171
static std::string to_base64(const std::vector<unsigned char> &data)
7272
{
7373
std::string result(EVP_ENCODE_LENGTH(data.size()), 0);
74-
auto ctx = make_unique_ptr(EVP_ENCODE_CTX_new(), EVP_ENCODE_CTX_free);
74+
auto ctx = make_unique_ptr<EVP_ENCODE_CTX_free>(EVP_ENCODE_CTX_new());
7575
EVP_EncodeInit(ctx.get());
7676
int size{};
7777
if(EVP_EncodeUpdate(ctx.get(), (unsigned char*)result.data(), &size, data.data(), int(data.size())) < 1)
@@ -232,7 +232,9 @@ struct XMLNode: public XMLElem<xmlNode>
232232
{
233233
if(!d)
234234
return *this;
235-
xmlNodeAddContentLen(d, pcxmlChar(text.data()), int(text.length()));
235+
xmlNodeSetContentLen(d, nullptr, 0);
236+
if(!text.empty())
237+
xmlNodeAddContentLen(d, pcxmlChar(text.data()), int(text.length()));
236238
return *this;
237239
}
238240

@@ -305,11 +307,11 @@ struct XMLDocument: public unique_free_t<xmlDoc>, public XMLNode
305307

306308
static XMLDocument openStream(std::istream &is, const XMLName &name = {}, bool hugeFile = false)
307309
{
308-
auto ctxt = make_unique_ptr(xmlCreateIOParserCtxt(nullptr, nullptr, [](void *context, char *buffer, int len) -> int {
310+
auto ctxt = make_unique_ptr<xmlFreeParserCtxt>(xmlCreateIOParserCtxt(nullptr, nullptr, [](void *context, char *buffer, int len) -> int {
309311
auto *is = static_cast<std::istream *>(context);
310312
is->read(buffer, len);
311313
return is->good() || is->eof() ? int(is->gcount()) : -1;
312-
}, nullptr, &is, XML_CHAR_ENCODING_NONE), xmlFreeParserCtxt);
314+
}, nullptr, &is, XML_CHAR_ENCODING_NONE));
313315
ctxt->linenumbers = 1;
314316
ctxt->options |= XML_PARSE_NOENT|XML_PARSE_DTDLOAD|XML_PARSE_DTDATTR|XML_PARSE_NONET;
315317
ctxt->loadsubset |= XML_DETECT_IDS|XML_COMPLETE_ATTRS;
@@ -358,11 +360,11 @@ struct XMLDocument: public unique_free_t<xmlDoc>, public XMLNode
358360
}
359361
else if(!algo.empty())
360362
THROW("Unsupported canonicalization method '%.*s'", int(algo.size()), algo.data());
361-
auto buf = make_unique_ptr(xmlOutputBufferCreateIO([](void *context, const char *buffer, int len) {
363+
auto buf = make_unique_ptr<xmlOutputBufferClose>(xmlOutputBufferCreateIO([](void *context, const char *buffer, int len) {
362364
auto *digest = static_cast<Digest *>(context);
363365
digest->update(pcxmlChar(buffer), size_t(len));
364366
return len;
365-
}, nullptr, const_cast<Digest*>(&digest), nullptr), xmlOutputBufferClose);
367+
}, nullptr, const_cast<Digest*>(&digest), nullptr));
366368
int size = xmlC14NExecute(get(), [](void *root, xmlNodePtr node, xmlNodePtr parent) constexpr noexcept {
367369
if(root == node)
368370
return 1;
@@ -396,14 +398,14 @@ struct XMLDocument: public unique_free_t<xmlDoc>, public XMLNode
396398

397399
void validateSchema(const std::string &schemaPath) const
398400
{
399-
auto parser = make_unique_ptr(xmlSchemaNewParserCtxt(schemaPath.c_str()), xmlSchemaFreeParserCtxt);
401+
auto parser = make_unique_ptr<xmlSchemaFreeParserCtxt>(xmlSchemaNewParserCtxt(schemaPath.c_str()));
400402
if(!parser)
401403
THROW("Failed to create schema parser context %s", schemaPath.c_str());
402404
xmlSchemaSetParserErrors(parser.get(), schemaValidationError, schemaValidationWarning, nullptr);
403-
auto schema = make_unique_ptr(xmlSchemaParse(parser.get()), xmlSchemaFree);
405+
auto schema = make_unique_ptr<xmlSchemaFree>(xmlSchemaParse(parser.get()));
404406
if(!schema)
405407
THROW("Failed to parse schema %s", schemaPath.c_str());
406-
auto validate = make_unique_ptr(xmlSchemaNewValidCtxt(schema.get()), xmlSchemaFreeValidCtxt);
408+
auto validate = make_unique_ptr<xmlSchemaFreeValidCtxt>(xmlSchemaNewValidCtxt(schema.get()));
407409
if(!validate)
408410
THROW("Failed to create schema validation context %s", schemaPath.c_str());
409411
Exception e(EXCEPTION_PARAMS("Failed to XML with schema"));
@@ -414,12 +416,12 @@ struct XMLDocument: public unique_free_t<xmlDoc>, public XMLNode
414416

415417
static bool verifySignature(XMLNode signature, [[maybe_unused]] Exception *e = {}) noexcept
416418
{
417-
auto mngr = make_unique_ptr(xmlSecKeysMngrCreate(), xmlSecKeysMngrDestroy);
419+
auto mngr = make_unique_ptr<xmlSecKeysMngrDestroy>(xmlSecKeysMngrCreate());
418420
if(!mngr)
419421
return false;
420422
if(xmlSecCryptoAppDefaultKeysMngrInit(mngr.get()) < 0)
421423
return false;
422-
auto ctx = make_unique_ptr(xmlSecDSigCtxCreate(mngr.get()), xmlSecDSigCtxDestroy);
424+
auto ctx = make_unique_ptr<xmlSecDSigCtxDestroy>(xmlSecDSigCtxCreate(mngr.get()));
423425
if(!ctx)
424426
return false;
425427
ctx->keyInfoReadCtx.flags |= XMLSEC_KEYINFO_FLAGS_X509DATA_DONT_VERIFY_CERTS;

0 commit comments

Comments
 (0)