Bug 640339: Make generate-id() generate safer ids. r=peterv
authorJonas Sicking <jonas@sicking.cc>
Mon, 11 Apr 2011 18:28:28 -0700
changeset 67876 c2bd5cf4070e0794a5dcd197f904fdbfd18ac0cf
parent 67875 0cfe6840e0a4a771e290a94c6a31101f9beaf9f5
child 67877 bc035b7a2aab1e31f3e7c2c19e32d2f05dc0ae74
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerspeterv
bugs640339
milestone2.2a1pre
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 640339: Make generate-id() generate safer ids. r=peterv
content/xslt/src/xpath/txMozillaXPathTreeWalker.cpp
content/xslt/src/xpath/txXPathTreeWalker.h
content/xslt/src/xslt/txGenerateIdFunctionCall.cpp
--- a/content/xslt/src/xpath/txMozillaXPathTreeWalker.cpp
+++ b/content/xslt/src/xpath/txMozillaXPathTreeWalker.cpp
@@ -609,25 +609,27 @@ const char gPrintfFmtAttr[] = "id0x%08p-
 #define kFmtSizeAttr 32
 const char gPrintfFmt[] = "id0x%016p";
 const char gPrintfFmtAttr[] = "id0x%016p-%010i";
 #endif
 
 /* static */
 nsresult
 txXPathNodeUtils::getXSLTId(const txXPathNode& aNode,
+                            const txXPathNode& aBase,
                             nsAString& aResult)
 {
+    PRUword nodeid = ((PRUword)aNode.mNode) - ((PRUword)aBase.mNode);
     if (!aNode.isAttribute()) {
-        CopyASCIItoUTF16(nsPrintfCString(kFmtSize, gPrintfFmt, aNode.mNode),
+        CopyASCIItoUTF16(nsPrintfCString(kFmtSize, gPrintfFmt, nodeid),
                          aResult);
     }
     else {
         CopyASCIItoUTF16(nsPrintfCString(kFmtSizeAttr, gPrintfFmtAttr,
-                                         aNode.mNode, aNode.mIndex), aResult);
+                                         nodeid, aNode.mIndex), aResult);
     }
 
     return NS_OK;
 }
 
 /* static */
 void
 txXPathNodeUtils::getBaseURI(const txXPathNode& aNode, nsAString& aURI)
--- a/content/xslt/src/xpath/txXPathTreeWalker.h
+++ b/content/xslt/src/xpath/txXPathTreeWalker.h
@@ -125,17 +125,18 @@ public:
     static PRInt32 getNamespaceID(const txXPathNode& aNode);
     static void getNamespaceURI(const txXPathNode& aNode, nsAString& aURI);
     static PRUint16 getNodeType(const txXPathNode& aNode);
     static void appendNodeValue(const txXPathNode& aNode, nsAString& aResult);
     static PRBool isWhitespace(const txXPathNode& aNode);
     static txXPathNode* getDocument(const txXPathNode& aNode);
     static txXPathNode* getOwnerDocument(const txXPathNode& aNode);
     static PRInt32 getUniqueIdentifier(const txXPathNode& aNode);
-    static nsresult getXSLTId(const txXPathNode& aNode, nsAString& aResult);
+    static nsresult getXSLTId(const txXPathNode& aNode,
+                              const txXPathNode& aBase, nsAString& aResult);
     static void release(txXPathNode* aNode);
     static void getBaseURI(const txXPathNode& aNode, nsAString& aURI);
     static PRIntn comparePosition(const txXPathNode& aNode,
                                   const txXPathNode& aOtherNode);
     static PRBool localNameEquals(const txXPathNode& aNode,
                                   nsIAtom* aLocalName);
     static PRBool isRoot(const txXPathNode& aNode);
     static PRBool isElement(const txXPathNode& aNode);
--- a/content/xslt/src/xslt/txGenerateIdFunctionCall.cpp
+++ b/content/xslt/src/xslt/txGenerateIdFunctionCall.cpp
@@ -35,16 +35,17 @@
  *
  * ***** END LICENSE BLOCK ***** */
 
 #include "txAtoms.h"
 #include "txIXPathContext.h"
 #include "txNodeSet.h"
 #include "txXPathTreeWalker.h"
 #include "txXSLTFunctions.h"
+#include "txExecutionState.h"
 
 /*
   Implementation of XSLT 1.0 extension function: generate-id
 */
 
 /**
  * Creates a new generate-id function call
 **/
@@ -63,23 +64,32 @@ GenerateIdFunctionCall::GenerateIdFuncti
 nsresult
 GenerateIdFunctionCall::evaluate(txIEvalContext* aContext,
                                  txAExprResult** aResult)
 {
     *aResult = nsnull;
     if (!requireParams(0, 1, aContext))
         return NS_ERROR_XPATH_BAD_ARGUMENT_COUNT;
 
+    txExecutionState* es = 
+        static_cast<txExecutionState*>(aContext->getPrivateContext());
+    if (!es) {
+        NS_ERROR(
+            "called xslt extension function \"current\" with wrong context");
+        return NS_ERROR_UNEXPECTED;
+    }
+
     nsresult rv = NS_OK;
     if (mParams.IsEmpty()) {
         StringResult* strRes;
         rv = aContext->recycler()->getStringResult(&strRes);
         NS_ENSURE_SUCCESS(rv, rv);
 
         txXPathNodeUtils::getXSLTId(aContext->getContextNode(),
+                                    es->getSourceDocument(),
                                     strRes->mValue);
 
         *aResult = strRes;
  
         return NS_OK;
     }
 
     nsRefPtr<txNodeSet> nodes;
@@ -92,17 +102,18 @@ GenerateIdFunctionCall::evaluate(txIEval
 
         return NS_OK;
     }
     
     StringResult* strRes;
     rv = aContext->recycler()->getStringResult(&strRes);
     NS_ENSURE_SUCCESS(rv, rv);
 
-    txXPathNodeUtils::getXSLTId(nodes->get(0), strRes->mValue);
+    txXPathNodeUtils::getXSLTId(nodes->get(0), es->getSourceDocument(),
+                                strRes->mValue);
 
     *aResult = strRes;
  
     return NS_OK;
 }
 
 Expr::ResultType
 GenerateIdFunctionCall::getReturnType()