Skip to content

Add null checking when getting value from Map #388

@7hong13

Description

@7hong13

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:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug 🐞Something isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions