Skip to content

Commit 942bed7

Browse files
committed
rustdoc: make calls of markdown::render safer
Previously, external code might call `markdown::render()` without having called `markdown::reset_headers()`, meaning the TLS key `used_header_map` was unset. Now `markdown::render()` ensures that `used_header_map` is set by calling `reset_headers` if necessary. Fix rust-lang#17736
1 parent dfbe9eb commit 942bed7

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/librustdoc/html/markdown.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
268268
text.with_c_str(|p| unsafe { hoedown_buffer_puts(ob, p) });
269269
}
270270

271+
if used_header_map.get().is_none() {
272+
reset_headers();
273+
}
274+
271275
unsafe {
272276
let ob = hoedown_buffer_new(DEF_OUNIT);
273277
let renderer = hoedown_html_renderer_new(0, 0);
@@ -446,7 +450,7 @@ impl<'a> fmt::Show for MarkdownWithToc<'a> {
446450

447451
#[cfg(test)]
448452
mod tests {
449-
use super::LangString;
453+
use super::{LangString, Markdown};
450454

451455
#[test]
452456
fn test_lang_string_parse() {
@@ -474,4 +478,10 @@ mod tests {
474478
t("{.example .rust}", false,false,false,false,false);
475479
t("{.test_harness .rust}", false,false,false,false,true);
476480
}
481+
482+
#[test]
483+
fn issue_17736() {
484+
let markdown = "# title";
485+
format!("{}", Markdown(markdown.as_slice()));
486+
}
477487
}

0 commit comments

Comments
 (0)