close socket on error

This commit is contained in:
Theis Pieter Hollebeek 2024-12-12 11:35:26 +01:00
parent ef528ccb3f
commit d67b55e570

View File

@ -124,12 +124,14 @@ public:
auto err
= ::bind(socket, (struct sockaddr*)&address, sizeof(address));
if (err < 0) {
close(socket);
return Err { .msg = std::format("could not bind ({})", err) };
};
}
{
auto err = ::listen(socket, 0);
if (err < 0) {
close(socket);
return Err { .msg = std::format("could not listen ({})", err) };
}
}
@ -140,6 +142,7 @@ public:
int client = ::accept(
socket, (struct sockaddr*)&client_address, &address_size);
if (client < 0) {
close(socket);
return Err { .msg
= std::format("could not accept ({})", client) };
}
@ -149,10 +152,7 @@ public:
std::string message = {};
while (true) {
ssize_t bytes_read = read(client, buffer, buf_len);
if (bytes_read < 0) {
return Err { .msg
= std::format("could not read ({})", bytes_read) };
} else if (bytes_read == 0) {
if (bytes_read <= 0) {
break;
}
for (size_t i = 0; i < (size_t)bytes_read; ++i) {
@ -168,6 +168,7 @@ public:
"error parsing rpc message: '{}' @ {}:{}\n",
err.msg, err.pos.line, err.pos.col);
close(client);
close(socket);
return Err {
.msg = msg,
};