Skip to content

Commit

Permalink
Merge pull request #331 from taartspi/linuxfs_buff_overrun
Browse files Browse the repository at this point in the history
LinuxFile reused scratch buffers ensuring size was usable.  But the …
  • Loading branch information
eitch authored Mar 3, 2024
2 parents ca45f76 + 2d30fb0 commit 48f8fb0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,8 @@ protected synchronized IntBuffer getOffsetsBuffer(int size) {

if (byteSize > localBufferSize)
throw new ScratchBufferOverrun();

// if no buffer currently exists, new one always created.
// if buffer exists and limit is not equal to this getOffsetsBuffer, allocate new
if (buf == null) {
ByteBuffer bb = ByteBuffer.allocateDirect(localBufferSize);

Expand All @@ -223,8 +224,16 @@ protected synchronized IntBuffer getOffsetsBuffer(int size) {

buf = bb.asIntBuffer();
localOffsetsBuffer.set(buf);
}
} else if(buf.limit() != size) {
localOffsetsBuffer.remove();
ByteBuffer bb = ByteBuffer.allocateDirect(localBufferSize);

//keep native order, set before cast to IntBuffer
bb.order(ByteOrder.nativeOrder());

buf = bb.asIntBuffer();
localOffsetsBuffer.set(buf);
}
return buf;
}

Expand All @@ -238,6 +247,10 @@ protected synchronized ByteBuffer getDataBuffer(int size) {
if (buf == null) {
buf = ByteBuffer.allocateDirect(size);
localDataBuffer.set(buf);
}else if(buf.limit() != size){
localDataBuffer.remove();
buf = ByteBuffer.allocateDirect(size);
localDataBuffer.set(buf);
}

return buf;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,8 @@ public int readRegister(byte[] register, byte[] buffer, int offset, int length)
short writeLength = (short) register.length;
// two pointers will be used so 2 pairs of offset entries
IntBuffer offsets = IntBuffer.allocate(4);
// create ByteBuffer, load with write details
ByteBuffer ioctlData = ByteBuffer.allocate(500);
// create ByteBuffer, load with the write details Matches i2c provider
ByteBuffer ioctlData = ByteBuffer.allocate(2048);
// Ensures Pi BCM little_endian
ioctlData.order(ByteOrder.nativeOrder());
ioctlData.putShort(deviceAddr);
Expand Down

0 comments on commit 48f8fb0

Please sign in to comment.