Skip to content

Commit

Permalink
recvfrom should set srcAddress for empty packets
Browse files Browse the repository at this point in the history
0 is a valid return value for recvfrom. It returns -1 on error, instead
of 0.

Test: libcore.io.OsTest
Bug: 33483694
Change-Id: I66261f7254bc5e4e5155a8a353018ed626259b82
  • Loading branch information
kongy committed Dec 12, 2016
1 parent 56d22f0 commit a6f1e8a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion luni/src/main/native/libcore_io_Posix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1718,7 +1718,7 @@ static jint Posix_recvfromBytes(JNIEnv* env, jobject, jobject javaFd, jobject ja
sockaddr* from = (javaInetSocketAddress != NULL) ? reinterpret_cast<sockaddr*>(&ss) : NULL;
socklen_t* fromLength = (javaInetSocketAddress != NULL) ? &sl : 0;
jint recvCount = NET_FAILURE_RETRY(env, ssize_t, recvfrom, javaFd, bytes.get() + byteOffset, byteCount, flags, from, fromLength);
if (recvCount > 0) {
if (recvCount >= 0) {
fillInetSocketAddress(env, javaInetSocketAddress, ss);
}
return recvCount;
Expand Down
19 changes: 19 additions & 0 deletions luni/src/test/java/libcore/io/OsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
Expand All @@ -43,6 +45,7 @@
import java.util.Locale;
import java.util.concurrent.atomic.AtomicReference;
import junit.framework.TestCase;

import static android.system.OsConstants.*;

public class OsTest extends TestCase {
Expand Down Expand Up @@ -762,4 +765,20 @@ public void test_readlink() throws Exception {
assertTrue("Could not delete symlink: " + path, new File(path).delete());
}
}

// Address should be correctly set for empty packets. http://b/33481605
public void test_recvfrom_EmptyPacket() throws Exception {
try (DatagramSocket ds = new DatagramSocket();
DatagramSocket srcSock = new DatagramSocket()) {
srcSock.send(new DatagramPacket(new byte[0], 0, ds.getLocalSocketAddress()));

byte[] recvBuf = new byte[16];
InetSocketAddress address = new InetSocketAddress();
int recvCount =
android.system.Os.recvfrom(ds.getFileDescriptor$(), recvBuf, 0, 16, 0, address);
assertEquals(0, recvCount);
assertTrue(address.getAddress().isLoopbackAddress());
assertEquals(srcSock.getLocalPort(), address.getPort());
}
}
}

0 comments on commit a6f1e8a

Please sign in to comment.