Skip to content

Commit 887f71a

Browse files
committed
fix: in js 0 is falsy, which messes with ternary logic
We have this helper function which converts `bigint` to `number` or raises an error in the unlikely case that it's out of bounds. And for typing reasons, we want to keep its signature purely `function(bigint): number`. But in a few places, we needed to call that function on a value which might be undefined. So we used ternary logic, like this: ```js foo? saveBigIntToNumber(foo): undefined ``` And on its face that does the right thing: both `null` and `undefined` are falsy in JS, so you get `undefined` out. What we hadn't considered was that the defined number 0 is _also_ falsy in JS, meaning that those instances could change a defined value to an undefined one, causing some subtle bugs. So let's get really explicit about this and do things properly.
1 parent ec4d0df commit 887f71a

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

crypto-ffi/bindings/js/src/Conversions.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,12 @@ export function safeBigintToNumber(x: bigint): number {
99
}
1010
return new Number(x).valueOf();
1111
}
12+
13+
export function safeBigIntOrUndefinedToNumberOrUndefined(
14+
x: bigint | undefined | null
15+
): number | undefined {
16+
if (x === undefined || x === null) {
17+
return undefined;
18+
}
19+
return safeBigintToNumber(x);
20+
}

crypto-ffi/bindings/js/src/CoreCryptoE2EI.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { safeBigintToNumber } from "./Conversions.js";
1+
import { safeBigIntOrUndefinedToNumberOrUndefined } from "./Conversions.js";
22
import * as CoreCryptoFfiTypes from "./autogenerated/core-crypto-ffi.d.js";
33

44
import {
@@ -30,7 +30,7 @@ export interface CRLRegistration {
3030
export function crlRegistrationFromFfi(r: CrlRegistrationFfi): CRLRegistration {
3131
return {
3232
dirty: r.dirty,
33-
expiration: r.expiration ? safeBigintToNumber(r.expiration) : undefined,
33+
expiration: safeBigIntOrUndefinedToNumberOrUndefined(r.expiration),
3434
};
3535
}
3636

crypto-ffi/bindings/js/src/CoreCryptoMLS.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { safeBigintToNumber } from "./Conversions";
1+
import { safeBigIntOrUndefinedToNumberOrUndefined } from "./Conversions";
22
import {
33
BufferedDecryptedMessage as BufferedDecryptedMessageFfi,
44
CommitBundle as CommitBundleFfi,
@@ -198,9 +198,7 @@ export function bufferedDecryptedMessageFromFfi(
198198
return {
199199
message: m.message,
200200
isActive: m.isActive,
201-
commitDelay: m.commitDelay
202-
? safeBigintToNumber(m.commitDelay)
203-
: undefined,
201+
commitDelay: safeBigIntOrUndefinedToNumberOrUndefined(m.commitDelay),
204202
senderClientId: m.senderClientId,
205203
hasEpochChanged: m.hasEpochChanged,
206204
identity: m.identity,

0 commit comments

Comments
 (0)