Skip to content

Panic on merge empty vec of streams #214

@collin-nzxt

Description

@collin-nzxt

I've encountered a panic when merging an empty vec of streams.

Issue is similar to #156

Example triggering code

#!/usr/bin/env -S cargo +nightly -Zscript
---
[package]
name = "panic-div-zero"
version = "0.1.0"
edition = "2024"

[dependencies]
futures = "0.3.31"
futures-concurrency = "7.6.3"
tokio = { version = "1.48.0", features = ["full"] }
---

use futures_concurrency::prelude::*;
use futures::stream::StreamExt;

#[tokio::main]
async fn main() {
    // Create an empty vec of streams
    let data: Vec<futures::stream::Iter<std::vec::IntoIter<i32>>> = vec![];

    // This will panic with divide-by-zero in Indexer::iter()
    // when wrapping_rem(0) is called
    let mut merged = data.merge();

    println!("Attempting to poll empty merge...");
    let result = merged.next().await;
    println!("Result: {result:?}");
}

it seems the bug is in the wrapping_div here:

impl Indexer {
    pub(crate) fn new(max: usize) -> Self {
        Self { offset: 0, max }
    }

    /// Generate a range between `0..max`, incrementing the starting point
    /// for the next iteration.
    pub(crate) fn iter(&mut self) -> IndexIter {
        // Increment the starting point for next time.
        let offset = self.offset;
        self.offset = (self.offset + 1).wrapping_rem(self.max);

        IndexIter {
            iter: (0..self.max),
            offset,
        }
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions