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

fix: builder.writeInt(0, 1); // value is not zero or -1 for 1 bits. Got 0 #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

stels-cs
Copy link

This code will throw an error, but should work

let builder = new BitBuilder();
builder.writeInt(0, 1); // value is not zero or -1 for 1 bits. Got 0

The error occurs because the variable value is checked on line 155 instead of v from line 148.
value can be a number or a BigInt, so comparing it with BigInt constants will not work.

writeInt(value: bigint | number, bits: number) {
let v = BigInt(value);
if (bits < 0 || !Number.isSafeInteger(bits)) {
throw Error(`invalid bit length. Got ${bits}`);
}
// Corner case for zero bits
if (bits === 0) {
if (value !== 0n) {
throw Error(`value is not zero for ${bits} bits. Got ${value}`);
} else {
return;
}
}

Same problem with

builder.writeInt(0, 0);
builder.writeInt(-1, 1);

@stels-cs stels-cs changed the title fix: error witeInt with number and size 1 or 0 fix: builder.writeInt(0, 1); // value is not zero or -1 for 1 bits. Got 0 Oct 24, 2024
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

Successfully merging this pull request may close these issues.

1 participant