aboutsummaryrefslogtreecommitdiffstats
path: root/WireGuardNetworkExtension
diff options
context:
space:
mode:
authorJeroen Leenarts <jeroen.leenarts@gmail.com>2018-08-05 15:58:52 +0200
committerJeroen Leenarts <jeroen.leenarts@gmail.com>2018-08-05 16:02:45 +0200
commitb6d8219244a9791a14605e94724bf62b24da15e9 (patch)
tree4d3bd7c3e0d436a2e7641595d24ab1f099b705d1 /WireGuardNetworkExtension
parentAdd default allowed IPs. (diff)
downloadwireguard-apple-b6d8219244a9791a14605e94724bf62b24da15e9.tar.xz
wireguard-apple-b6d8219244a9791a14605e94724bf62b24da15e9.zip
Clean up do_read.
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Diffstat (limited to 'WireGuardNetworkExtension')
-rw-r--r--WireGuardNetworkExtension/WireGuardGoWrapper.m13
1 files changed, 9 insertions, 4 deletions
diff --git a/WireGuardNetworkExtension/WireGuardGoWrapper.m b/WireGuardNetworkExtension/WireGuardGoWrapper.m
index 556827c..cd684bf 100644
--- a/WireGuardNetworkExtension/WireGuardGoWrapper.m
+++ b/WireGuardNetworkExtension/WireGuardGoWrapper.m
@@ -81,6 +81,8 @@ static void do_log(int level, const char *tag, const char *msg);
static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len)
{
WireGuardGoWrapper *wrapper = (__bridge WireGuardGoWrapper *)ctx;
+ if (wrapper.isClosed) return -1;
+
if (wrapper.packets.count == 0) {
[wrapper.packetFlow readPacketsWithCompletionHandler:^(NSArray<NSData *> * _Nonnull packets, NSArray<NSNumber *> * _Nonnull protocols) {
@@ -97,11 +99,14 @@ static ssize_t do_read(const void *ctx, const unsigned char *buf, size_t len)
[wrapper.packets removeObjectAtIndex:0];
[wrapper.protocols removeObjectAtIndex:0];
- len = [packet length];
- buf = (Byte*)malloc(len);
- memcpy(buf, [packet bytes], len);
+ NSUInteger packetLength = [packet length];
+ if (packetLength > len) {
+ // The packet will be dropped when we end up here.
+ return 0;
+ }
+ memcpy(buf, [packet bytes], packetLength);
- return wrapper.isClosed ? -1 : 0;
+ return packetLength;
}
static ssize_t do_write(const void *ctx, const unsigned char *buf, size_t len)