From ad955fa8a488ba592f6426d714cad250a1bfd92f Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 9 Apr 2014 14:22:23 +0200 Subject: [PATCH] [#2371] Fix Potential data corruption in EpollSocketChannel when writing to the Channel Motivation: We sometimes see data corruption when writing to the EpollSocketChannel. Modifications: Correctly update the position of the ByteBuffer after something was written. Result: Fix data-corruption which could happen on partial writes --- .../src/main/c/io_netty_channel_epoll_Native.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c b/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c index 459aa251c4..6d7c407f74 100644 --- a/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c +++ b/transport-native-epoll/src/main/c/io_netty_channel_epoll_Native.c @@ -530,7 +530,14 @@ JNIEXPORT jint JNICALL Java_io_netty_channel_epoll_Native_write(JNIEnv * env, jc throwRuntimeException(env, "Unable to access address of buffer"); return -1; } - return write0(env, clazz, fd, buffer, pos, limit); + jint res = write0(env, clazz, fd, buffer, pos, limit); + if (res > 0) { + // Increment the pos of the ByteBuffer as it may be only partial written to prevent data-corruption later once we + // try to write the remaining data. + // See https://github.com/netty/netty/issues/2371 + incrementPosition(env, jbuffer, res); + } + return res; } JNIEXPORT jint JNICALL Java_io_netty_channel_epoll_Native_writeAddress(JNIEnv * env, jclass clazz, jint fd, jlong address, jint pos, jint limit) {