Skip to content

Commit d7d7725

Browse files
committed
rustdoc: Fully escape link section and export name
Escape "special characters" (e.g., double quotes `"` and line breaks `\n`). Escape HTML. Lastly, add regression tests and clean up existing tests.
1 parent 88e7977 commit d7d7725

File tree

7 files changed

+38
-46
lines changed

7 files changed

+38
-46
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2935,11 +2935,11 @@ fn render_attributes_in_code(
29352935
let hir::Attribute::Parsed(kind) = attr else { continue };
29362936
let attr = match kind {
29372937
AttributeKind::LinkSection { name, .. } => {
2938-
Cow::Owned(format!("#[unsafe(link_section = \"{name}\")]"))
2938+
Cow::Owned(format!("#[unsafe(link_section = {})]", Escape(&format!("{name:?}"))))
29392939
}
29402940
AttributeKind::NoMangle(..) => Cow::Borrowed("#[unsafe(no_mangle)]"),
29412941
AttributeKind::ExportName { name, .. } => {
2942-
Cow::Owned(format!("#[unsafe(export_name = \"{name}\")]"))
2942+
Cow::Owned(format!("#[unsafe(export_name = {})]", Escape(&format!("{name:?}"))))
29432943
}
29442944
AttributeKind::NonExhaustive(..) => Cow::Borrowed("#[non_exhaustive]"),
29452945
_ => continue,

tests/rustdoc/attribute-rendering.rs

Lines changed: 0 additions & 8 deletions
This file was deleted.

tests/rustdoc/attributes.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ pub extern "C" fn f() {}
99
#[unsafe(export_name = "bar")]
1010
pub extern "C" fn g() {}
1111

12+
//@ has foo/fn.escape_special.html '//*[@class="code-attribute"]' \
13+
// '#[unsafe(export_name = "\n\"\n")]'
14+
#[unsafe(export_name = "\n\"
15+
")]
16+
pub extern "C" fn escape_special() {}
17+
18+
// issue: <https://github.com/rust-lang/rust/issues/142835>
19+
//@ has foo/fn.escape_html.html '//*[@class="code-attribute"]' \
20+
// '#[unsafe(export_name = "<script>alert()</script>")]'
21+
#[unsafe(export_name = "<script>alert()</script>")]
22+
pub extern "C" fn escape_html() {}
23+
1224
//@ has foo/fn.example.html '//*[@class="code-attribute"]' '#[unsafe(link_section = ".text")]'
1325
#[unsafe(link_section = ".text")]
1426
pub extern "C" fn example() {}
Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
1+
// Ensure that we render attributes on inlined cross-crate re-exported items.
2+
// issue: <https://github.com/rust-lang/rust/issues/144004>
3+
14
//@ aux-crate:attributes=attributes.rs
25
//@ edition:2021
36
#![crate_name = "user"]
47

5-
//@ has 'user/struct.NonExhaustive.html'
6-
//@ has - '//*[@class="rust item-decl"]//*[@class="code-attribute"]' '#[non_exhaustive]'
8+
//@ has 'user/fn.no_mangle.html' '//pre[@class="rust item-decl"]' '#[unsafe(no_mangle)]'
9+
pub use attributes::no_mangle;
10+
11+
//@ has 'user/fn.link_section.html' '//pre[@class="rust item-decl"]' \
12+
// '#[unsafe(link_section = ".here")]'
13+
pub use attributes::link_section;
14+
15+
//@ has 'user/fn.export_name.html' '//pre[@class="rust item-decl"]' \
16+
// '#[unsafe(export_name = "exonym")]'
17+
pub use attributes::export_name;
18+
19+
//@ has 'user/struct.NonExhaustive.html' '//pre[@class="rust item-decl"]' '#[non_exhaustive]'
720
pub use attributes::NonExhaustive;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1+
#[unsafe(no_mangle)]
2+
pub fn no_mangle() {}
3+
4+
#[unsafe(link_section = ".here")]
5+
pub fn link_section() {}
6+
7+
#[unsafe(export_name = "exonym")]
8+
pub fn export_name() {}
9+
110
#[non_exhaustive]
211
pub struct NonExhaustive;

tests/rustdoc/reexport/auxiliary/reexports-attrs.rs

Lines changed: 0 additions & 14 deletions
This file was deleted.

tests/rustdoc/reexport/reexport-attrs.rs

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)