File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -169,6 +169,31 @@ data and its length in bytes. Note that the buffer MAY be uninitialized.
169
169
On success, the function should return ` Ok(()) ` and fully fill the input buffer;
170
170
otherwise, it should return an error value.
171
171
172
+ While wrapping functions which work with byte slices you should fully initialize
173
+ the buffer before passing it to the function:
174
+ ``` rust
175
+ use getrandom :: Error ;
176
+
177
+ fn my_entropy_source (buf : & mut [u8 ]) -> Result <(), getrandom :: Error > {
178
+ // ...
179
+ Ok (())
180
+ }
181
+
182
+ #[no_mangle]
183
+ unsafe extern " Rust" fn __getrandom_v03_custom (
184
+ dest : * mut u8 ,
185
+ len : usize ,
186
+ ) -> Result <(), Error > {
187
+ let buf = unsafe {
188
+ // fill the buffer with zeros
189
+ core :: ptr :: write_bytes (dest , 0 , len );
190
+ // create mutable byte slice
191
+ core :: slice :: from_raw_parts_mut (dest , len )
192
+ };
193
+ my_entropy_source (buf )
194
+ }
195
+ ```
196
+
172
197
If you are confident that ` getrandom ` is not used in your project, but
173
198
it gets pulled nevertheless by one of your dependencies, then you can
174
199
use the following custom backend, which always returns the "unsupported" error:
You can’t perform that action at this time.
0 commit comments