Skip to content

Add a helper function getOrUpdateProperty to prevent unprotected access to Maps. #10115

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
1 commit merged into from
Aug 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,12 @@ namespace ts {
return keys;
}

export function getProperty<T>(map: Map<T>, key: string): T {
return hasOwnProperty.call(map, key) ? map[key] : undefined;
export function getProperty<T>(map: Map<T>, key: string): T | undefined {
return hasProperty(map, key) ? map[key] : undefined;
}

export function getOrUpdateProperty<T>(map: Map<T>, key: string, makeValue: () => T): T {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are there any other places that should use this function? I think we use objects as maps quite a lot.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a lot of places that use code like this -- just search for ] || -- but they seem to be using numeric IDs (so are safe from default properties) or are safe for other reasons.
I'm also currently looking into using Maps as maps.

return hasProperty(map, key) ? map[key] : map[key] = makeValue();
}

export function isEmpty<T>(map: Map<T>) {
Expand Down Expand Up @@ -941,7 +945,7 @@ namespace ts {
* [^./] # matches everything up to the first . character (excluding directory seperators)
* (\\.(?!min\\.js$))? # matches . characters but not if they are part of the .min.js file extension
*/
const singleAsteriskRegexFragmentFiles = "([^./]|(\\.(?!min\\.js$))?)*";
const singleAsteriskRegexFragmentFiles = "([^./]|(\\.(?!min\\.js$))?)*";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what changes on this line? Is it the line ending?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's line ending. This line ends with just an LF in master.

Which is strange, because we have a lint rule covering this which is not triggering.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it only detect extra line endings? It just fired for me when I had an extra.

const singleAsteriskRegexFragmentOther = "[^/]*";

export function getRegularExpressionForWildcard(specs: string[], basePath: string, usage: "files" | "directories" | "exclude") {
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6842,7 +6842,7 @@ const _super = (function (geti, seti) {
// export { x, y }
for (const specifier of (<ExportDeclaration>node).exportClause.elements) {
const name = (specifier.propertyName || specifier.name).text;
(exportSpecifiers[name] || (exportSpecifiers[name] = [])).push(specifier);
getOrUpdateProperty(exportSpecifiers, name, () => []).push(specifier);
}
}
break;
Expand Down
9 changes: 9 additions & 0 deletions tests/baselines/reference/exportToString.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//// [exportToString.ts]
const toString = 0;
export { toString };


//// [exportToString.js]
"use strict";
var toString = 0;
exports.toString = toString;
7 changes: 7 additions & 0 deletions tests/baselines/reference/exportToString.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
=== tests/cases/compiler/exportToString.ts ===
const toString = 0;
>toString : Symbol(toString, Decl(exportToString.ts, 0, 5))

export { toString };
>toString : Symbol(toString, Decl(exportToString.ts, 1, 8))

8 changes: 8 additions & 0 deletions tests/baselines/reference/exportToString.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
=== tests/cases/compiler/exportToString.ts ===
const toString = 0;
>toString : number
>0 : number

export { toString };
>toString : number

2 changes: 2 additions & 0 deletions tests/cases/compiler/exportToString.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const toString = 0;
export { toString };