Skip to content

There are memory leaks and null pointer dereference bugs in the code. #663

Description

@ErisianArchitect

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions