From: Chris Duncan Date: Sun, 9 Aug 2026 20:37:10 +0000 (-0700) Subject: Fix possible undefined access. X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=fae66caf40a6cfd477d8aa6a6bb59b283640f5d4;p=libnemo.git Fix possible undefined access. --- diff --git a/src/lib/rpc/accounts_balances.ts b/src/lib/rpc/accounts_balances.ts index ebc90e0..bce825a 100644 --- a/src/lib/rpc/accounts_balances.ts +++ b/src/lib/rpc/accounts_balances.ts @@ -28,8 +28,9 @@ export function accounts_balances (body: unknown): AccountsBalancesResponse { throw new RpcError(error) } } catch (err: any) { - response.errors ??= Object.create(null) - response.errors[address] = err?.message + const errors = response.errors ?? Object.create(null) + response.errors = errors + errors[address] = err?.message } } } @@ -43,11 +44,13 @@ export function accounts_balances (body: unknown): AccountsBalancesResponse { if (data == null || typeof data !== 'object') { throw new RpcError('Invalid account balance data') } - response.balances ??= Object.create(null) - response.balances[address] = account_balance(data) + const balances = response.balances ?? Object.create(null) + response.balances = balances + balances[address] = account_balance(data) } catch (err: any) { - response.errors ??= Object.create(null) - response.errors[address] ??= err?.message + const errors = response.errors ?? Object.create(null) + response.errors = errors + errors[address] = err?.message } } } diff --git a/src/lib/rpc/accounts_frontiers.ts b/src/lib/rpc/accounts_frontiers.ts index 5b84d45..173eae4 100644 --- a/src/lib/rpc/accounts_frontiers.ts +++ b/src/lib/rpc/accounts_frontiers.ts @@ -30,12 +30,14 @@ export function accounts_frontiers (body: unknown): AccountsFrontiersResponse { throw new RpcError('Invalid account address') } if (hex.is(frontier, 64)) { - response.frontiers ??= Object.create(null) - response.frontiers[address] = frontier + const frontiers = response.frontiers ?? Object.create(null) + response.frontiers = frontiers + frontiers[address] = frontier } } catch (err: any) { - response.errors ??= Object.create(null) - response.errors[address] = err?.message + const errors = response.errors ?? Object.create(null) + response.errors = errors + errors[address] = err?.message } } } @@ -52,8 +54,9 @@ export function accounts_frontiers (body: unknown): AccountsFrontiersResponse { throw new Error(error) } } catch (err: any) { - response.errors ??= Object.create(null) - response.errors[address] = err?.message + const errors = response.errors ?? Object.create(null) + response.errors = errors + errors[address] = err?.message } } } diff --git a/src/lib/rpc/blocks_info.ts b/src/lib/rpc/blocks_info.ts index 2260bff..a5d088f 100644 --- a/src/lib/rpc/blocks_info.ts +++ b/src/lib/rpc/blocks_info.ts @@ -28,8 +28,9 @@ export function blocks_info (body: unknown): BlocksInfoResponse { throw new Error('Invalid blocks_info error') } } catch (err: any) { - response.errors ??= Object.create(null) - response.errors[hash] = err?.message + const errors = response.errors ?? Object.create(null) + response.errors = errors + errors[address] = err?.message } } } @@ -38,14 +39,16 @@ export function blocks_info (body: unknown): BlocksInfoResponse { for (const [hash, data] of Object.entries(body.blocks) as [string, unknown][]) { try { if (hex.is(hash)) { - response.blocks ??= Object.create(null) - response.blocks[hash] = block_info(data) + const blocks = response.blocks ?? Object.create(null) + response.blocks = blocks + blocks[hash] = block_info(data) } else { throw new Error('Invalid blocks_info hash') } } catch (err: any) { - response.errors ??= Object.create(null) - response.errors[hash] ??= err?.message + const errors = response.errors ?? Object.create(null) + response.errors = errors + errors[address] = err?.message } } }