Skip to content

Commit 360b580

Browse files
committed
rustdoc: HTML-escape link section and export name
Lastly, add regression tests and clean up existing tests.
1 parent 8300b14 commit 360b580

File tree

7 files changed

+30
-46
lines changed

7 files changed

+30
-46
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2936,13 +2936,13 @@ fn render_attributes_in_code(
29362936
prefix,
29372937
match attr {
29382938
hir::Attribute::Parsed(AttributeKind::LinkSection { name, .. }) => {
2939-
Cow::Owned(format!("#[unsafe(link_section = \"{name}\")]"))
2939+
Cow::Owned(format!("#[unsafe(link_section = \"{}\")]", Escape(name.as_str())))
29402940
}
29412941
hir::Attribute::Parsed(AttributeKind::NoMangle(..)) => {
29422942
Cow::Borrowed("#[unsafe(no_mangle)]")
29432943
}
29442944
hir::Attribute::Parsed(AttributeKind::ExportName { name, .. }) => {
2945-
Cow::Owned(format!("#[unsafe(export_name = \"{name}\")]"))
2945+
Cow::Owned(format!("#[unsafe(export_name = \"{}\")]", Escape(name.as_str())))
29462946
}
29472947
hir::Attribute::Parsed(AttributeKind::NonExhaustive(..)) => {
29482948
Cow::Borrowed("#[non_exhaustive]")

tests/rustdoc/attribute-rendering.rs

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

tests/rustdoc/attributes.rs

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

12+
//@ has foo/fn.escape.html '//*[@class="code-attribute"]' \
13+
// '#[unsafe(export_name = "<script>alert()</script>")]'
14+
#[unsafe(export_name = "<script>alert()</script>")]
15+
pub extern "C" fn escape() {}
16+
1217
//@ has foo/fn.example.html '//*[@class="code-attribute"]' '#[unsafe(link_section = ".text")]'
1318
#[unsafe(link_section = ".text")]
1419
pub extern "C" fn example() {}
Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,19 @@
1+
// Ensure that we render attributes on inlined cross-crate re-exported items.
2+
13
//@ aux-crate:attributes=attributes.rs
24
//@ edition:2021
35
#![crate_name = "user"]
46

5-
//@ has 'user/struct.NonExhaustive.html'
6-
//@ has - '//*[@class="rust item-decl"]//*[@class="code-attribute"]' '#[non_exhaustive]'
7+
//@ has 'user/fn.no_mangle.html' '//pre[@class="rust item-decl"]' '#[unsafe(no_mangle)]'
8+
pub use attributes::no_mangle;
9+
10+
//@ has 'user/fn.link_section.html' '//pre[@class="rust item-decl"]' \
11+
// '#[unsafe(link_section = ".here")]'
12+
pub use attributes::link_section;
13+
14+
//@ has 'user/fn.export_name.html' '//pre[@class="rust item-decl"]' \
15+
// '#[unsafe(export_name = "exonym")]'
16+
pub use attributes::export_name;
17+
18+
//@ has 'user/struct.NonExhaustive.html' '//pre[@class="rust item-decl"]' '#[non_exhaustive]'
719
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)