Skip to content

Rustdoc: Don't cause UB when rendering code spans #27858

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ type headerfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
libc::c_int, *mut libc::c_void);

type codespanfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer,
*mut libc::c_void);
*mut libc::c_void) -> libc::c_int;

type linkfn = extern "C" fn (*mut hoedown_buffer, *const hoedown_buffer,
*const hoedown_buffer, *const hoedown_buffer,
Expand Down Expand Up @@ -317,7 +317,11 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {

reset_headers();

extern fn codespan(ob: *mut hoedown_buffer, text: *const hoedown_buffer, _: *mut libc::c_void) {
extern fn codespan(
ob: *mut hoedown_buffer,
text: *const hoedown_buffer,
_: *mut libc::c_void,
) -> libc::c_int {
let content = if text.is_null() {
"".to_string()
} else {
Expand All @@ -329,6 +333,8 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
let content = format!("<code>{}</code>", Escape(&content));
let element = CString::new(content).unwrap();
unsafe { hoedown_buffer_puts(ob, element.as_ptr()); }
// Return anything except 0, which would mean "also print the code span verbatim".
1
}

unsafe {
Expand Down