Skip to content

Commit f722320

Browse files
adwsinghrhernandez35
authored andcommitted
Fix bounds check when non-exact bytebuffer is passed
1 parent de66491 commit f722320

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

codecs/cbor-codec/src/main/java/software/amazon/smithy/java/cbor/CborDeserializer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,11 @@ private Schema getMemberIfSame(Object o, byte[] bytes, int off, int len) {
108108
if (byteBuffer.hasArray()) {
109109
byte[] payload = byteBuffer.array();
110110
this.payload = payload;
111+
int start = byteBuffer.arrayOffset() + byteBuffer.position();
111112
this.parser = new CborParser(
112113
payload,
113-
byteBuffer.arrayOffset() + byteBuffer.position(),
114-
byteBuffer.remaining());
114+
start,
115+
start + byteBuffer.remaining());
115116
} else {
116117
int pos = byteBuffer.position();
117118
this.payload = new byte[byteBuffer.remaining()];

codecs/cbor-codec/src/main/java/software/amazon/smithy/java/cbor/CborParser.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public static int itemLength(int itemLength) {
149149
}
150150

151151
private final byte[] buffer;
152-
private final int len;
152+
private final int end;
153153
private int idx;
154154
private byte token;
155155

@@ -168,10 +168,10 @@ public CborParser(byte[] buffer) {
168168
this(buffer, 0, buffer.length);
169169
}
170170

171-
public CborParser(byte[] buffer, int off, int len) {
171+
public CborParser(byte[] buffer, int off, int end) {
172172
this.buffer = buffer;
173173
this.idx = off;
174-
this.len = len;
174+
this.end = end;
175175
}
176176

177177
/**
@@ -229,15 +229,15 @@ private byte nextToken0() {
229229
} else if ((state & 3) == 0) {
230230
// mask is 0b11: low bit is collection type (map == 0), high bit is 0 if the count is even
231231
int i = (idx += itemLength(itemLength) + overhead);
232-
if (i >= len) {
232+
if (i >= end) {
233233
throwIncompleteCollectionException();
234234
}
235235
return dispatchKey(buffer[i]);
236236
}
237237
}
238238

239239
int i = (idx += itemLength(itemLength) + overhead);
240-
if (i >= len) {
240+
if (i >= end) {
241241
return endOfBuffer(i);
242242
}
243243

@@ -260,7 +260,7 @@ private byte dispatchKey(byte b) {
260260
private byte endOfBuffer(int i) {
261261
itemLength = 0;
262262
overhead = 0;
263-
if (i > len) {
263+
if (i > end) {
264264
throw new BadCborException("unexpected end of payload");
265265
}
266266
if (inCollection) {
@@ -506,7 +506,7 @@ private void readIndefiniteLength(byte type) {
506506
itemLength = 0;
507507
int scan = ++idx;
508508
while (true) {
509-
if (scan >= len)
509+
if (scan >= end)
510510
throw new BadCborException("non-terminating string");
511511
byte b = buffer[scan];
512512
if (b == SIMPLE_STREAM_BREAK) {

0 commit comments

Comments
 (0)