Skip to content

[Bug] Fix non-short-circuit operator in for-loop condition #14509

Description

@cxhello

Background

Follow-up to #14469 (SpotBugs check enforcement). This addresses the NS_DANGEROUS_NON_SHORT_CIRCUIT pattern — using the non-short-circuit operator & instead of && in a boolean expression. When used in a for-loop condition, & always evaluates both operands even when the first is already false, which is unnecessary and potentially dangerous.

Affected locations (1 occurrence)

File Line Current code
core/.../NacosServerLoaderService.java L137 for (int i = 0; i < overLimitServer.size() & i < lowLimitServer.size(); i++)

The & operator evaluates both i < overLimitServer.size() and i < lowLimitServer.size() unconditionally. With &&, the second condition would be skipped when the first is already false, which is the correct short-circuit behavior for a loop guard.

Fix

Replace & with && in the for-loop condition (single character change):

- for (int i = 0; i < overLimitServer.size() & i < lowLimitServer.size(); i++) {
+ for (int i = 0; i < overLimitServer.size() && i < lowLimitServer.size(); i++) {

After fixing, the NS_DANGEROUS_NON_SHORT_CIRCUIT exclusion in style/spotbugs-exclude.xml should be removed so that future occurrences are caught by CI.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions