blautenb    2003/02/18 03:28:13

  Modified:    c/src/dsig DSIGSignature.cpp DSIGTransformXPath.cpp
               c/src/tools/checksig checksig.cpp
               c/src/transformers TXFMBase.cpp TXFMXPath.cpp
               c/src/utils XSECSafeBuffer.cpp
  Log:
  Catch and clean up after Xalan exceptions
  
  Revision  Changes    Path
  1.4       +11 -2     xml-security/c/src/dsig/DSIGSignature.cpp
  
  Index: DSIGSignature.cpp
  ===================================================================
  RCS file: /home/cvs/xml-security/c/src/dsig/DSIGSignature.cpp,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DSIGSignature.cpp 9 Feb 2003 11:13:47 -0000       1.3
  +++ DSIGSignature.cpp 18 Feb 2003 11:28:12 -0000      1.4
  @@ -95,8 +95,10 @@
   // Xerces includes
   
   #include <xercesc/dom/DOMNamedNodeMap.hpp>
  +#include <xercesc/util/Janitor.hpp>
   
   XSEC_USING_XERCES(DOMNamedNodeMap);
  +XSEC_USING_XERCES(Janitor);
   
   // 
--------------------------------------------------------------------------------
   //           Some useful utility functions
  @@ -798,11 +800,18 @@
                                        
DSIGTransformList::TransformListVectorType::size_type size, i;
                                        size = l->getSize();
                                        for (i = 0; i < size; ++ i) {
  -                                             currentTxfm = 
l->item(i)->createTransformer(currentTxfm);
  +                                             try {
  +                                                     currentTxfm = 
l->item(i)->createTransformer(currentTxfm);
  +                                             }
  +                                             catch (...) {
  +                                                     
deleteTXFMChain(currentTxfm);
  +                                                     delete l;
  +                                                     throw;
  +                                             }
                                        }
   
                                        delete l;
  -                     
  +
                                }
   
                                // Find out the type of the final transform and 
process accordingly
  
  
  
  1.4       +1 -3      xml-security/c/src/dsig/DSIGTransformXPath.cpp
  
  Index: DSIGTransformXPath.cpp
  ===================================================================
  RCS file: /home/cvs/xml-security/c/src/dsig/DSIGTransformXPath.cpp,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- DSIGTransformXPath.cpp    17 Feb 2003 11:23:38 -0000      1.3
  +++ DSIGTransformXPath.cpp    18 Feb 2003 11:28:12 -0000      1.4
  @@ -64,9 +64,7 @@
    *
    * Author(s): Berin Lautenbach
    *
  - * $ID$
  - *
  - * $LOG$
  + * $Id$
    *
    */
   
  
  
  
  1.6       +12 -1     xml-security/c/src/tools/checksig/checksig.cpp
  
  Index: checksig.cpp
  ===================================================================
  RCS file: /home/cvs/xml-security/c/src/tools/checksig/checksig.cpp,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- checksig.cpp      17 Feb 2003 11:22:39 -0000      1.5
  +++ checksig.cpp      18 Feb 2003 11:28:12 -0000      1.6
  @@ -67,6 +67,9 @@
    * $Id$
    *
    * $Log$
  + * Revision 1.6  2003/02/18 11:28:12  blautenb
  + * Catch and clean up after Xalan exceptions
  + *
    * Revision 1.5  2003/02/17 11:22:39  blautenb
    * Now handle relative file URIs in references
    *
  @@ -378,6 +381,14 @@
   
                ERR_print_errors(bio_err);
                return 2;
  +     }
  +
  +     catch (...) {
  +
  +             cerr << "Unknown Exception type occured.  Cleaning up and 
exiting\n" << endl;
  +
  +             return 2;
  +
        }
   
        int retResult;
  
  
  
  1.3       +1 -3      xml-security/c/src/transformers/TXFMBase.cpp
  
  Index: TXFMBase.cpp
  ===================================================================
  RCS file: /home/cvs/xml-security/c/src/transformers/TXFMBase.cpp,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TXFMBase.cpp      9 Feb 2003 11:13:51 -0000       1.2
  +++ TXFMBase.cpp      18 Feb 2003 11:28:12 -0000      1.3
  @@ -65,9 +65,7 @@
    *
    * Author(s): Berin Lautenbach
    *
  - * $ID$
  - *
  - * $LOG$
  + * $Id$
    *
    */
   
  
  
  
  1.4       +137 -116  xml-security/c/src/transformers/TXFMXPath.cpp
  
  Index: TXFMXPath.cpp
  ===================================================================
  RCS file: /home/cvs/xml-security/c/src/transformers/TXFMXPath.cpp,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TXFMXPath.cpp     17 Feb 2003 11:21:03 -0000      1.3
  +++ TXFMXPath.cpp     18 Feb 2003 11:28:12 -0000      1.4
  @@ -66,11 +66,6 @@
    *
    * $Id$
    *
  - * $Log$
  - * Revision 1.3  2003/02/17 11:21:03  blautenb
  - * Bug fix to ensure duplicate XPath namespace does not delete document ns
  - *
  - *
    */
   
   
  @@ -104,6 +99,7 @@
   XALAN_USING_XALAN(XPath)
   XALAN_USING_XALAN(NodeRefListBase)
   XALAN_USING_XALAN(XSLTResultTarget)
  +XALAN_USING_XALAN(XSLException)
   
   #endif
   
  @@ -328,187 +324,209 @@
        XalanDocument           * xd;
        XalanNode                       * contextNode;
   
  +     // Xalan can throw exceptions in all functions, so do one broad catch 
