From 6f7d2cad9df4cb4fb7a8be2f0a2849ffcdf9c213 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?V=C3=ADt=C4=9Bzslav=20K=C5=99=C3=AD=C5=BE?=
 <vitezslav.kriz@nic.cz>
Date: Tue, 18 Aug 2015 16:37:33 +0200
Subject: [PATCH] afl: handle empty input

---
 tests-fuzz/wrap/udp-handler.c | 20 +++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)

diff --git a/tests-fuzz/wrap/udp-handler.c b/tests-fuzz/wrap/udp-handler.c
index 62287acaf8..cc62f13450 100644
--- a/tests-fuzz/wrap/udp-handler.c
+++ b/tests-fuzz/wrap/udp-handler.c
@@ -29,6 +29,15 @@ struct udp_stdin {
 	struct sockaddr_storage addr;
 };
 
+static void next(void)
+{
+	if (getenv("AFL_PERSISTENT")) {
+		raise(SIGSTOP);
+	} else {
+		exit(0);
+	}
+}
+
 static void *udp_stdin_init(void)
 {
 	struct udp_stdin *rq = malloc(sizeof(struct udp_stdin));
@@ -56,6 +65,11 @@ static int udp_stdin_recv(int fd, void *d)
 	struct udp_stdin *rq = (struct udp_stdin *) d;
 	rq->iov[RX].iov_len = fread(rq->iov[RX].iov_base,
 	                            1, KNOT_WIRE_MAX_PKTSIZE, stdin);
+
+	if (rq->iov[RX].iov_len == 0) {
+		next();
+	}
+
 	return rq->iov[RX].iov_len;
 }
 
@@ -69,11 +83,7 @@ static int udp_stdin_handle(udp_context_t *ctx, void *d)
 static int udp_stdin_send(void *d)
 {
 	struct udp_stdin *rq = (struct udp_stdin *) d;
-	if (getenv("AFL_PERSISTENT")) {
-		raise(SIGSTOP);
-	} else {
-		exit(0);
-	}
+	next();
 	return 0;
 }
 
-- 
GitLab