Skip to content

Commit 631e7b4

Browse files
committed
Rollup merge of rust-lang#33372 - birkenfeld:rustdoc-escape-code, r=cmr
rustdoc: HTML-escape Rust code (from constants) Especially in cases like the one in the test file, this can blow up the rendering big time if string constants in the code contain HTML. But also other constants can contain special chars (e.g. `&` as an operator in constant expressions).
2 parents 1ab0195 + 1bcf41e commit 631e7b4

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Diff for: src/librustdoc/html/format.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use rustc::hir;
2626
use clean;
2727
use core::DocAccessLevels;
2828
use html::item_type::ItemType;
29+
use html::escape::Escape;
2930
use html::render;
3031
use html::render::{cache, CURRENT_LOCATION_KEY};
3132

@@ -496,7 +497,7 @@ impl fmt::Display for clean::Type {
496497
primitive_link(f, clean::PrimitiveType::Array, "[")?;
497498
write!(f, "{}", t)?;
498499
primitive_link(f, clean::PrimitiveType::Array,
499-
&format!("; {}]", *s))
500+
&format!("; {}]", Escape(s)))
500501
}
501502
clean::Bottom => f.write_str("!"),
502503
clean::RawPointer(m, ref t) => {

Diff for: src/librustdoc/html/render.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1866,7 +1866,7 @@ impl<'a> fmt::Display for Initializer<'a> {
18661866
let Initializer(s) = *self;
18671867
if s.is_empty() { return Ok(()); }
18681868
write!(f, "<code> = </code>")?;
1869-
write!(f, "<code>{}</code>", s)
1869+
write!(f, "<code>{}</code>", Escape(s))
18701870
}
18711871
}
18721872

@@ -2106,7 +2106,7 @@ fn assoc_const(w: &mut fmt::Formatter,
21062106

21072107
write!(w, ": {}", ty)?;
21082108
if let Some(default) = default {
2109-
write!(w, " = {}", default)?;
2109+
write!(w, " = {}", Escape(default))?;
21102110
}
21112111
Ok(())
21122112
}

Diff for: src/test/rustdoc/escape-rust-expr.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test that we HTML-escape Rust expressions, where HTML special chars
12+
// can occur, and we know it's definitely not markup.
13+
14+
// @has escape_rust_expr/constant.CONST_S.html '//pre[@class="rust const"]' '"<script>"'
15+
pub const CONST_S: &'static str = "<script>";

0 commit comments

Comments
 (0)