summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrian Gianforcaro <bgianf@serenityos.org>2021-05-13 01:34:04 -0700
committerAndreas Kling <kling@serenityos.org>2021-05-13 16:21:53 +0200
commit2e34714ba139a341f957b69c30a7ca106f460794 (patch)
tree16af2e0571588c8a30512da1e05f4569ed5bd4d4
parent858fff979a2836d1b92e57ea8a0a5b21d8336245 (diff)
downloadserenity-2e34714ba139a341f957b69c30a7ca106f460794.zip
Kernel: Make UDPSocket::create() API OOM safe
-rw-r--r--Kernel/Net/IPv4Socket.cpp8
-rw-r--r--Kernel/Net/UDPSocket.cpp7
-rw-r--r--Kernel/Net/UDPSocket.h3
3 files changed, 13 insertions, 5 deletions
diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp
index c8ebed1ae7..1520649c18 100644
--- a/Kernel/Net/IPv4Socket.cpp
+++ b/Kernel/Net/IPv4Socket.cpp
@@ -42,8 +42,12 @@ KResultOr<NonnullRefPtr<Socket>> IPv4Socket::create(int type, int protocol)
return tcp_socket.error();
return tcp_socket.release_value();
}
- if (type == SOCK_DGRAM)
- return UDPSocket::create(protocol);
+ if (type == SOCK_DGRAM) {
+ auto udp_socket = UDPSocket::create(protocol);
+ if (udp_socket.is_error())
+ return udp_socket.error();
+ return udp_socket.release_value();
+ }
if (type == SOCK_RAW) {
auto raw_socket = adopt_ref_if_nonnull(new IPv4Socket(type, protocol));
if (raw_socket)
diff --git a/Kernel/Net/UDPSocket.cpp b/Kernel/Net/UDPSocket.cpp
index ebe10199ea..36f95b400c 100644
--- a/Kernel/Net/UDPSocket.cpp
+++ b/Kernel/Net/UDPSocket.cpp
@@ -54,9 +54,12 @@ UDPSocket::~UDPSocket()
sockets_by_port().resource().remove(local_port());
}
-NonnullRefPtr<UDPSocket> UDPSocket::create(int protocol)
+KResultOr<NonnullRefPtr<UDPSocket>> UDPSocket::create(int protocol)
{
- return adopt_ref(*new UDPSocket(protocol));
+ auto socket = adopt_ref_if_nonnull(new UDPSocket(protocol));
+ if (socket)
+ return socket.release_nonnull();
+ return ENOMEM;
}
KResultOr<size_t> UDPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags)
diff --git a/Kernel/Net/UDPSocket.h b/Kernel/Net/UDPSocket.h
index 62bb42f368..dd4ce967bb 100644
--- a/Kernel/Net/UDPSocket.h
+++ b/Kernel/Net/UDPSocket.h
@@ -6,13 +6,14 @@
#pragma once
+#include <Kernel/KResult.h>
#include <Kernel/Net/IPv4Socket.h>
namespace Kernel {
class UDPSocket final : public IPv4Socket {
public:
- static NonnullRefPtr<UDPSocket> create(int protocol);
+ static KResultOr<NonnullRefPtr<UDPSocket>> create(int protocol);
virtual ~UDPSocket() override;
static SocketHandle<UDPSocket> from_port(u16);