Skip to content
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

chore: prevent unchecked index access #1612

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open

Conversation

weboko
Copy link
Collaborator

@weboko weboko commented Sep 25, 2023

Problem

When access happens by index it is possible to hit a case when no value returned.
With no proper handling of such edge case it's easy to hit a problem in run time and increase amount of problems for consumers.
Examples of such issue is here - #1562

Solution

Enable TypeScript to check for such a case

Notes

@weboko weboko marked this pull request as ready for review September 25, 2023 23:03
@weboko weboko requested a review from a team as a code owner September 25, 2023 23:03
@weboko weboko marked this pull request as draft September 25, 2023 23:11
@weboko weboko force-pushed the weboko/ts-warning branch from 26bf15b to 9e12340 Compare February 1, 2024 21:31
@weboko weboko marked this pull request as ready for review February 1, 2024 23:36
Copy link

github-actions bot commented Feb 1, 2024

size-limit report 📦

Path Size Loading time (3g) Running time (snapdragon) Total time
Waku core 36.22 KB (+0.07% 🔺) 725 ms (+0.07% 🔺) 251 ms (+20.44% 🔺) 976 ms
Waku Simple Light Node 278.33 KB (+0.02% 🔺) 5.6 s (+0.02% 🔺) 472 ms (+12.08% 🔺) 6.1 s
ECIES encryption 32.07 KB (+0.17% 🔺) 642 ms (+0.17% 🔺) 233 ms (+32.13% 🔺) 875 ms
Symmetric encryption 32.06 KB (+0.19% 🔺) 642 ms (+0.19% 🔺) 329 ms (+105.55% 🔺) 971 ms
DNS discovery 107.65 KB (-0.06% 🔽) 2.2 s (-0.06% 🔽) 325 ms (-43.1% 🔽) 2.5 s
Privacy preserving protocols 128.92 KB (-0.01% 🔽) 2.6 s (-0.01% 🔽) 393 ms (+20.53% 🔺) 3 s
Light protocols 34.27 KB (+0.03% 🔺) 686 ms (+0.03% 🔺) 199 ms (+0.81% 🔺) 885 ms
History retrieval protocols 32.77 KB (+0.21% 🔺) 656 ms (+0.21% 🔺) 173 ms (+1.91% 🔺) 829 ms
Deterministic Message Hashing 5.96 KB (0%) 120 ms (0%) 41 ms (-18.92% 🔽) 160 ms

Copy link
Collaborator

@danisharora099 danisharora099 left a comment

Choose a reason for hiding this comment

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

nice!

Comment on lines +25 to +26
const tmp = arr[i] as T;
arr[i] = arr[j] as T;
Copy link
Collaborator

Choose a reason for hiding this comment

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

does it not automatically infer the type? arr is of the type T[] 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

noUncheckedIndexedAccess makes so anything that is accepted by index becomes T | undefined

Comment on lines +70 to +71
const clusterId = parseInt(parts[4] as string);
const shard = parseInt(parts[5] as string);
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: similar comment as above of not automatically inferring the types

@@ -3,6 +3,7 @@
"compilerOptions": {
"module": "ESNext",
"moduleResolution": "Bundler",
"noEmit": true
"noEmit": true,
"noUncheckedIndexedAccess": false,
Copy link
Collaborator

Choose a reason for hiding this comment

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

why is this false here? 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

tsconfig.dev is used for tests where we don't really need to enforce this rule

Copy link
Collaborator

Choose a reason for hiding this comment

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

wouldn't it be still good to have tests with no unchecked index access? can see it infact being more useful because it would spot errors before execution level :D

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

not sure, let's discuss

I can enable it for tests but in that case many utilities we write would need to be compliant with more eager checks
I am in favor of enabling it for tests too tbh

@adklempner wdyt?

Copy link
Member

Choose a reason for hiding this comment

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

Sounds beneficial but warrants a separate PR imo

Copy link
Collaborator

Choose a reason for hiding this comment

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

^ agree

{
test: /\.([cm]?ts|tsx)$/,
loader: "ts-loader",
options: { configFile: tsConfigFile },
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This change makes tests running in with tsconfig.dev.json which makes sense as we do have a lower bar for linting tests.

@waku-org/js-waku-developers

"test": "NODE_ENV=test npm run test --workspaces --if-present",
"test:browser": "NODE_ENV=test npm run test:browser --workspaces --if-present",
"test:node": "NODE_ENV=test npm run test:node --workspaces --if-present",
"test": "npm run test --workspaces --if-present",
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Is not needed as each of internal commands should be worrying about supplying ENV flags
fyi @danisharora099

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants