Skip to content

Commit

Permalink
fix incorrect byte array comparison (#8379)
Browse files Browse the repository at this point in the history
Fixes an exception when opening the code editor. `indexedDB.cmp` doesn't accept `undefined` values. Moved its usage to a separate function to guard against that.
  • Loading branch information
Frizi authored Nov 24, 2023
1 parent c6b6384 commit ec5c60b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
6 changes: 3 additions & 3 deletions app/gui2/src/stores/graph/graphDatabase.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { nonDictatedPlacement } from '@/components/ComponentBrowser/placement'
import { SuggestionDb, groupColorStyle, type Group } from '@/stores/suggestionDatabase'
import type { SuggestionEntry } from '@/stores/suggestionDatabase/entry'
import { tryGetIndex } from '@/util/array'
import { byteArraysEqual, tryGetIndex } from '@/util/array'
import { Ast, AstExtended } from '@/util/ast'
import { AliasAnalyzer } from '@/util/ast/aliasAnalysis'
import { colorFromString } from '@/util/colors'
Expand Down Expand Up @@ -262,13 +262,13 @@ export class GraphDb {
if (node == null) {
this.nodeIdToNode.set(nodeId, newNode)
} else {
if (indexedDB.cmp(node.pattern?.contentHash(), newNode.pattern?.contentHash())) {
if (!byteArraysEqual(node.pattern?.contentHash(), newNode.pattern?.contentHash())) {
node.pattern = newNode.pattern
}
if (node.outerExprId !== newNode.outerExprId) {
node.outerExprId = newNode.outerExprId
}
if (indexedDB.cmp(node.rootSpan.contentHash(), newNode.rootSpan.contentHash()) !== 0) {
if (!byteArraysEqual(node.rootSpan.contentHash(), newNode.rootSpan.contentHash())) {
node.rootSpan = newNode.rootSpan
}
}
Expand Down
8 changes: 8 additions & 0 deletions app/gui2/src/util/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,11 @@ export function partitionPoint<T>(
export function tryGetIndex<T>(arr: Opt<T[]>, index: Opt<number>): T | undefined {
return index == null ? undefined : arr?.[index]
}

/**
* Check if two byte arrays have the same value. Allows optional values. If both inputs are `null`
* or `undefined`, the comparison also succeeds.
*/
export function byteArraysEqual(a: Opt<Uint8Array>, b: Opt<Uint8Array>): boolean {
return a === b || (a != null && b != null && indexedDB.cmp(a, b) === 0)
}

0 comments on commit ec5c60b

Please sign in to comment.