Describe the Bug
Earlier, someone posted an issue (#661) about a segfault that they were having. Upon further investigation, it was discovered that it was a null pointer dereference. I submitted a pull request (#662 ) to fix the error with verify, but then I went reading some more of the code and I noticed a pretty glaring flaw (or multiple).
/// Prints the content of the `Module` to a file.
pub fn print_to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), LLVMString> {
let path_str = path
.as_ref()
.to_str()
.expect("Did not find a valid Unicode path string");
let path = to_c_str(path_str);
let mut err_string = MaybeUninit::uninit();
let return_code = unsafe {
LLVMPrintModuleToFile(
self.module.get(),
path.as_ptr() as *const ::libc::c_char,
err_string.as_mut_ptr(),
)
};
if return_code == 1 {
unsafe {
return Err(LLVMString::new(err_string.assume_init()));
}
}
Ok(())
}
There are 2 problems with this code. The first problem is that LLVM might not even touch the err_string pointer, so it may be initialized to garbage, which could cause a segfault, or otherwise it might touch memory that it's not supposed to, producing garbage.
The second problem is that if return_code == 1 assumes that err_string has been properly initialized. There's no null pointer check.
I'm going to do some more digging to see if I can find more locations that have dereference errors or memory leaks, because I have a feeling that there are other places where this same mistake was made.
Describe the Bug
Earlier, someone posted an issue (#661) about a segfault that they were having. Upon further investigation, it was discovered that it was a null pointer dereference. I submitted a pull request (#662 ) to fix the error with
verify, but then I went reading some more of the code and I noticed a pretty glaring flaw (or multiple).There are 2 problems with this code. The first problem is that LLVM might not even touch the
err_stringpointer, so it may be initialized to garbage, which could cause a segfault, or otherwise it might touch memory that it's not supposed to, producing garbage.The second problem is that
if return_code == 1assumes thaterr_stringhas been properly initialized. There's no null pointer check.I'm going to do some more digging to see if I can find more locations that have dereference errors or memory leaks, because I have a feeling that there are other places where this same mistake was made.