Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

It sorts dictionary entries incorrectly. #14

Open
issuefiler opened this issue May 27, 2023 · 0 comments
Open

It sorts dictionary entries incorrectly. #14

issuefiler opened this issue May 27, 2023 · 0 comments

Comments

@issuefiler
Copy link

Bug

encodeDictionary = (object) ->
keys = (key for own key of object).sort()

   keys = (key for own key of object).sort() 

This is not the correct way of sorting dictionary entries. The encoder is producing specification-incompliant octets that’ll be floating around in the wild, polluting the network.


When you say “strings” in the context of Bencoding, you mean “binary strings,” or more specifically, “8-bit byte sequences.”

BEP 52 — The BitTorrent protocol specification version 2

Note that, in the context of bencoding, strings, including dictionary keys, are arbitrary byte sequences (uint8_t[]).

And Array.prototype.sort compares 16-bit units by default.

If compareFn is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order.

The simple .sort() results in a different order (sorted_in_utf16) than the correct one (sorted_in_utf8). Observe:

const A = String.fromCodePoint(0xFF61);
const B = String.fromCodePoint(0x10002);
const sorted_in_utf8 = [A, B].sort((a, b) => Buffer.compare(Buffer.from(a), Buffer.from(b))); // [A, B]
const sorted_in_utf16 = [A, B].sort(); // [B, A]

The related issue

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

No branches or pull requests

1 participant