Skip to content
This repository has been archived by the owner on Jul 3, 2023. It is now read-only.

Possible heap overflow: Cancelled orders are not removed #20

Merged
merged 1 commit into from
Jul 3, 2023

Conversation

LeonardGiglhuber
Copy link
Contributor

Issue:
The following code generates a heap overflow:

import Channel from "@nodeguy/channel";

async function heapTest() {
    const closedDefault = new Channel<{}>();
    closedDefault.close();
    const randomOtherChannel = new Channel<{}>();

    while (true) {
        switch (await Channel.select([randomOtherChannel.push({}), closedDefault.shift()])) {
            case randomOtherChannel:
                // Will never happen
                throw new Error();
            default:
                // Will always happen
                break;
        }
    }
}

heapTest();

Why:
Every iteration of the while loop appends a new order object to the pushes array of randomOtherChannel. The order immediately get’s cancelled but is never removed from the array. It should normally be skipped inside matchPushesAndShifts() and then sliced off inside processOrders(). This never happens because the while loop condition inside matchPushesAndShifts() is never met (the shifts array of randomOtherChannel is always empty).

Solution:
Filter out all cancelled orders from pushes and shifts at the end of every processOrders() call.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants