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

Add null checking when getting value from Map #388

Open
7hong13 opened this issue Sep 23, 2022 · 0 comments
Open

Add null checking when getting value from Map #388

7hong13 opened this issue Sep 23, 2022 · 0 comments
Labels
bug 🐞 Something isn't working

Comments

@7hong13
Copy link

7hong13 commented Sep 23, 2022

Description:
When getting value from Map data structure, it can return null value(undefined in TS) for not having the key.
But I found out some functions not checking it properly.

For example, getPrevCreatedAt function in RGATreeList is implemented as below:

  public getPrevCreatedAt(createdAt: TimeTicket): TimeTicket { 
    let node = this.nodeMapByCreatedAt.get(createdAt.toIDString()); 
    do {
      node = node!.getPrev()!;
    } while (this.dummyHead !== node && node.isRemoved());
    return node.getValue().getCreatedAt();
  }

It is not checking the case that nodeMapByCreatedAt doesn't contain the key and node is assigned null value.

Thus, it might be better to fix it like this:

  public getPrevCreatedAt(createdAt: TimeTicket): TimeTicket { 
    let node = this.nodeMapByCreatedAt.get(createdAt.toIDString());
    if (!node) {
        // throw error or exception
    }
    do {
      node = node!.getPrev()!;
    } while (this.dummyHead !== node && node.isRemoved());
    return node.getValue().getCreatedAt();
  }

I only looked through RGATreeList, so it might be necessary to check other parts of the code as well.

Why:

@hackerwins hackerwins added the bug 🐞 Something isn't working label Sep 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐞 Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants