Skip to content

Commit 7661a1e

Browse files
authored
Merge pull request #53 from wsalembi/buffer_overflow
Handle buffer overflow errors
2 parents 1aac5bc + d55c4c3 commit 7661a1e

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
@@ -166,6 +166,10 @@ private synchronized boolean send(byte[] bytes) {
166166
if (!flushBuffer()) {
167167
return false;
168168
}
169+
if (bytes.length > pendings.remaining()) {
170+
LOG.error("Log data {} larger than remaining buffer size {}", bytes.length, pendings.remaining());
171+
return false;
172+
}
169173
}
170174
pendings.put(bytes);
171175

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;
@@ -418,4 +415,54 @@ public void run() {
418415
assertEquals(0, bufferFull.getCount());
419416
assertEquals(i, elist.size());
420417
}
418+
419+
@Test
420+
public void testBufferOverflow() throws Exception {
421+
// start mock fluentd
422+
int port = MockFluentd.randomPort();
423+
MockFluentd fluentd = new MockFluentd(port, new MockFluentd.MockProcess() {
424+
public void process(MessagePack msgpack, Socket socket) throws IOException {
425+
BufferedInputStream in = new BufferedInputStream(socket.getInputStream());
426+
try {
427+
Unpacker unpacker = msgpack.createUnpacker(in);
428+
while (true) {
429+
unpacker.read(Event.class);
430+
}
431+
//socket.close();
432+
} catch (EOFException e) {
433+
// ignore
434+
}
435+
}
436+
});
437+
fluentd.start();
438+
439+
// start senders
440+
Sender sender = new RawSocketSender("localhost", port, 3000, 256);
441+
Map<String, Object> data = new HashMap<String, Object>();
442+
data.put("large", randomString(512));
443+
boolean success = sender.emit("tag.label1", data);
444+
assertFalse(success);
445+
446+
// close sender sockets
447+
sender.close();
448+
// close mock server sockets
449+
fluentd.close();
450+
}
451+
452+
private static final String CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ ";
453+
454+
private String randomString(int len) {
455+
StringBuilder sb = new StringBuilder(len);
456+
457+
Random rnd = new Random();
458+
for (int i = 0; i < len; i++) {
459+
if (i != 0 && i % 128 == 0) {
460+
sb.append("\r\n");
461+
}
462+
463+
sb.append(CHARS.charAt(rnd.nextInt(CHARS.length())));
464+
}
465+
466+
return sb.toString();
467+
}
421468
}

0 commit comments

Comments
 (0)