parseLocation: prevent endless recursive loops

This commit is contained in:
Jannis R 2020-02-15 19:14:50 +00:00
parent 76e310218a
commit 1abafb5bd4
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
2 changed files with 14 additions and 2 deletions

View file

@ -112,4 +112,13 @@ const parseLocation = (ctx, l) => {
return res return res
} }
module.exports = parseLocation // We use a "visitied list" to prevent endless recursion.
const seen = Symbol('parseLocation seen items')
const parseLocationWithoutCycles = (ctx, l, ...args) => {
if (ctx[seen] && ctx[seen].includes(l)) return null
const newSeen = ctx[seen] ? [...ctx[seen], l] : [l]
return parseLocation({...ctx, [seen]: newSeen}, l, ...args)
}
module.exports = parseLocationWithoutCycles

View file

@ -137,7 +137,10 @@ test('handles recursive references properly', (t) => {
type: 'S', type: 'S',
name: 'Northern Platform', name: 'Northern Platform',
lid: 'a=b@L=northern-platform', lid: 'a=b@L=northern-platform',
crd: {x: 44444444, y: 33333333} crd: {x: 44444444, y: 33333333},
// This doesn't make sense semantically, but we test if
// `parseLocation` falls into an endless recursive loop.
entryLocL: [0]
} }
const common = {locL: [southernInput, northernInput]} const common = {locL: [southernInput, northernInput]}
const _ctx = {...ctx, res: {common}} const _ctx = {...ctx, res: {common}}