After reading the docs for the add() function (pointer - Rust) I thought that the above should print two different values, and not the same value for both pointers. My initial thought is that I must misunderstand how rust is doing pointer arithmetic.
Can anyone say why the same value is being displayed for both PTR and END?
I have narrowed the problem down to an issue with c_void pointers. The original code uses the WinPcap external library. (See https://github.com/libpnet/libpnet/blob/master/pnet_datalink/src/winpcap.rs) I have distilled the issue down to a simple snippet that demonstrates the behavior where add() does not seem to work. I have tried using simple types like pointers into a constant string, which work fine, so I am assuming there is some sort of type wizardry going on with external C pointers that I do not understand.
// from elsewhere: pub type PVOID = *mut ctypes::c_void;
unsafe {
let mut b = Vec::new();
b.resize(1000, 0u8);
let bp: *mut ctypes::c_void = b.as_mut_ptr() as winpcap::PVOID;
println!("PRE: {:?} PTR: {:?} END: {:?}", bp.sub(0x2), bp, bp.add(0x2));
}
(All three values -- PRE, PTR, and END -- display the same value.)
Success -- thank you! Enabling the std feature of the winapi dependency worked.
For reference, this change in Cargo.toml makes the pointers work as expected:
[target.'cfg(windows)'.dependencies]
# winapi = "0.3.8" # This will not work because the size of c_void defaults to 0.
winapi = { version = "0.3.8", features = [ "std" ] }