Skip to content

Commit faa8e07

Browse files
committed
handle buffer overflow and prevent java.nio.BufferOverflowException when message is too big
1 parent 1cdc9d3 commit faa8e07

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

src/main/java/org/fluentd/logger/sender/RawSocketSender.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ private synchronized boolean send(byte[] bytes) {
165165
LOG.error("Cannot send logs to " + server.toString());
166166
return false;
167167
}
168+
if (bytes.length > pendings.capacity()) {
169+
LOG.error("Log data larger {} than buffer size {}", bytes.length, pendings.capacity());
170+
return false;
171+
}
168172
}
169173
pendings.put(bytes);
170174

src/test/java/org/fluentd/logger/sender/TestRawSocketSender.java

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
import java.io.EOFException;
1313
import java.io.IOException;
1414
import java.net.Socket;
15-
import java.util.ArrayList;
16-
import java.util.HashMap;
17-
import java.util.List;
18-
import java.util.Map;
15+
import java.util.*;
1916
import java.util.concurrent.ConcurrentLinkedQueue;
2017
import java.util.concurrent.CountDownLatch;
2118
import java.util.concurrent.ExecutorService;
@@ -412,4 +409,54 @@ public void run() {
412409
assertEquals(0, bufferFull.getCount());
413410
assertEquals(i, elist.size());
414411
}
412+
413+
@Test
414+
public void testBufferOverflow() throws Exception {
415+
// start mock fluentd
416+
int port = MockFluentd.randomPort();
417+
MockFluentd fluentd = new MockFluentd(port, new MockFluentd.MockProcess() {
418+
public void process(MessagePack msgpack, Socket socket) throws IOException {
419+
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
420+
try {
421+
Unpacker unpacker = msgpack.createUnpacker(in);
422+
while (true) {
423+
unpacker.read(Event.class);
424+
}
425+
//socket.close();
426+
} catch (EOFException e) {
427+
// ignore
428+
}
429+
}
430+
});
431+
fluentd.start();
432+
433+
// start senders
434+
Sender sender = new RawSocketSender("localhost", port, 3000, 256);
435+
Map<String, Object> data = new HashMap<String, Object>();
436+
data.put("large", randomString(512));
437+
boolean success = sender.emit("tag.label1", data);
438+
assertFalse(success);
439+
440+
// close sender sockets
441+
sender.close();
442+
// close mock server sockets
443+
fluentd.close();
444+
}
445+
446+
private static final String CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
447+
448+
private String randomString(int len) {
449+
StringBuilder sb = new StringBuilder(len);
450+
451+
Random rnd = new Random();
452+
for (int i = 0; i < len; i++) {
453+
if (i != 0 && i % 128 == 0) {
454+
sb.append("\r\n");
455+
}
456+
457+
sb.append(CHARS.charAt(rnd.nextInt(CHARS.length())));
458+
}
459+
460+
return sb.toString();
461+
}
415462
}

0 commit comments

Comments
 (0)