+
Skip to content

Redundant null-checks in PolicyAdapter. SAST #40578

Open
@Babaijan

Description

@Babaijan

Description

The updated variable is checked after dereference. That is, if a null value is called in the if block, an exception will be thrown and the code will not reach the updated check for null.

Discussion

No response

Motivation

Code clarity and maintainability

Details

Even though the null check appears after the dereference of updated, in practice, the method reference assigned to modelSupplier - namely this::getPolicyModel() - is implemented in such a way that it is not expected to return null. Specifically, getPolicyModel() retrieves the policy via findById(), and in this context the underlying data model and session cache are always initialized during usage. Therefore, the method modelSupplier.get() is effectively guaranteed to return a non-null value.

public Policy getDelegateForUpdate() {
if (updated == null) {
updated = modelSupplier.get();
String defaultResourceType = updated.getConfig().get("defaultResourceType");
cacheSession.registerPolicyInvalidation(cached.getId(), cached.getName(), cached.getResourcesIds(session, modelSupplier), cached.getScopesIds(session, modelSupplier), defaultResourceType, cached.getResourceServerId());
if (updated == null) throw new IllegalStateException("Not found in database");
}
return updated;

Also, in case of an error, the code will throw an exception before reaching the if check.
Hence the check at the end of the code in the if block is unreachable.

if (updated == null) throw new IllegalStateException("Not found in database");

Found by Linux Verification Center with SVACE

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载