Skip to content

Commit ed208f2

Browse files
committed
LinuxFile reused scratch buffers ensuring size was suaable. But the limit value cannot be modified so later usage failed as an intended overwrite
1 parent ca45f76 commit ed208f2

File tree

1 file changed

+17
-13
lines changed
  • libraries/pi4j-library-linuxfs/src/main/java/com/pi4j/library/linuxfs

1 file changed

+17
-13
lines changed

libraries/pi4j-library-linuxfs/src/main/java/com/pi4j/library/linuxfs/LinuxFile.java

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -211,34 +211,38 @@ protected static int getWordSize() {
211211
protected synchronized IntBuffer getOffsetsBuffer(int size) {
212212
final int byteSize = size * 4;
213213
IntBuffer buf = localOffsetsBuffer.get();
214-
214+
if (buf != null) {
215+
// always reallocate buffer to ensure correct limits
216+
localOffsetsBuffer.remove();
217+
buf = localOffsetsBuffer.get();
218+
}
215219
if (byteSize > localBufferSize)
216220
throw new ScratchBufferOverrun();
217221

218-
if (buf == null) {
219-
ByteBuffer bb = ByteBuffer.allocateDirect(localBufferSize);
222+
ByteBuffer bb = ByteBuffer.allocateDirect(localBufferSize);
220223

221-
//keep native order, set before cast to IntBuffer
222-
bb.order(ByteOrder.nativeOrder());
224+
//keep native order, set before cast to IntBuffer
225+
bb.order(ByteOrder.nativeOrder());
223226

224-
buf = bb.asIntBuffer();
225-
localOffsetsBuffer.set(buf);
226-
}
227+
buf = bb.asIntBuffer();
228+
localOffsetsBuffer.set(buf);
227229

228230
return buf;
229231
}
230232

231233

232234
protected synchronized ByteBuffer getDataBuffer(int size) {
233235
ByteBuffer buf = localDataBuffer.get();
234-
236+
if (buf != null) {
237+
// always reallocate buffer to ensure correct limits
238+
localDataBuffer.remove();
239+
buf = localDataBuffer.get();
240+
}
235241
if (size > localBufferSize)
236242
throw new ScratchBufferOverrun();
237243

238-
if (buf == null) {
239-
buf = ByteBuffer.allocateDirect(size);
240-
localDataBuffer.set(buf);
241-
}
244+
buf = ByteBuffer.allocateDirect(size);
245+
localDataBuffer.set(buf);
242246

243247
return buf;
244248
}

0 commit comments

Comments
 (0)