1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 14:47:34 +00:00

Kernel: Make UDPSocket::create() API OOM safe

This commit is contained in:
Brian Gianforcaro 2021-05-13 01:34:04 -07:00 committed by Andreas Kling
parent 858fff979a
commit 2e34714ba1
3 changed files with 13 additions and 5 deletions

View file

@ -42,8 +42,12 @@ KResultOr<NonnullRefPtr<Socket>> IPv4Socket::create(int type, int protocol)
return tcp_socket.error(); return tcp_socket.error();
return tcp_socket.release_value(); return tcp_socket.release_value();
} }
if (type == SOCK_DGRAM) if (type == SOCK_DGRAM) {
return UDPSocket::create(protocol); auto udp_socket = UDPSocket::create(protocol);
if (udp_socket.is_error())
return udp_socket.error();
return udp_socket.release_value();
}
if (type == SOCK_RAW) { if (type == SOCK_RAW) {
auto raw_socket = adopt_ref_if_nonnull(new IPv4Socket(type, protocol)); auto raw_socket = adopt_ref_if_nonnull(new IPv4Socket(type, protocol));
if (raw_socket) if (raw_socket)

View file

@ -54,9 +54,12 @@ UDPSocket::~UDPSocket()
sockets_by_port().resource().remove(local_port()); 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) KResultOr<size_t> UDPSocket::protocol_receive(ReadonlyBytes raw_ipv4_packet, UserOrKernelBuffer& buffer, size_t buffer_size, [[maybe_unused]] int flags)

View file

@ -6,13 +6,14 @@
#pragma once #pragma once
#include <Kernel/KResult.h>
#include <Kernel/Net/IPv4Socket.h> #include <Kernel/Net/IPv4Socket.h>
namespace Kernel { namespace Kernel {
class UDPSocket final : public IPv4Socket { class UDPSocket final : public IPv4Socket {
public: public:
static NonnullRefPtr<UDPSocket> create(int protocol); static KResultOr<NonnullRefPtr<UDPSocket>> create(int protocol);
virtual ~UDPSocket() override; virtual ~UDPSocket() override;
static SocketHandle<UDPSocket> from_port(u16); static SocketHandle<UDPSocket> from_port(u16);