Bug 1752498 - XML error shows garbage for location. r=bholley

Differential Revision: https://phabricator.services.mozilla.com/D137262
This commit is contained in:
Peter Van der Beken 2022-02-02 09:28:33 +00:00
parent a9dda47334
commit dd05949994
3 changed files with 48 additions and 6 deletions

View File

@ -4,6 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsExpatDriver.h"
#include "mozilla/fallible.h"
#include "nsCOMPtr.h"
#include "nsParserCIID.h"
#include "CParserContext.h"
@ -15,6 +16,7 @@
#include "nsIUnicharInputStream.h"
#include "nsIProtocolHandler.h"
#include "nsNetUtil.h"
#include "nsString.h"
#include "nsTextFormatter.h"
#include "nsDirectoryServiceDefs.h"
#include "nsCRT.h"
@ -46,6 +48,7 @@
using mozilla::fallible;
using mozilla::LogLevel;
using mozilla::MakeStringSpan;
using mozilla::Unused;
using mozilla::dom::Document;
// We only pass chunks of length sMaxChunkLength to Expat in the RLBOX sandbox.
@ -1041,12 +1044,18 @@ nsresult nsExpatDriver::HandleError() {
1;
uint32_t lineNumber = RLBOX_EXPAT_SAFE_MCALL(MOZ_XML_GetCurrentLineNumber,
safe_unverified<XML_Size>);
const XML_Char* expatBase =
RLBOX_EXPAT_MCALL(MOZ_XML_GetBase)
.copy_and_verify_address(unverified_xml_string);
nsAutoString uri;
nsCOMPtr<nsIURI> baseURI;
if (expatBase && (baseURI = GetBaseURI(expatBase))) {
// Let's ignore if this fails, we're already reporting a parse error.
Unused << CopyUTF8toUTF16(baseURI->GetSpecOrDefault(), uri, fallible);
}
nsAutoString errorText;
const auto* aBase = RLBOX_EXPAT_MCALL(MOZ_XML_GetBase)
.copy_and_verify_address(unverified_xml_string);
CreateErrorText(description.get(), aBase, lineNumber, colNumber, errorText,
spoofEnglish);
CreateErrorText(description.get(), uri.get(), lineNumber, colNumber,
errorText, spoofEnglish);
nsAutoString sourceText(mLastLine);
AppendErrorPointer(colNumber, mLastLine.get(), sourceText);

View File

@ -140,7 +140,8 @@ skip-if = (xorigin && debug)
support-files =
file_defer_bug1104732.js
file_async_bug1104732.sjs
[test_bug1752498.html]
skip-if = true # Bug 1751796
[test_compatmode.html]
[test_html5_tree_construction.html]
[test_html5_tree_construction_part2.html]

View File

@ -0,0 +1,32 @@
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title><!-- TODO: insert title here --></title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
<script>
add_task(async function test() {
let string = "<p>Not a <<b>well-formed</b> xml string</p>";
// Line and column are 1-based.
let line = (string.match(/\r\n|\r|\n/g)?.length || 0) + 1;
let column = string.search("<b>") + 1;
let expectedError = `XML Parsing Error: not well-formed
Location: ${location.href}
Line Number ${line}, Column ${column}:${string}
${"^".padStart(column, "-")}`;
let error = new window.DOMParser()
.parseFromString(string, "text/xml")
.getElementsByTagName("parsererror")[0].textContent;
is(error, expectedError, "Check that parsererror contains the right data.");
});
</script>
</head>
<body>
<p id="display"></p>
<div id="content" style="display: none"></div>
<pre id="test"></pre>
</body>
</html>