From 26dd77f4f3ec4adfa22cb6dfd25fafc5b55b7466 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 5 Aug 2017 23:54:48 +0200 Subject: [PATCH 1/3] Fix hoedown error in rustdoc --- src/librustdoc/html/markdown.rs | 15 +++++++-------- src/test/rustdoc/nul-error.rs | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 src/test/rustdoc/nul-error.rs diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 03da451fd9a0a..cd58ff3109f53 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -32,7 +32,6 @@ use std::ascii::AsciiExt; use std::cell::RefCell; use std::collections::{HashMap, VecDeque}; use std::default::Default; -use std::ffi::CString; use std::fmt::{self, Write}; use std::str; use syntax::feature_gate::UnstableFeatures; @@ -531,6 +530,7 @@ extern { fn hoedown_buffer_new(unit: libc::size_t) -> *mut hoedown_buffer; fn hoedown_buffer_puts(b: *mut hoedown_buffer, c: *const libc::c_char); fn hoedown_buffer_free(b: *mut hoedown_buffer); + fn hoedown_buffer_put(b: *mut hoedown_buffer, c: *const libc::c_char, len: libc::size_t); } impl hoedown_buffer { @@ -620,8 +620,7 @@ pub fn render(w: &mut fmt::Formatter, Some("rust-example-rendered"), None, playground_button.as_ref().map(String::as_str))); - let output = CString::new(s).unwrap(); - hoedown_buffer_puts(ob, output.as_ptr()); + hoedown_buffer_put(ob, s.as_ptr() as *const libc::c_char, s.len()); }) } } @@ -681,8 +680,7 @@ pub fn render(w: &mut fmt::Formatter, {sec}{}", s, lvl = level, id = id, sec = sec); - let text = CString::new(text).unwrap(); - unsafe { hoedown_buffer_puts(ob, text.as_ptr()) } + unsafe { hoedown_buffer_put(ob, text.as_ptr() as *const libc::c_char, text.len()); } } extern fn codespan( @@ -699,9 +697,10 @@ pub fn render(w: &mut fmt::Formatter, collapse_whitespace(s) }; - let content = format!("{}", Escape(&content)); - let element = CString::new(content).unwrap(); - unsafe { hoedown_buffer_puts(ob, element.as_ptr()); } + let content = format!("{}", Escape(&content)).replace("\0", "\\0"); + unsafe { + hoedown_buffer_put(ob, content.as_ptr() as *const libc::c_char, content.len()); + } // Return anything except 0, which would mean "also print the code span verbatim". 1 } diff --git a/src/test/rustdoc/nul-error.rs b/src/test/rustdoc/nul-error.rs new file mode 100644 index 0000000000000..f20d19d0e59cc --- /dev/null +++ b/src/test/rustdoc/nul-error.rs @@ -0,0 +1,18 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// build-aux-docs +// ignore-cross-compile + +#![crate_name = "foo"] + +// @has foo/fn.foo.html '//code' '0' +#[doc = "Attempted to pass a string containing `\0`"] +pub fn foo() {} From d0916c57ca480cb1a7672c02cda00ec008a50c3c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 7 Aug 2017 20:17:45 +0200 Subject: [PATCH 2/3] Remove \0 printing --- src/librustdoc/html/markdown.rs | 10 +++++----- src/test/rustdoc/nul-error.rs | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index cd58ff3109f53..35d0f0a116d07 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -530,7 +530,7 @@ extern { fn hoedown_buffer_new(unit: libc::size_t) -> *mut hoedown_buffer; fn hoedown_buffer_puts(b: *mut hoedown_buffer, c: *const libc::c_char); fn hoedown_buffer_free(b: *mut hoedown_buffer); - fn hoedown_buffer_put(b: *mut hoedown_buffer, c: *const libc::c_char, len: libc::size_t); + fn hoedown_buffer_put(b: *mut hoedown_buffer, c: *const u8, len: libc::size_t); } impl hoedown_buffer { @@ -620,7 +620,7 @@ pub fn render(w: &mut fmt::Formatter, Some("rust-example-rendered"), None, playground_button.as_ref().map(String::as_str))); - hoedown_buffer_put(ob, s.as_ptr() as *const libc::c_char, s.len()); + hoedown_buffer_put(ob, s.as_ptr(), s.len()); }) } } @@ -680,7 +680,7 @@ pub fn render(w: &mut fmt::Formatter, {sec}{}", s, lvl = level, id = id, sec = sec); - unsafe { hoedown_buffer_put(ob, text.as_ptr() as *const libc::c_char, text.len()); } + unsafe { hoedown_buffer_put(ob, text.as_ptr(), text.len()); } } extern fn codespan( @@ -697,9 +697,9 @@ pub fn render(w: &mut fmt::Formatter, collapse_whitespace(s) }; - let content = format!("{}", Escape(&content)).replace("\0", "\\0"); + let content = format!("{}", Escape(&content)); unsafe { - hoedown_buffer_put(ob, content.as_ptr() as *const libc::c_char, content.len()); + hoedown_buffer_put(ob, content.as_ptr(), content.len()); } // Return anything except 0, which would mean "also print the code span verbatim". 1 diff --git a/src/test/rustdoc/nul-error.rs b/src/test/rustdoc/nul-error.rs index f20d19d0e59cc..2c9d07f190d26 100644 --- a/src/test/rustdoc/nul-error.rs +++ b/src/test/rustdoc/nul-error.rs @@ -13,6 +13,6 @@ #![crate_name = "foo"] -// @has foo/fn.foo.html '//code' '0' +// @has foo/fn.foo.html '//code' '' #[doc = "Attempted to pass a string containing `\0`"] pub fn foo() {} From ec0ca3a7c64c08dab44eb30f11b2b0870c4583ea Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 8 Aug 2017 21:25:39 +0200 Subject: [PATCH 3/3] Remove all usage of hoedown_buffer_puts --- src/librustdoc/html/markdown.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 35d0f0a116d07..735c9d8af7a8a 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -528,7 +528,6 @@ extern { fn hoedown_document_free(md: *mut hoedown_document); fn hoedown_buffer_new(unit: libc::size_t) -> *mut hoedown_buffer; - fn hoedown_buffer_puts(b: *mut hoedown_buffer, c: *const libc::c_char); fn hoedown_buffer_free(b: *mut hoedown_buffer); fn hoedown_buffer_put(b: *mut hoedown_buffer, c: *const u8, len: libc::size_t); } @@ -629,7 +628,7 @@ pub fn render(w: &mut fmt::Formatter, level: libc::c_int, data: *const hoedown_renderer_data, _: libc::size_t) { // hoedown does this, we may as well too - unsafe { hoedown_buffer_puts(ob, "\n\0".as_ptr() as *const _); } + unsafe { hoedown_buffer_put(ob, "\n".as_ptr(), 1); } // Extract the text provided let s = if text.is_null() {