CyberChef icon indicating copy to clipboard operation
CyberChef copied to clipboard

Bug report: Median operation returns incorrect result for unsorted odd-length inputs

Open williballenthin opened this issue 8 months ago • 4 comments

Describe the bug The Median operation only sorts the input array when its length is even. For odd-length inputs it returns the middle element of the unsorted array, which is mathematically incorrect.

src/core/lib/Arithmetic.mjs, median() function, lines 84-93

export function median(data) {
    if ((data.length % 2) === 0 && data.length > 0) {
        data.sort(function(a, b) {
            return a.minus(b);
        });
        const first = data[Math.floor(data.length / 2)];
        const second = data[Math.floor(data.length / 2) - 1];
        return mean([first, second]);
    } else {
        return data[Math.floor(data.length / 2)];
    }
}

For odd-length arrays the function skips sorting entirely and returns data[Math.floor(data.length / 2)]. The median of any numeric set requires sorting first.

To Reproduce add the Median operation with input 10,1,2 (comma delimiter). Expected output is 2 (the median of [1, 2, 10]); actual output is 1 (the middle element of the unsorted array).

Additional context Suggested fix:

export function median(data) {
    if (data.length === 0) {
        return undefined;
    }

    data.sort(function(a, b) {
        return a.minus(b);
    });

    if ((data.length % 2) === 0) {
        const first = data[Math.floor(data.length / 2)];
        const second = data[Math.floor(data.length / 2) - 1];
        return mean([first, second]);
    }

    return data[Math.floor(data.length / 2)];
}

This sorts unconditionally, handles empty input by returning undefined, and preserves existing even-length behavior.

williballenthin avatar Mar 13 '26 08:03 williballenthin

URL for reproducing (as described manually above)

https://gchq.github.io/CyberChef/#recipe=Median('Comma')&input=MTAsIDEsIDI

GCHQDeveloper581 avatar Mar 13 '26 09:03 GCHQDeveloper581

FYI I discovered this set of bugs while building (or having an agent build) test cases for https://github.com/HexRaysSA/ida-cyberchef.

williballenthin avatar Mar 13 '26 10:03 williballenthin

FYI I discovered this set of bugs while building (or having an agent build) test cases for https://github.com/HexRaysSA/ida-cyberchef.

Just be aware there are currently problems (hopefully soon to be resolved) with the Node API where the Operation uses an async run function (see https://github.com/gchq/CyberChef/issues/2128 , https://github.com/gchq/CyberChef/pull/2222)

Also - an alternative to calling the Node API directly would be to run an instance of CyberChef-server and send web requests to that.

GCHQDeveloper581 avatar Mar 13 '26 12:03 GCHQDeveloper581

Fixed in PR #2457. The sort was inside the if (even) branch — odd-length arrays skipped sorting entirely. Fix is to sort unconditionally before branching on length parity.

vigneshrajan94 avatar May 27 '26 11:05 vigneshrajan94