Skip to content

Potential unreachable condition in FileInfo.moveToNewLocation #4736

Open

Description

@silviaperelli

BUG REPORT

Describe the bug

While analyzing the implementation of FileInfo.moveToNewLocation, I noticed a condition that appears to be unreachable.

Specifically, the following check:

if (written <= 0 && size > 0)

seems redundant given the logic of the preceding loop.

To Reproduce

This is not a runtime bug but a logical issue identified through static analysis.

Relevant code:

long written = 0;
while (written < size) {
    long count = fc.transferTo(written, size, newFc);
    if (count <= 0) {
        throw new IOException("Copying to new location " + rlocFile + " failed");
    }
    written += count;
}

Expected behavior

The condition after the loop should represent a reachable state.

However, based on the current logic:

  • If size > 0, the loop executes at least once.
  • If count <= 0, an exception is thrown immediately.
  • Otherwise, written is incremented (written += count, with count > 0).

Therefore, after normal loop termination, written should always be greater than 0.

Screenshots

Not applicable.

Additional context

Given the loop semantics, the condition:

if (written <= 0 && size > 0)

appears to be unreachable.

This might be:

  • a leftover defensive check, or
  • redundant code that could be removed for clarity.

cc @gulyx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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