point.
  +
  +     try {
        
  -     // Map to Xalan
  -     xd = xpl.createDocument(document);
  +             // Map to Xalan
  +             xd = xpl.createDocument(document);
   
  -     // For performing mapping
  -     XercesDocumentWrapper *xdw = xpl.mapDocumentToWrapper(xd);
  -     XercesWrapperNavigator xwn(xdw);
  +             // For performing mapping
  +             XercesDocumentWrapper *xdw = xpl.mapDocumentToWrapper(xd);
  +             XercesWrapperNavigator xwn(xdw);
   
  -     // Map the "here" node - but only if part of current document
  +             // Map the "here" node - but only if part of current document
   
  -     bool haveHereNode;
  -     XalanNode * hereNode;
  +             bool haveHereNode;
  +             XalanNode * hereNode;
   
  -     if (h->getOwnerDocument() == document) {
  -             
  -             hereNode = xwn.mapNode(h);
  +             if (h->getOwnerDocument() == document) {
  +                     
  +                     hereNode = xwn.mapNode(h);
   
  -             if (hereNode == NULL) {
  +                     if (hereNode == NULL) {
   
  -                     hereNode = findHereNodeFromXalan(&xwn, xd, h);
  +                             hereNode = findHereNodeFromXalan(&xwn, xd, h);
   
  -                     if (hereNode == NULL) {
  +                             if (hereNode == NULL) {
   
  -                             throw XSECException(XSECException::XPathError,
  -                                "Unable to find here node in Xalan Wrapper 
map");
  -                     }
  +                                     throw 
XSECException(XSECException::XPathError,
  +                                        "Unable to find here node in Xalan 
Wrapper map");
  +                             }
   
  +                     }
  +                     haveHereNode = true;
                }
  -             haveHereNode = true;
  -     }
  -     else
  -             haveHereNode = false;
  +             else
  +                     haveHereNode = false;
   
  -     // Now work out what we have to set up in the new processing
  +             // Now work out what we have to set up in the new processing
   
  -     TXFMBase::nodeType inputType = input->getNodeType();
  +             TXFMBase::nodeType inputType = input->getNodeType();
   
  -     XalanDOMString cd;              // For the moment assume the root is 
the context
  +             XalanDOMString cd;              // For the moment assume the 
root is the context
   
  -     const XalanDOMChar * cexpr;
  +             const XalanDOMChar * cexpr;
   
  -     safeBuffer contextExpr;
  +             safeBuffer contextExpr;
   
  -     switch (inputType) {
  +             switch (inputType) {
   
  -     case DOM_NODE_DOCUMENT :
  +             case DOM_NODE_DOCUMENT :
   
  -             cd = XalanDOMString("/");               // Root node
  -             cexpr = cd.c_str();
  +                     cd = XalanDOMString("/");               // Root node
  +                     cexpr = cd.c_str();
   
  -             // The context node is the "root" node
  -             contextNode =
  -                     xpe.selectSingleNode(
  -                     xds,
  -                     xd,
  -                     cexpr,
  -                     xd->getDocumentElement());
  +                     // The context node is the "root" node
  +                     contextNode =
  +                             xpe.selectSingleNode(
  +                             xds,
  +                             xd,
  +                             cexpr,
  +                             xd->getDocumentElement());
   
  -             break;
  +                     break;
   
  -     case DOM_NODE_DOCUMENT_FRAGMENT :
  -             {
  +             case DOM_NODE_DOCUMENT_FRAGMENT :
  +                     {
   
  -                     // Need to map the DOM_Node that we are given from the 
input to the appropriate XalanNode
  +                             // Need to map the DOM_Node that we are given 
from the input to the appropriate XalanNode
   
  -                     // Create the XPath expression to find the node
  +                             // Create the XPath expression to find the node
   
  -                     if (input->getFragmentId() != NULL) {
  +                             if (input->getFragmentId() != NULL) {
   
  -                             
contextExpr.sbTranscodeIn("//descendant-or-self::node()[attribute::Id='");
  -                             contextExpr.sbXMLChCat(input->getFragmentId());
  -                             contextExpr.sbXMLChCat("']");
  +                                     
contextExpr.sbTranscodeIn("//descendant-or-self::node()[attribute::Id='");
  +                                     
contextExpr.sbXMLChCat(input->getFragmentId());
  +                                     contextExpr.sbXMLChCat("']");
   
  -                             // Map the node
  +                                     // Map the node
   
  -                             contextNode = 
  -                                     xpe.selectSingleNode(
  -                                     xds,
  -                                     xd,
  -                                     contextExpr.rawXMLChBuffer(), 
//XalanDOMString((char *) contextExpr.rawBuffer()).c_str(), 
  -                                     xd->getDocumentElement());
  +                                     contextNode = 
  +                                             xpe.selectSingleNode(
  +                                             xds,
  +                                             xd,
  +                                             contextExpr.rawXMLChBuffer(), 
//XalanDOMString((char *) contextExpr.rawBuffer()).c_str(), 
  +                                             xd->getDocumentElement());
   
   
  -                             if (contextNode == NULL) {
  -                                     // Last Ditch
  -                                     contextNode = 
xwn.mapNode(input->getFragmentNode());
  +                                     if (contextNode == NULL) {
  +                                             // Last Ditch
  +                                             contextNode = 
xwn.mapNode(input->getFragmentNode());
  +
  +                                     }
   
                                }
  +                             else
  +                                     contextNode = 
xwn.mapNode(input->getFragmentNode());
   
  -                     }
  -                     else
  -                             contextNode = 
xwn.mapNode(input->getFragmentNode());
  +                             if (contextNode == NULL) {
   
  -                     if (contextNode == NULL) {
  +                                     // Something wrong
  +                                     throw 
XSECException(XSECException::XPathError, "Error mapping context node");
   
  -                             // Something wrong
  -                             throw XSECException(XSECException::XPathError, 
"Error mapping context node");
  +                             }
   
  +                             break;
                        }
   
  -                     break;
  -             }
  +             default :
   
  -     default :
  +                     throw XSECException(XSECException::XPathError); // 
Should never get here
   
  -             throw XSECException(XSECException::XPathError); // Should never 
get here
  +             }
   
  -     }
  +             safeBuffer str;
  +             XPathEnvSupportDefault xpesd;
  +             XObjectFactoryDefault                   xof;
  +             XPathExecutionContextDefault    xpec(xpesd, xds, xof);
  +
  +             ElementPrefixResolverProxy pr(xd->getDocumentElement(), xpesd, 
xds);
   
  -     safeBuffer str;
  -     XPathEnvSupportDefault xpesd;
  -     XObjectFactoryDefault                   xof;
  -     XPathExecutionContextDefault    xpec(xpesd, xds, xof);
  +             // Work around the fact that the XPath implementation is 
designed for XSLT, so does
  +             // not allow here() as a NCName.
   
  -     ElementPrefixResolverProxy pr(xd->getDocumentElement(), xpesd, xds);
  +             // THIS IS A KLUDGE AND SHOULD BE DONE BETTER
   
  -     // Work around the fact that the XPath implementation is designed for 
XSLT, so does
  -     // not allow here() as a NCName.
  +             int offset = 0;
  +             safeBuffer k(KLUDGE_PREFIX);
  +             k.sbStrcatIn(":");
   
  -     // THIS IS A KLUDGE AND SHOULD BE DONE BETTER
  +             offset = expr.sbStrstr("here()");
   
  -     int offset = 0;
  -     safeBuffer k(KLUDGE_PREFIX);
  -     k.sbStrcatIn(":");
  +             while (offset >= 0) {
   
  -     offset = expr.sbStrstr("here()");
  +                     if (offset == 0 || offset == 1 || 
  +                             (!(expr[offset - 1] == ':' && expr[offset - 2] 
!= ':') &&
  +                             separator(expr[offset - 1]))) {
   
  -     while (offset >= 0) {
  +                             expr.sbStrinsIn(k.rawCharBuffer(), offset);
   
  -             if (offset == 0 || offset == 1 || 
  -                     (!(expr[offset - 1] == ':' && expr[offset - 2] != ':') 
&&
  -                     separator(expr[offset - 1]))) {
  +                     }
   
  -                     expr.sbStrinsIn(k.rawCharBuffer(), offset);
  +                     offset = expr.sbOffsetStrstr("here()", offset + 11);
   
                }
   
  -             offset = expr.sbOffsetStrstr("here()", offset + 11);
  +             // Install the External function in the Environment handler
   
  -     }
  +             if (haveHereNode) {
   
  -     // Install the External function in the Environment handler
  +                     
xpesd.installExternalFunctionLocal(XalanDOMString(URI_ID_DSIG), 
XalanDOMString("here"), DSIGXPathHere(hereNode));
   
  -     if (haveHereNode) {
  +             }
   
  -             xpesd.installExternalFunctionLocal(XalanDOMString(URI_ID_DSIG), 
XalanDOMString("here"), DSIGXPathHere(hereNode));
  +             str.sbStrcpyIn("(descendant-or-self::node() | 
descendant-or-self::node()/attribute::* | 
descendant-or-self::node()/namespace::*)[");
  +             str.sbStrcatIn(expr);
  +             str.sbStrcatIn("]");
   
  -     }
  +             XPath * xp = xpf.create();
   
  -     str.sbStrcpyIn("(descendant-or-self::node() | 
descendant-or-self::node()/attribute::* | 
descendant-or-self::node()/namespace::*)[");
  -     str.sbStrcatIn(expr);
  -     str.sbStrcatIn("]");
  +             XalanDOMString Xexpr((char *) str.rawBuffer());
  +             xppi.initXPath(*xp, xpcc, Xexpr, pr);
  +             
  +             // Now resolve
   
  -     XPath * xp = xpf.create();
  +             XObjectPtr xObj = xp->execute(contextNode, pr, xpec);
   
  -     XalanDOMString Xexpr((char *) str.rawBuffer());
  -     xppi.initXPath(*xp, xpcc, Xexpr, pr);
  -     
  -     // Now resolve
  +             // Now map to a list that others can use (naieve list at this 
time)
   
  -     XObjectPtr xObj = xp->execute(contextNode, pr, xpec);
  +             const NodeRefListBase&  lst = xObj->nodeset();
  +             
  +             int size = lst.getLength();
  +             const DOMNode *item;
  +             
  +             for (int i = 0; i < size; ++ i) {
   
  -     // Now map to a list that others can use (naieve list at this time)
  +                     if (lst.item(i) == xd)
  +                             m_XPathMap.addNode(document);
  +                     else {
  +                             item = xwn.mapNode(lst.item(i));
  +                             m_XPathMap.addNode(item);
  +                     }
  +             }
   
  -     const NodeRefListBase&  lst = xObj->nodeset();
  -     
  -     int size = lst.getLength();
  -     const DOMNode *item;
  -     
  -     for (int i = 0; i < size; ++ i) {
  +             
xpesd.uninstallExternalFunctionGlobal(XalanDOMString(URI_ID_DSIG), 
XalanDOMString("here"));
   
  -             if (lst.item(i) == xd)
  -                     m_XPathMap.addNode(document);
  -             else {
  -                     item = xwn.mapNode(lst.item(i));
  -                     m_XPathMap.addNode(item);
  -             }
        }
   
  -     xpesd.uninstallExternalFunctionGlobal(XalanDOMString(URI_ID_DSIG), 
XalanDOMString("here"));
  +     catch (XSLException &e) {
   
  +             safeBuffer msg;
  +
  +             // Whatever happens - fix any changes to the original document
  +             clearXPathNS(document, addedNodes, formatter, mp_nse);
  +     
  +             // Collate the exception message into an XSEC message.          
  +             msg.sbTranscodeIn("Xalan Exception : ");
  +             msg.sbXMLChCat(e.getType().c_str());
  +             msg.sbXMLChCat(" caught.  Message : ");
  +             msg.sbXMLChCat(e.getMessage().c_str());
  +
  +             throw XSECException(XSECException::XPathError,
  +                     msg.rawXMLChBuffer());
  +     }
  +     
        clearXPathNS(document, addedNodes, formatter, mp_nse);
   
   }
  
  
  
  1.3       +2 -4      xml-security/c/src/utils/XSECSafeBuffer.cpp
  
  Index: XSECSafeBuffer.cpp
  ===================================================================
  RCS file: /home/cvs/xml-security/c/src/utils/XSECSafeBuffer.cpp,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- XSECSafeBuffer.cpp        9 Feb 2003 11:13:52 -0000       1.2
  +++ XSECSafeBuffer.cpp        18 Feb 2003 11:28:12 -0000      1.3
  @@ -64,9 +64,7 @@
    *
    * Author(s): Berin Lautenbach
    *
  - * $ID$
  - *
  - * $LOG$
  + * $Id$
    *
    */
   
  @@ -524,7 +522,7 @@
        
        checkBufferType(BUFFER_UNICODE);
        unsigned int len = XMLString::stringLen((XMLCh *) buffer) * size_XMLCh;
  -     len += XMLString::stringLen(str);
  +     len += XMLString::stringLen(str) * size_XMLCh;
        len += (2 * size_XMLCh);
   
        checkAndExpand(len);
  
  
  

Reply via email to