GoogleContainerTools / GoogleContainerTools/jib

Inverted boolean logic in HistoryEntry.hasCorrespondingLayer()

Open
#3,581 8 comments 0 reactions 0 assignees View on GitHub
priority:p4 type:bug
Dominant language
Java
Stars
14.5k
Forks
1.5k
PR merge metrics
No merged PRs in 30d

Description

**Environment**:

- *Jib version:* jib.core 0.20.0
- *Build tool:* N/A
- *OS:* N/A

**Description of the issue**:

Method `hasCorrespondingLayer()` of `class com.google.cloud.tools.jib.image.json.HistoryEntry` returns wrong values. It returns **true** for empty layers and vice versa.

**Expected behavior**:

Method should return false for empty layers and true for layers with content.

**Steps to reproduce**:

N/A. Sorry, just look at the one-line implementation of the method. The bug is obvious.
https://github.com/GoogleContainerTools/jib/blob/master/jib-core/src/main/java/com/google/cloud/tools/jib/image/json/HistoryEntry.java#L138-L146

**Additional Information**:

There seems to be only one use of the method within jib here:
https://github.com/GoogleContainerTools/jib/blob/0ed7dca36864b6b19ff61629fc578018041fa15f/jib-core/src/main/java/com/google/cloud/tools/jib/builder/steps/BuildImageStep.java#L96-L98

The snippet is similarly wrong:
```
if (!historyObject.hasCorrespondingLayer()) {
nonEmptyLayerCount++;
}
```
The if statement as written will trigger for empty layers, but the variable is named nonEmptyLayerCount. The logic is inverted which hides the underlying error in hasCorrespondingLayer().

hasCorrespondingLayer() needs to be fixed and this code adapted to:
```
if (historyObject.hasCorrespondingLayer()) {
nonEmptyLayerCount++;
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.