CyberChef icon indicating copy to clipboard operation
CyberChef copied to clipboard

Fix median sort, set-op duplicates, and Unescape Unicode range (#2239, #2241, #2242)

Open vigneshrajan94 opened this issue 6 months ago • 2 comments

Summary

Fixes three independent bugs in a single PR.

#2239 — Median returns wrong result for unsorted odd-length inputs

src/core/lib/Arithmetic.mjs — median()

The .sort() call was inside if (even), so odd-length arrays were never sorted before picking the middle element. Median([10, 1, 2]) returned 1 instead of 2.

Fix: sort unconditionally before branching on even/odd length.


#2241 — Set Difference / Set Intersection preserve duplicates from first input

src/core/operations/SetDifference.mjs, src/core/operations/SetIntersection.mjs

Both operations used a plain .filter() on array a. If a contained duplicate entries that passed the filter, all copies appeared in the output, violating set semantics. SetDifference(["x","x","y"], ["y"]) returned "x,x".

Fix: deduplicate a via [...new Set(a)] before filtering.


#2242 — Unescape Unicode Characters rejects code points > 4 hex digits under U+

src/core/operations/UnescapeUnicodeCharacters.mjs — run()

The regex quantifier was hardcoded to {4} for all prefix types. The U+ notation supports 4–6 hex digits (e.g. U+1F600 for 😀, U+000041 zero-padded). \u and %u remain at {4} per their respective specs.

Fix: use {4,6} when the selected prefix is U+, {4} otherwise.


Test plan

  • [ ] npm test — all 2178 tests pass (243 Node API + 1935 operations), zero failures
  • [ ] Median with input 10,1,2 (Comma delimiter) → 2
  • [ ] Set Difference with sample 1 x,x,y, sample 2 y → x
  • [ ] Set Intersection with sample 1 y,y,z, sample 2 y → y
  • [ ] Unescape Unicode Characters (prefix U+), input U+1F600 → 😀
  • [ ] Unescape Unicode Characters (prefix U+), input U+000041 → A
  • [ ] Unescape Unicode Characters (prefix \u), input A → A (4-digit still works)

vigneshrajan94 avatar May 27 '26 11:05 vigneshrajan94

@vigneshrajan94, @GCHQDeveloper58, will say you what ai you used?

HackingRepo avatar Jun 04 '26 06:06 HackingRepo

@vigneshrajan94 thank you for your pull request. In general, we would prefer minimally scoped individual pull requests, rather than pull requests incorporating multiple features/bug fixes. Would you mind splitting the pull request into one for each issue? This makes it easier for us to review and to track changes.

C85297 avatar Jun 15 '26 08:06 C85297