Unverified Commit 47e4caea authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Validate analyzer source children

Co-authored-by: 's avatarWill Binns-Smith <755844+wbinnssmith@users.noreply.github.com>
parent 1d4944b9
...@@ -109,7 +109,9 @@ describe('analyzer data parser', () => { ...@@ -109,7 +109,9 @@ describe('analyzer data parser', () => {
expect(analyze.sourceChildren(0)).toEqual([1]) expect(analyze.sourceChildren(0)).toEqual([1])
const modules = new ModulesData(modulesBuffer()) const modules = new ModulesData(modulesBuffer())
expect(modules.getModuleIndiciesFromPath('[project]/src/a.ts')).toHaveLength(2) expect(
modules.getModuleIndiciesFromPath('[project]/src/a.ts')
).toHaveLength(2)
expect(modules.moduleDependents(0)).toEqual([1]) expect(modules.moduleDependents(0)).toEqual([1])
expect(modules.asyncModuleDependents(1)).toEqual([0]) expect(modules.asyncModuleDependents(1)).toEqual([0])
expect(modules.moduleDependencies(1)).toEqual([0]) expect(modules.moduleDependencies(1)).toEqual([0])
...@@ -125,12 +127,12 @@ describe('analyzer data parser', () => { ...@@ -125,12 +127,12 @@ describe('analyzer data parser', () => {
expect(() => new AnalyzeData(frame({}, []))).toThrow('analyze.data sources') expect(() => new AnalyzeData(frame({}, []))).toThrow('analyze.data sources')
}) })
it('rejects source parent cycles', () => { it('rejects source child cycles', () => {
const section = edges([[1], [0]]) const section = edges([[1], [0]])
const cyclic = frame( const cyclic = frame(
{ {
sources: [ sources: [
{ parent_source_index: 1, path: 'a' }, { parent_source_index: null, path: 'a' },
{ parent_source_index: 0, path: 'b' }, { parent_source_index: 0, path: 'b' },
], ],
chunk_parts: [], chunk_parts: [],
...@@ -144,11 +146,11 @@ describe('analyzer data parser', () => { ...@@ -144,11 +146,11 @@ describe('analyzer data parser', () => {
offset: edges([]).byteLength + edges([[], []]).byteLength, offset: edges([]).byteLength + edges([[], []]).byteLength,
length: section.byteLength, length: section.byteLength,
}, },
source_roots: [], source_roots: [0],
}, },
[edges([]), edges([[], []]), section] [edges([]), edges([[], []]), section]
) )
expect(() => new AnalyzeData(cyclic)).toThrow('source parent cycle') expect(() => new AnalyzeData(cyclic)).toThrow('source children')
}) })
it('rejects malformed adjacency sections', () => { it('rejects malformed adjacency sections', () => {
......
...@@ -313,6 +313,24 @@ export class AnalyzeData { ...@@ -313,6 +313,24 @@ export class AnalyzeData {
throw new Error('Invalid analyze.data source') throw new Error('Invalid analyze.data source')
} }
} }
const childParents = new Array(sources.length).fill(-1)
for (let parent = 0; parent < sources.length; parent++) {
for (const child of this.sourceChildren(parent)) {
if (
childParents[child] !== -1 ||
sources[child].parent_source_index !== parent
) {
throw new Error('Invalid analyze.data source children')
}
childParents[child] = parent
}
}
for (let index = 0; index < sources.length; index++) {
const parent = sources[index].parent_source_index
if (parent !== null && childParents[index] !== parent) {
throw new Error('Invalid analyze.data source children')
}
}
for (const part of parts) { for (const part of parts) {
if ( if (
!Number.isInteger(part?.source_index) || !Number.isInteger(part?.source_index) ||
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment