Unsafe access to uninitialized variables is allowed in any function
See original GitHub issueTypeScript Version: 3.3.1
Search Terms: uninitialized variable, unsafe access
Code
function blowup() {
ohNo.toLocaleLowerCase();
}
let ohNo: string;
blowup();
Expected behavior:
This should not compile when using strictNullChecks
Actual behavior:
It compiles, probably because the flow analysis abandonned.
This convenience is not worth the safety tradeoff, the compiler should enforce that ohNo
be typed as string | undefined
since the initialization code is not immediately following.
Playground Link:
Related Issues: Not really. It’s somewhat related to the old bugs around class property initialization not being enforced.
Issue Analytics
- State:
- Created 5 years ago
- Comments:10 (4 by maintainers)
Top Results From Across the Web
Eliminating the Danger of Uninitialized Variables
INTRODUCTION: An uninitialized variable has an undefined value, often corresponding to the data that was already in the particular memory ...
Read more >What are the dangers of uninitialised variables? - Stack Overflow
Never access uninitialized variable ! It is undefined behavior and it's much much worse than "the variable has some garbage value".
Read more >EXP33-C. Do not read uninitialized memory
Uninitialized automatic variables or dynamically allocated memory has indeterminate values, which for objects of some types, can be a trap representation.
Read more >CWE-457: Use of Uninitialized Variable (4.9) - MITRE
An attacker can sometimes control or read these contents. In other languages or conditions, a variable that is not explicitly initialized can be...
Read more >Uninitialized variables - cppreference.com
It is possible to create a variable without a value. This is very dangerous, but it can give an efficiency boost in certain...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
What I mean is, the
ohYes
variable is not immediately initialized in your example, because there’s an intervening possibly-undefined-observing function call between its declaration and its definite initialization. I suspect that will often be the case even for code that is well-formed.We could add a flag but this could also be syntactically enforced by a lint rule, so I’d generally defer to that route, especially since the definition of “immediate” is really up for debate.