question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unsafe access to uninitialized variables is allowed in any function

See original GitHub issue

TypeScript 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:

https://www.typescriptlang.org/play/#src=function blowup() { ohNo.toLocaleLowerCase()%3B } let ohNo%3A string%3B blowup()%3B

Related Issues: Not really. It’s somewhat related to the old bugs around class property initialization not being enforced.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Feb 28, 2019

“immediate” as in, direct code statements, not function calls, since functions seem to be the current limitation

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.

0reactions
ziloencommented, Nov 11, 2022
function someFn() {
  console.log(someVar) // this will cause runtime errors
} 
someFn()
const someVar = 123
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found