Skip to content

Commit

Permalink
Add test to ensure IndexOutOfBoundsException is thrown for structs wi…
Browse files Browse the repository at this point in the history
…th no STOP tag

Summary: This is a followup to D63777839, which ensures that malformed structs missing a STOP tag/byte don't end up in an infite loop. They now cause the serializer to throw when a malformed input is used with an InputStream.

Reviewed By: avalonalex

Differential Revision: D63800844

fbshipit-source-id: 31624306e0fdd8e3e5097cff8752f8d8070b8677
  • Loading branch information
j-bahr authored and facebook-github-bot committed Oct 3, 2024
1 parent e53d039 commit 316161e
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.facebook.thrift.util;

import com.facebook.thrift.test.EveryLayout;
import com.facebook.thrift.test.universalname.TestRequest;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -92,6 +94,24 @@ public void testToAndFromStreams() throws Exception {
Assert.assertEquals(this.everyLayout, everyLayout);
}

@Test(expected = IndexOutOfBoundsException.class)
public void testBadDataWithoutStopTagThrowsIndexOutOfBoundsException() {
ByteBuffer in =
SerializerUtil.toByteBuffer(
new TestRequest.Builder().setAString("abcd").build(), SerializationProtocol.TCompact);

// Remove Trailing byte tha would normally indicate STOP
byte[] slice = new byte[in.capacity() - 1];
in.get(slice, 0, in.capacity() - 1);

// Use ByteArrayInputStream to simulate use of input stream
TestRequest everyLayout =
SerializerUtil.fromInputStream(
TestRequest.asReader(),
new ByteArrayInputStream(slice),
SerializationProtocol.TCompact);
}

@Test
public void testToOutputStream() {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
Expand Down

0 comments on commit 316161e

Please sign in to comment.