MuntashirAkon/AppManager · error · IllegalArgumentException

Negative size

Error message

Negative size

What it means

RemoteFileChannel.truncate(size) throws IllegalArgumentException("Negative size") when the requested truncation length is below zero, mirroring FileChannel's contract. The channel checks this before any IPC to avoid sending an invalid size to the remote ftruncate.

Solutions

  1. Clamp the target size: use Math.max(0, size) before calling truncate.
  2. Validate the source of the size (e.g. length()) and treat -1 as an error before truncating.
  3. Check the subtraction that produced the size for underflow.
  4. Assert size >= 0 in the calling code path with a clear failure message.

Example fix

// before
long size = channel.size() - padding;
channel.truncate(size); // may be negative
// after
long size = Math.max(0, channel.size() - padding);
channel.truncate(size);
Defensive patterns

Strategy: validation

Validate before calling

if (size < 0) {
    throw new IllegalArgumentException("truncate size must be >= 0, got " + size);
}
channel.truncate(size);

Try / catch

try {
    channel.truncate(size);
} catch (IllegalArgumentException e) {
    logger.warn("Bad truncate size {}", size, e);
}

Prevention

When it happens

Trigger: Calling truncate(size) with size < 0, typically from a computed value like fileSize - extra where fileSize was -1 (unknown) or an arithmetic underflow.

Common situations: Using a size obtained from a failed stat/length() call (-1); subtracting a padding larger than the file; passing an uninitialized sentinel value.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of MuntashirAkon/AppManager@0152f468fc (2026-09-12). Data as JSON: /api/errors/c8205d0cd702e054. Report an issue: GitHub.

Appendix: source

Thrown at libcore/io/src/main/java/io/github/muntashirakon/io/RemoteFileChannel.java:253

            throw new IOException(e);
        }
    }

    @Override
    public long size() throws IOException {
        ensureOpen();
        try {
            return fs.size(handle).tryAndGet();
        } catch (RemoteException e) {
            throw new IOException(e);
        }
    }

    @Override
    public RemoteFileChannel truncate(long size) throws IOException {
        ensureOpen();
        if (size < 0)
            throw new IllegalArgumentException("Negative size");
        if (!writable())
            throw new NonWritableChannelException();
        try {
            fs.ftruncate(handle, size).checkException();
            return this;
        } catch (RemoteException e) {
            throw new IOException(e);
        }
    }

    @Override
    public void force(boolean metaData) throws IOException {
        ensureOpen();
        try {
            fs.sync(handle, metaData).checkException();
        } catch (RemoteException e) {
            throw new IOException(e);
        }

View on GitHub (pinned to 0152f468fc)