Bug 1458016: Fix bad cast to Text. r?bz draft
authorEmilio Cobos Álvarez <emilio@crisal.io>
Mon, 30 Apr 2018 22:03:33 +0200
changeset 790023 ee9dc9487796b6d016275439d7fe930c50497980
parent 789955 9ef753b5c31dd9fbb94049a80c5a919f0ac5fdd5
child 790061 d4284a40b404dbca9e5c77f1de228e62a5e218e6
child 790082 4bf2cd2eefb7d164f2047107151b5f185a16b088
push id108393
push userbmo:emilio@crisal.io
push dateTue, 01 May 2018 00:31:37 +0000
reviewersbz
bugs1458016
milestone61.0a1
Bug 1458016: Fix bad cast to Text. r?bz I think it's harmless security-wise, since Text and char data are both nsIContent, so the vtable offset should be the same. I could also just do AsContent(), which may be cleaner but involve a virtual call, or add AsCharacterData(), just let me know. MozReview-Commit-ID: 79I9CuCmioF
dom/base/Selection.cpp
--- a/dom/base/Selection.cpp
+++ b/dom/base/Selection.cpp
@@ -3210,19 +3210,19 @@ Selection::ContainsNode(nsINode& aNode, 
 {
   nsresult rv;
   if (mRanges.Length() == 0) {
     return false;
   }
 
   // XXXbz this duplicates the GetNodeLength code in nsRange.cpp
   uint32_t nodeLength;
-  bool isData = aNode.IsCharacterData();
-  if (isData) {
-    nodeLength = aNode.AsText()->TextLength();
+  auto* nodeAsCharData = CharacterData::FromNode(aNode);
+  if (nodeAsCharData) {
+    nodeLength = nodeAsCharData->TextLength();
   } else {
     nodeLength = aNode.GetChildCount();
   }
 
   nsTArray<nsRange*> overlappingRanges;
   rv = GetRangesForIntervalArray(&aNode, 0, &aNode, nodeLength,
                                  false, &overlappingRanges);
   if (NS_FAILED(rv)) {
@@ -3233,17 +3233,17 @@ Selection::ContainsNode(nsINode& aNode, 
     return false; // no ranges overlap
 
   // if the caller said partial intersections are OK, we're done
   if (aAllowPartial) {
     return true;
   }
 
   // text nodes always count as inside
-  if (isData) {
+  if (nodeAsCharData) {
     return true;
   }
 
   // The caller wants to know if the node is entirely within the given range,
   // so we have to check all intersecting ranges.
   for (uint32_t i = 0; i < overlappingRanges.Length(); i++) {
     bool nodeStartsBeforeRange, nodeEndsAfterRange;
     if (NS_SUCCEEDED(nsRange::CompareNodeToRange(&aNode, overlappingRanges[i],