Skip to content

Commit

Permalink
Libxml errors (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
lsh123 committed Nov 24, 2016
1 parent fd18076 commit 8c82253
Show file tree
Hide file tree
Showing 18 changed files with 312 additions and 542 deletions.
2 changes: 1 addition & 1 deletion HACKING
Expand Up @@ -100,7 +100,7 @@ Igor Zlatkovic get an exception for the send before commit rule.
xmlSecMallocError() aka XMLSEC_ERRORS_R_MALLOC_FAILED
else if the function starts with "xml" or "xslt" (i.e. it comes
from libxml or libxslt) then use
XMLSEC_ERRORS_R_XML_FAILED
xmlSecXmlError/xmlSecXmlParserError aka XMLSEC_ERRORS_R_XML_FAILED
else if it is related to IO (fopen, fread, fwrite, etc.) then use
XMLSEC_ERRORS_R_IO_FAILED
else if the function could be used only from xmlsec-crypto (i.e.
Expand Down
98 changes: 97 additions & 1 deletion include/xmlsec/errors.h
Expand Up @@ -552,14 +552,110 @@ XMLSEC_EXPORT void xmlSecError (const char* file,
*
* Macro. The XMLSec library macro for reporting xmlStrdup() errors.
*/
#define xmlSecStrdupError(str, errorObject) \
#define xmlSecStrdupError(str, errorObject) \
xmlSecError(XMLSEC_ERRORS_HERE, \
(const char*)(errorObject), \
"xmlStrdup", \
XMLSEC_ERRORS_R_STRDUP_FAILED, \
"size=%lu", (unsigned long)xmlStrlen(str) \
)

/**
* xmlSecXmlError:
* @errorFunction: the failed function.
* @errorObject: the error specific error object (e.g. transform, key data, etc).
*
* Macro. The XMLSec library macro for reporting xmlStrdup() errors.
*/
#define xmlSecXmlError(errorFunction, errorObject) \
{ \
xmlErrorPtr error = xmlGetLastError(); \
int code = (error != NULL) ? error->code : 0; \
const char* message = (error != NULL) ? error->message : NULL; \
xmlSecError(XMLSEC_ERRORS_HERE, \
(const char*)(errorObject), \
(errorFunction), \
XMLSEC_ERRORS_R_XML_FAILED, \
"xml error: %lu: %s", \
(unsigned long)code, \
xmlSecErrorsSafeString(message) \
); \
}

/**
* xmlSecXmlError2:
* @errorFunction: the failed function.
* @errorObject: the error specific error object (e.g. transform, key data, etc).
* @msg: the extra message.
* @param: the extra message param.
*
* Macro. The XMLSec library macro for reporting xmlStrdup() errors.
*/
#define xmlSecXmlError2(errorFunction, errorObject, msg, param) \
{ \
xmlErrorPtr error = xmlGetLastError(); \
int code = (error != NULL) ? error->code : 0; \
const char* message = (error != NULL) ? error->message : NULL; \
xmlSecError(XMLSEC_ERRORS_HERE, \
(const char*)(errorObject), \
(errorFunction), \
XMLSEC_ERRORS_R_XML_FAILED, \
msg "; xml error: %lu: %s", \
(param), \
(unsigned long)code, \
xmlSecErrorsSafeString(message) \
); \
}

/**
* xmlSecXmlParserError:
* @errorFunction: the failed function.
* @errorObject: the error specific error object (e.g. transform, key data, etc).
* @ctxt: the parser context.
*
* Macro. The XMLSec library macro for reporting xmlStrdup() errors.
*/
#define xmlSecXmlParserError(errorFunction, ctxt, errorObject) \
{ \
xmlErrorPtr error = xmlCtxtGetLastError(ctxt); \
int code = (error != NULL) ? error->code : 0; \
const char* message = (error != NULL) ? error->message : NULL; \
xmlSecError(XMLSEC_ERRORS_HERE, \
(const char*)(errorObject), \
(errorFunction), \
XMLSEC_ERRORS_R_XML_FAILED, \
"xml error: %lu: %s", \
(unsigned long)code, \
xmlSecErrorsSafeString(message) \
); \
}

/**
* xmlSecXmlParserError2:
* @errorFunction: the failed function.
* @errorObject: the error specific error object (e.g. transform, key data, etc).
* @ctxt: the parser context.
* @msg: the extra message.
* @param: the extra message param.
*
* Macro. The XMLSec library macro for reporting xmlStrdup() errors.
*/
#define xmlSecXmlParserError2(errorFunction, ctxt, errorObject, msg, param) \
{ \
xmlErrorPtr error = xmlCtxtGetLastError(ctxt); \
int code = (error != NULL) ? error->code : 0; \
const char* message = (error != NULL) ? error->message : NULL; \
xmlSecError(XMLSEC_ERRORS_HERE, \
(const char*)(errorObject), \
(errorFunction), \
XMLSEC_ERRORS_R_XML_FAILED, \
msg "; xml error: %lu: %s", \
(param), \
(unsigned long)code, \
xmlSecErrorsSafeString(message) \
); \
}

/**********************************************************************
*
* Assertions
Expand Down
12 changes: 2 additions & 10 deletions src/bn.c
Expand Up @@ -798,11 +798,7 @@ xmlSecBnGetNodeValue(xmlSecBnPtr bn, xmlNodePtr cur, xmlSecBnFormat format, int
case xmlSecBnHex:
content = xmlNodeGetContent(cur);
if(content == NULL) {
xmlSecError(XMLSEC_ERRORS_HERE,
NULL,
"xmlNodeGetContent",
XMLSEC_ERRORS_R_XML_FAILED,
XMLSEC_ERRORS_NO_MESSAGE);
xmlSecXmlError("xmlNodeGetContent", NULL);
return(-1);
}
ret = xmlSecBnFromHexString(bn, content);
Expand All @@ -816,11 +812,7 @@ xmlSecBnGetNodeValue(xmlSecBnPtr bn, xmlNodePtr cur, xmlSecBnFormat format, int
case xmlSecBnDec:
content = xmlNodeGetContent(cur);
if(content == NULL) {
xmlSecError(XMLSEC_ERRORS_HERE,
NULL,
"xmlNodeGetContent",
XMLSEC_ERRORS_R_XML_FAILED,
XMLSEC_ERRORS_NO_MESSAGE);
xmlSecXmlError("xmlNodeGetContent", NULL);
return(-1);
}
ret = xmlSecBnFromDecString(bn, content);
Expand Down
18 changes: 3 additions & 15 deletions src/c14n.c
Expand Up @@ -254,11 +254,7 @@ xmlSecTransformC14NPushXml(xmlSecTransformPtr transform, xmlSecNodeSetPtr nodes,

ret = xmlOutputBufferClose(buf);
if(ret < 0) {
xmlSecError(XMLSEC_ERRORS_HERE,
xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
"xmlOutputBufferClose",
XMLSEC_ERRORS_R_XML_FAILED,
XMLSEC_ERRORS_NO_MESSAGE);
xmlSecXmlError("xmlOutputBufferClose", xmlSecTransformGetName(transform));
return(-1);
}
transform->status = xmlSecTransformStatusFinished;
Expand Down Expand Up @@ -321,11 +317,7 @@ xmlSecTransformC14NPopBin(xmlSecTransformPtr transform, xmlSecByte* data,
}
ret = xmlOutputBufferClose(buf);
if(ret < 0) {
xmlSecError(XMLSEC_ERRORS_HERE,
xmlSecErrorsSafeString(xmlSecTransformGetName(transform)),
"xmlOutputBufferClose",
XMLSEC_ERRORS_R_XML_FAILED,
XMLSEC_ERRORS_NO_MESSAGE);
xmlSecXmlError("xmlOutputBufferClose", xmlSecTransformGetName(transform));
return(-1);
}
transform->status = xmlSecTransformStatusWorking;
Expand Down Expand Up @@ -421,11 +413,7 @@ xmlSecTransformC14NExecute(xmlSecTransformId id, xmlSecNodeSetPtr nodes, xmlChar
}

if(ret < 0) {
xmlSecError(XMLSEC_ERRORS_HERE,
xmlSecErrorsSafeString(xmlSecTransformKlassGetName(id)),
"xmlC14NExecute",
XMLSEC_ERRORS_R_XML_FAILED,
XMLSEC_ERRORS_NO_MESSAGE);
xmlSecXmlError("xmlC14NExecute", xmlSecTransformKlassGetName(id));
return(-1);
}

Expand Down
12 changes: 2 additions & 10 deletions src/keyinfo.c
Expand Up @@ -1180,21 +1180,13 @@ xmlSecKeyDataRetrievalMethodReadXmlResult(xmlSecKeyDataId typeId, xmlSecKeyPtr k

doc = xmlRecoverMemory((const char*)buffer, bufferSize);
if(doc == NULL) {
xmlSecError(XMLSEC_ERRORS_HERE,
xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(typeId)),
"xmlRecoverMemory",
XMLSEC_ERRORS_R_XML_FAILED,
XMLSEC_ERRORS_NO_MESSAGE);
xmlSecXmlError("xmlRecoverMemory", xmlSecKeyDataKlassGetName(typeId));
return(-1);
}

cur = xmlDocGetRootElement(doc);
if(cur == NULL) {
xmlSecError(XMLSEC_ERRORS_HERE,
xmlSecErrorsSafeString(xmlSecKeyDataKlassGetName(typeId)),
"xmlDocGetRootElement",
XMLSEC_ERRORS_R_XML_FAILED,
XMLSEC_ERRORS_NO_MESSAGE);
xmlSecXmlError("xmlDocGetRootElement", xmlSecKeyDataKlassGetName(typeId));
xmlFreeDoc(doc);
return(-1);
}
Expand Down
16 changes: 4 additions & 12 deletions src/keysmngr.c
Expand Up @@ -394,12 +394,8 @@ xmlSecSimpleKeysStoreLoad(xmlSecKeyStorePtr store, const char *uri,

doc = xmlParseFile(uri);
if(doc == NULL) {
xmlSecError(XMLSEC_ERRORS_HERE,
xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
"xmlParseFile",
XMLSEC_ERRORS_R_XML_FAILED,
"uri=%s",
xmlSecErrorsSafeString(uri));
xmlSecXmlError2("xmlParseFile", xmlSecKeyStoreGetName(store),
"uri=%s", xmlSecErrorsSafeString(uri));
return(-1);
}

Expand Down Expand Up @@ -607,12 +603,8 @@ xmlSecSimpleKeysStoreSave(xmlSecKeyStorePtr store, const char *filename, xmlSecK
/* now write result */
ret = xmlSaveFormatFile(filename, doc, 1);
if(ret < 0) {
xmlSecError(XMLSEC_ERRORS_HERE,
xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
"xmlSaveFormatFile",
XMLSEC_ERRORS_R_XML_FAILED,
"filename=%s",
xmlSecErrorsSafeString(filename));
xmlSecXmlError2("xmlSaveFormatFile", xmlSecKeyStoreGetName(store),
"filename=%s", xmlSecErrorsSafeString(filename));
xmlFreeDoc(doc);
return(-1);
}
Expand Down
8 changes: 2 additions & 6 deletions src/mscrypto/keysstore.c
Expand Up @@ -146,12 +146,8 @@ xmlSecMSCryptoKeysStoreLoad(xmlSecKeyStorePtr store, const char *uri,

doc = xmlParseFile(uri);
if(doc == NULL) {
xmlSecError(XMLSEC_ERRORS_HERE,
xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
"xmlParseFile",
XMLSEC_ERRORS_R_XML_FAILED,
"uri=%s",
xmlSecErrorsSafeString(uri));
xmlSecXmlError2("xmlParseFile", xmlSecKeyStoreGetName(store),
"uri=%s", xmlSecErrorsSafeString(uri));
return(-1);
}

Expand Down
6 changes: 1 addition & 5 deletions src/nodeset.c
Expand Up @@ -464,11 +464,7 @@ xmlSecNodeSetGetChildren(xmlDocPtr doc, const xmlNodePtr parent, int withComment

nodes = xmlXPathNodeSetCreate(parent);
if(nodes == NULL) {
xmlSecError(XMLSEC_ERRORS_HERE,
NULL,
"xmlXPathNodeSetCreate",
XMLSEC_ERRORS_R_XML_FAILED,
XMLSEC_ERRORS_NO_MESSAGE);
xmlSecXmlError("xmlXPathNodeSetCreate", NULL);
return(NULL);
}

Expand Down
8 changes: 2 additions & 6 deletions src/nss/keysstore.c
Expand Up @@ -139,12 +139,8 @@ xmlSecNssKeysStoreLoad(xmlSecKeyStorePtr store, const char *uri,

doc = xmlParseFile(uri);
if(doc == NULL) {
xmlSecError(XMLSEC_ERRORS_HERE,
xmlSecErrorsSafeString(xmlSecKeyStoreGetName(store)),
"xmlParseFile",
XMLSEC_ERRORS_R_XML_FAILED,
"uri=%s",
xmlSecErrorsSafeString(uri));
xmlSecXmlError2("xmlParseFile", xmlSecKeyStoreGetName(store),
"uri=%s", xmlSecErrorsSafeString(uri));
return(-1);
}

Expand Down

0 comments on commit 8c82253

Please sign in to comment.