Binary decoder returns incorrect data after short stream reads

Open
#56 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
java
Domain
backend

Research direction

Start in BaseBinaryInputReader.checkBuffer() and reproduce the issue with the provided FixedBinaryDecoder and fragmented ByteArrayInputStream example. Trace directGetBytes() for byte-array input and the handling of MALException. Done means fragmented reads accumulate the requested bytes, insufficient streams or buffers throw MALException, and integers, strings, and byte blocks no longer contain incorrect or padded data.

Written by the indexing model from the issue text.

Description

On master 028751ad, BaseBinaryInputReader.checkBuffer() performs only one stream read before allowing decoding. A valid short read leaves part of the requested value unfilled, so the decoder can silently return incorrect data.

InputStream input = new ByteArrayInputStream(new byte[]{1, 35, 69, 103}) {
    @Override
    public synchronized int read(byte[] buffer, int offset, int length) {
        return super.read(buffer, offset, Math.min(length, 1));
    }
};
new FixedBinaryDecoder(input, null, false).decodeInteger();

Expected: 0x01234567 (19088743). Actual: 0x01000000 (16777216).

Strings and byte blocks also contain incorrect bytes after fragmented reads. With byte-array input, checkBuffer() does not verify the readable length, so directGetBytes() can silently pad truncated input with zeros or decode unused buffer capacity.

The reader should accumulate enough bytes before decoding and throw MALException when the stream or buffer cannot satisfy the requested length.

Dominant language
Java
Stars
18
Forks
11
Avg merge
15m
Merged PRs (30d)
1

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from esa/mo-services-java

All issues in esa/mo-services-java

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.