volarjs/volar.js

Directory named "constructor" could crash

Dmitrigar opened this issue · 1 comments

If a project have a directory named "constructor" it could crash with error like
TypeError: Cannot read properties of undefined (reading '<something>')

I found it happening because of a bug in the file packages/typescript/src/sys.ts , function getDirFromDir

	function getDirFromDir(dir: Dir, name: string) {
		let target = dir.dirs[name]; // <-------------- BUGGY LINE
		if (!target) {
			target = {
				dirs: {},
				files: {},
			};
			dir.dirs[name] = target;
		}
		return target;
	}

The function getDirFromDir gets called in a loop iterating through some directory path.
If there are a directory named "constructor" in the middle of the path then it will crash.

Getting name "constructor" of an empty object (which dir.dirs could be) returns the actual object constructor instead of a Dir object or undefined.
Then the object constructor gets returned as if it was a Dir found.
On the next call of getDirFromDir function there will be the object constructor used as the dir: Dir argument.
So if there is next call then the function throws a null-refference error because there is no dirs in the object constructor.

I kind of reproduced the bug and it seems like changing the buggy line would fix the issue:

let target = name in dir.dirs ? dir.dirs[name] : undefined;

Thanks for looking into this!