From ed208f26cfd2a92978d010495fa1c6b5f8726e12 Mon Sep 17 00:00:00 2001 From: Tom Aarts Date: Thu, 29 Feb 2024 19:36:02 -0600 Subject: [PATCH] LinuxFile reused scratch buffers ensuring size was suaable. But the limit value cannot be modified so later usage failed as an intended overwrite --- .../com/pi4j/library/linuxfs/LinuxFile.java | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/libraries/pi4j-library-linuxfs/src/main/java/com/pi4j/library/linuxfs/LinuxFile.java b/libraries/pi4j-library-linuxfs/src/main/java/com/pi4j/library/linuxfs/LinuxFile.java index 7080f70b..a0f768fa 100644 --- a/libraries/pi4j-library-linuxfs/src/main/java/com/pi4j/library/linuxfs/LinuxFile.java +++ b/libraries/pi4j-library-linuxfs/src/main/java/com/pi4j/library/linuxfs/LinuxFile.java @@ -211,19 +211,21 @@ protected static int getWordSize() { protected synchronized IntBuffer getOffsetsBuffer(int size) { final int byteSize = size * 4; IntBuffer buf = localOffsetsBuffer.get(); - + if (buf != null) { + // always reallocate buffer to ensure correct limits + localOffsetsBuffer.remove(); + buf = localOffsetsBuffer.get(); + } if (byteSize > localBufferSize) throw new ScratchBufferOverrun(); - if (buf == null) { - ByteBuffer bb = ByteBuffer.allocateDirect(localBufferSize); + ByteBuffer bb = ByteBuffer.allocateDirect(localBufferSize); - //keep native order, set before cast to IntBuffer - bb.order(ByteOrder.nativeOrder()); + //keep native order, set before cast to IntBuffer + bb.order(ByteOrder.nativeOrder()); - buf = bb.asIntBuffer(); - localOffsetsBuffer.set(buf); - } + buf = bb.asIntBuffer(); + localOffsetsBuffer.set(buf); return buf; } @@ -231,14 +233,16 @@ protected synchronized IntBuffer getOffsetsBuffer(int size) { protected synchronized ByteBuffer getDataBuffer(int size) { ByteBuffer buf = localDataBuffer.get(); - + if (buf != null) { + // always reallocate buffer to ensure correct limits + localDataBuffer.remove(); + buf = localDataBuffer.get(); + } if (size > localBufferSize) throw new ScratchBufferOverrun(); - if (buf == null) { - buf = ByteBuffer.allocateDirect(size); - localDataBuffer.set(buf); - } + buf = ByteBuffer.allocateDirect(size); + localDataBuffer.set(buf); return buf; }