Skip to content
Merged
Show file tree
Hide file tree
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
18 changes: 14 additions & 4 deletions src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,19 @@ pub fn report_error<'tcx, 'mir>(

e.print_backtrace();
let msg = e.to_string();
report_msg(ecx, &format!("{}: {}", title, msg), msg, helps, true)
report_msg(ecx, &format!("{}: {}", title, msg), msg, helps, true);

// Extra output to help debug specific issues.
if let UndefinedBehavior(UndefinedBehaviorInfo::InvalidUndefBytes(Some(ptr))) = e.kind {
eprintln!(
"Uninitialized read occurred at offset 0x{:x} into this allocation:",
ptr.offset.bytes(),
);
ecx.memory.dump_alloc(ptr.alloc_id);
eprintln!();
}

None
}

/// Report an error or note (depending on the `error` argument) at the current frame's current statement.
Expand All @@ -126,7 +138,7 @@ fn report_msg<'tcx, 'mir>(
span_msg: String,
mut helps: Vec<String>,
error: bool,
) -> Option<i64> {
) {
let span = if let Some(frame) = ecx.machine.stack.last() {
frame.current_source_info().unwrap().span
} else {
Expand Down Expand Up @@ -167,8 +179,6 @@ fn report_msg<'tcx, 'mir>(
trace!(" local {}: {:?}", i, local.value);
}
}
// Let the reported error determine the return code.
return None;
}

thread_local! {
Expand Down
20 changes: 20 additions & 0 deletions tests/compile-fail/undefined_buffer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// error-pattern: reading uninitialized memory

use std::alloc::{alloc, dealloc, Layout};
use std::slice::from_raw_parts;

fn main() {
let layout = Layout::from_size_align(32, 8).unwrap();
unsafe {
let ptr = alloc(layout);
*ptr = 0x41;
*ptr.add(1) = 0x42;
*ptr.add(2) = 0x43;
*ptr.add(3) = 0x44;
*ptr.add(16) = 0x00;
let slice1 = from_raw_parts(ptr, 16);
let slice2 = from_raw_parts(ptr.add(16), 16);
drop(slice1.cmp(slice2));
dealloc(ptr, layout);
}
}