housleyjk / ws-rs

Lightweight, event-driven WebSockets for Rust.
MIT License
1.47k stars 222 forks source link

Fix panic when using permessage-deflate #335

Closed UE2020 closed 2 years ago

UE2020 commented 2 years ago

When using the permessage-deflate feature under rustc v1.59.0, I was getting a panic because function pointers cannot be zeroed: "attempted to zero-initialize type libz_sys::z_stream, which is invalid"

This PR fixes the issue by using uninitialized memory, which should be slightly more performant anyways.

TheDan64 commented 2 years ago

Hi @UE2020 @housleyjk - I'm not confident that this fix is safe - the problem being that stream.as_mut() gives you a &mut reference pointing to uninit data - which goes against Rust's safety guarantees and is likely UB.

I believe this is a much more appropriate fix: https://github.com/TheDan64/ws-rs/commit/dbe884d6a5093c35c4c991b175423e49e015dbf9

Specifically because it provides a raw pointer into the MaybeUninit rather than a reference (whether we use MaybeUninit::zeroed() or MaybeUninit::uninit() is an implementation detail so long as all fields are being written to)