]> git.lizzy.rs Git - rust.git/commitdiff
rustdoc: make calls of markdown::render safer
authorTom Jakubowski <tom@crystae.net>
Sun, 5 Oct 2014 13:00:50 +0000 (06:00 -0700)
committerTom Jakubowski <tom@crystae.net>
Mon, 6 Oct 2014 08:51:58 +0000 (01:51 -0700)
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 #17736

src/librustdoc/html/markdown.rs

index 86f56660d3a64ed3bbaa6e59b972959ce031f395..faf361f029059819665f7c5cc10b114ae7ab5f73 100644 (file)
@@ -268,6 +268,10 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
         text.with_c_str(|p| unsafe { hoedown_buffer_puts(ob, p) });
     }
 
+    if used_header_map.get().is_none() {
+        reset_headers();
+    }
+
     unsafe {
         let ob = hoedown_buffer_new(DEF_OUNIT);
         let renderer = hoedown_html_renderer_new(0, 0);
@@ -446,7 +450,7 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
 
 #[cfg(test)]
 mod tests {
-    use super::LangString;
+    use super::{LangString, Markdown};
 
     #[test]
     fn test_lang_string_parse() {
@@ -474,4 +478,10 @@ fn t(s: &str,
         t("{.example .rust}", false,false,false,false,false);
         t("{.test_harness .rust}", false,false,false,false,true);
     }
+
+    #[test]
+    fn issue_17736() {
+        let markdown = "# title";
+        format!("{}", Markdown(markdown.as_slice()));
+    }
 }