From e9047063ca10e276943be3ebe59d28d958178bf7 Mon Sep 17 00:00:00 2001
From: Robin <robin@chilio.net>
Date: Mon, 12 Nov 2018 12:50:05 +0100
Subject: [PATCH] gnrc_sock_[udp|ip]: store return value before release

sock_[udp|ip]_recv returns `pkt->size` after pkt was released via `gnrc_pktbuf_release(pkt)`.
This can result in wrong values returned by this functions and thus is not according to its sepecification.

Storing this values before releasing pkt returning the stored values should fix this.
---
 sys/net/gnrc/sock/ip/gnrc_sock_ip.c   | 3 ++-
 sys/net/gnrc/sock/udp/gnrc_sock_udp.c | 3 ++-
 2 files changed, 4 insertions(+), 2 deletions(-)

diff --git a/sys/net/gnrc/sock/ip/gnrc_sock_ip.c b/sys/net/gnrc/sock/ip/gnrc_sock_ip.c
index 6ba232480a..548e0d710f 100644
--- a/sys/net/gnrc/sock/ip/gnrc_sock_ip.c
+++ b/sys/net/gnrc/sock/ip/gnrc_sock_ip.c
@@ -120,8 +120,9 @@ ssize_t sock_ip_recv(sock_ip_t *sock, void *data, size_t max_len,
         return -EPROTO;
     }
     memcpy(data, pkt->data, pkt->size);
+    res = (int)pkt->size;
     gnrc_pktbuf_release(pkt);
-    return (int)pkt->size;
+    return res;
 }
 
 ssize_t sock_ip_send(sock_ip_t *sock, const void *data, size_t len,
diff --git a/sys/net/gnrc/sock/udp/gnrc_sock_udp.c b/sys/net/gnrc/sock/udp/gnrc_sock_udp.c
index 228aa78095..6268befe2c 100644
--- a/sys/net/gnrc/sock/udp/gnrc_sock_udp.c
+++ b/sys/net/gnrc/sock/udp/gnrc_sock_udp.c
@@ -215,8 +215,9 @@ ssize_t sock_udp_recv(sock_udp_t *sock, void *data, size_t max_len,
         return -EPROTO;
     }
     memcpy(data, pkt->data, pkt->size);
+    res = (int)pkt->size;
     gnrc_pktbuf_release(pkt);
-    return (int)pkt->size;
+    return res;
 }
 
 ssize_t sock_udp_send(sock_udp_t *sock, const void *data, size_t len,
-- 
GitLab