Skip to content

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

Closed
@telotortium

Description

@telotortium
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.

Activity

alexcrichton

alexcrichton commented on Oct 31, 2013

@alexcrichton
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

huonw commented on Oct 31, 2013

@huonw
Member

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

telotortium commented on Oct 31, 2013

@telotortium
ContributorAuthor

I'll fix it in an hour or two.

added a commit that references this issue on Nov 1, 2013

auto merge of #10213 : telotortium/rust/rand-fill_bytes-stack-overflo…

a300314
added a commit that references this issue on Apr 3, 2025
4f58673
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

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @alexcrichton@huonw@telotortium

      Issue actions

        std::rand::reseeding: infinite recursion in fill_bytes() · Issue #10202 · rust-lang/rust