1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 07:54:58 +00:00

Everywhere: Make ByteBuffer::{create_*,copy}() OOM-safe

This commit is contained in:
Ali Mohammad Pur 2021-09-06 03:29:52 +04:30 committed by Andreas Kling
parent 3a9f00c59b
commit 97e97bccab
105 changed files with 629 additions and 290 deletions

View file

@ -127,7 +127,12 @@ int main(int argc, char** argv)
}
for (;;) {
ByteBuffer ping_packet = ByteBuffer::create_zeroed(sizeof(struct icmphdr) + payload_size);
auto ping_packet_result = ByteBuffer::create_zeroed(sizeof(struct icmphdr) + payload_size);
if (!ping_packet_result.has_value()) {
warnln("failed to allocate a large enough buffer for the ping packet");
return 1;
}
auto& ping_packet = ping_packet_result.value();
struct icmphdr* ping_hdr = reinterpret_cast<struct icmphdr*>(ping_packet.data());
ping_hdr->type = ICMP_ECHO;
ping_hdr->code = 0;
@ -157,7 +162,12 @@ int main(int argc, char** argv)
for (;;) {
// FIXME: IPv4 headers are not actually fixed-size, handle other sizes.
ByteBuffer pong_packet = ByteBuffer::create_uninitialized(sizeof(struct ip) + sizeof(struct icmphdr) + payload_size);
auto pong_packet_result = ByteBuffer::create_uninitialized(sizeof(struct ip) + sizeof(struct icmphdr) + payload_size);
if (!pong_packet_result.has_value()) {
warnln("failed to allocate a large enough buffer for the pong packet");
return 1;
}
auto& pong_packet = pong_packet_result.value();
struct icmphdr* pong_hdr = reinterpret_cast<struct icmphdr*>(pong_packet.data() + sizeof(struct ip));
socklen_t peer_address_size = sizeof(peer_address);
rc = recvfrom(fd, pong_packet.data(), pong_packet.size(), 0, (struct sockaddr*)&peer_address, &peer_address_size);