mirror of
https://github.com/public-transport/db-vendo-client.git
synced 2025-02-23 15:19:35 +02:00
parseLocation: prevent endless recursive loops
This commit is contained in:
parent
76e310218a
commit
1abafb5bd4
2 changed files with 14 additions and 2 deletions
|
@ -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
|
||||||
|
|
|
@ -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}}
|
||||||
|
|
Loading…
Add table
Reference in a new issue