Skip to content

std::rand::reseeding: infinite recursion in fill_bytes() #10202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
telotortium opened this issue Oct 31, 2013 · 3 comments
Closed

std::rand::reseeding: infinite recursion in fill_bytes() #10202

telotortium opened this issue Oct 31, 2013 · 3 comments

Comments

@telotortium
Copy link
Contributor

The example code for std::rand::Rng::fill_bytes fails due to a stack overflow:

use std::rand::{task_rng, Rng};

fn main() {
   let mut v = [0u8, .. 13579];
   task_rng().fill_bytes(v);
   println!("{:?}", v);
}

This is due to infinite recursion in the ReseedingRng trait (src/libstd/rand/reseeding.rs:72):

    fn fill_bytes(&mut self, dest: &mut [u8]) {
        self.reseed_if_necessary();
        self.bytes_generated += dest.len();
        self.fill_bytes(dest)
    }
}

self.fill_bytes(dest) should be self.rng.fill_bytes(dest) -- changing the line to that fixes the issue.

@alexcrichton
Copy link
Member

Oh that's not good! cc @huonw so he's aware, but if you would like to fix it (looks like the fix you have is legitimate), feel free to make the change, add some tests, and open a pull request!

@huonw
Copy link
Member

huonw commented Oct 31, 2013

Oops! Second time I've done this with the RNG stuff! 😦

@telotortium tell me if you're not going to fix it, so I can whip up a patch.

@telotortium
Copy link
Contributor Author

I'll fix it in an hour or two.

bors added a commit that referenced this issue Nov 1, 2013
…w, r=huonw

Fix the implementation of `std::rand::Rng::fill_bytes()` for
`std::rand::reseeding::ReseedingRng` to call the `fill_bytes()` method
of the underlying RNG rather than itself, which causes infinite
recursion.

Fixes #10202.
@bors bors closed this as completed in 96589e7 Nov 1, 2013
flip1995 pushed a commit to flip1995/rust that referenced this issue Apr 3, 2025
Closes rust-lang#10202.

This adds a new lint that checks for uses of the `.chars().enumerate()`
position in a context where a byte index is required and suggests
changing it to use `.char_indices()` instead.

I'm planning to extend this lint to also detect uses of the position in
iterator chains, e.g. `s.chars().enumerate().for_each(|(i, _)|
s.split_at(i));`, but that's for another time

----------------

changelog: new lint: `chars_enumerate_for_byte_indices`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants