refactor: add some misuse checks to the C API

This commit is contained in:
CJ van den Berg 2026-03-04 20:19:59 +01:00
parent 703e5d5be1
commit cf6685b34b
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
3 changed files with 34 additions and 0 deletions

View file

@ -2,6 +2,12 @@
#include <thespian/c/socket.h> #include <thespian/c/socket.h>
#include <thespian/socket.hpp> #include <thespian/socket.hpp>
#include <cassert>
#define ASSERT_HANDLE(h, fn) \
assert((h) != nullptr && "null handle passed to " fn ": was it created outside an actor?")
using socket_impl = thespian::socket_impl; using socket_impl = thespian::socket_impl;
extern "C" { extern "C" {
@ -22,6 +28,7 @@ auto thespian_socket_create(const char *tag, int fd)
auto thespian_socket_write(struct thespian_socket_handle *handle, auto thespian_socket_write(struct thespian_socket_handle *handle,
const char *data, size_t len) -> int { const char *data, size_t len) -> int {
ASSERT_HANDLE(handle, "thespian_socket_write");
try { try {
thespian::socket_write(reinterpret_cast<socket_impl *>(handle), // NOLINT thespian::socket_write(reinterpret_cast<socket_impl *>(handle), // NOLINT
std::string_view(data, len)); std::string_view(data, len));
@ -37,6 +44,7 @@ auto thespian_socket_write(struct thespian_socket_handle *handle,
auto thespian_socket_write_binary(struct thespian_socket_handle *handle, auto thespian_socket_write_binary(struct thespian_socket_handle *handle,
const uint8_t *data, size_t len) -> int { const uint8_t *data, size_t len) -> int {
ASSERT_HANDLE(handle, "thespian_socket_write_binary");
try { try {
std::vector<uint8_t> buf(data, data + len); // NOLINT std::vector<uint8_t> buf(data, data + len); // NOLINT
thespian::socket_write_binary( thespian::socket_write_binary(
@ -53,6 +61,7 @@ auto thespian_socket_write_binary(struct thespian_socket_handle *handle,
} }
auto thespian_socket_read(struct thespian_socket_handle *handle) -> int { auto thespian_socket_read(struct thespian_socket_handle *handle) -> int {
ASSERT_HANDLE(handle, "thespian_socket_read");
try { try {
thespian::socket_read(reinterpret_cast<socket_impl *>(handle)); // NOLINT thespian::socket_read(reinterpret_cast<socket_impl *>(handle)); // NOLINT
return 0; return 0;
@ -66,6 +75,7 @@ auto thespian_socket_read(struct thespian_socket_handle *handle) -> int {
} }
auto thespian_socket_close(struct thespian_socket_handle *handle) -> int { auto thespian_socket_close(struct thespian_socket_handle *handle) -> int {
ASSERT_HANDLE(handle, "thespian_socket_close");
try { try {
thespian::socket_close(reinterpret_cast<socket_impl *>(handle)); // NOLINT thespian::socket_close(reinterpret_cast<socket_impl *>(handle)); // NOLINT
return 0; return 0;
@ -79,6 +89,7 @@ auto thespian_socket_close(struct thespian_socket_handle *handle) -> int {
} }
void thespian_socket_destroy(struct thespian_socket_handle *handle) { void thespian_socket_destroy(struct thespian_socket_handle *handle) {
ASSERT_HANDLE(handle, "thespian_socket_destroy");
try { try {
thespian::destroy_socket(reinterpret_cast<socket_impl *>(handle)); // NOLINT thespian::destroy_socket(reinterpret_cast<socket_impl *>(handle)); // NOLINT
} catch (const std::exception &e) { } catch (const std::exception &e) {

View file

@ -2,6 +2,11 @@
#include <thespian/c/tcp.h> #include <thespian/c/tcp.h>
#include <thespian/tcp.hpp> #include <thespian/tcp.hpp>
#include <cassert>
#define ASSERT_HANDLE(h, fn) \
assert((h) != nullptr && "null handle passed to " fn ": was it created outside an actor?")
using ::port_t; using ::port_t;
using thespian::tcp::acceptor_impl; using thespian::tcp::acceptor_impl;
using thespian::tcp::connector_impl; using thespian::tcp::connector_impl;
@ -24,6 +29,7 @@ auto thespian_tcp_acceptor_create(const char *tag)
auto thespian_tcp_acceptor_listen(struct thespian_tcp_acceptor_handle *handle, auto thespian_tcp_acceptor_listen(struct thespian_tcp_acceptor_handle *handle,
in6_addr ip, uint16_t port) -> uint16_t { in6_addr ip, uint16_t port) -> uint16_t {
ASSERT_HANDLE(handle, "thespian_tcp_acceptor_listen");
try { try {
port_t p = thespian::tcp::acceptor_listen( port_t p = thespian::tcp::acceptor_listen(
reinterpret_cast<acceptor_impl *>(handle), ip, port); // NOLINT reinterpret_cast<acceptor_impl *>(handle), ip, port); // NOLINT
@ -39,6 +45,7 @@ auto thespian_tcp_acceptor_listen(struct thespian_tcp_acceptor_handle *handle,
auto thespian_tcp_acceptor_close(struct thespian_tcp_acceptor_handle *handle) auto thespian_tcp_acceptor_close(struct thespian_tcp_acceptor_handle *handle)
-> int { -> int {
ASSERT_HANDLE(handle, "thespian_tcp_acceptor_close");
try { try {
thespian::tcp::acceptor_close( thespian::tcp::acceptor_close(
reinterpret_cast<acceptor_impl *>(handle)); // NOLINT reinterpret_cast<acceptor_impl *>(handle)); // NOLINT
@ -54,6 +61,7 @@ auto thespian_tcp_acceptor_close(struct thespian_tcp_acceptor_handle *handle)
void thespian_tcp_acceptor_destroy( void thespian_tcp_acceptor_destroy(
struct thespian_tcp_acceptor_handle *handle) { struct thespian_tcp_acceptor_handle *handle) {
ASSERT_HANDLE(handle, "thespian_tcp_acceptor_destroy");
try { try {
thespian::tcp::destroy_acceptor( thespian::tcp::destroy_acceptor(
reinterpret_cast<acceptor_impl *>(handle)); // NOLINT reinterpret_cast<acceptor_impl *>(handle)); // NOLINT
@ -82,6 +90,7 @@ auto thespian_tcp_connector_create(const char *tag)
auto thespian_tcp_connector_connect( auto thespian_tcp_connector_connect(
struct thespian_tcp_connector_handle *handle, in6_addr ip, uint16_t port) struct thespian_tcp_connector_handle *handle, in6_addr ip, uint16_t port)
-> int { -> int {
ASSERT_HANDLE(handle, "thespian_tcp_connector_connect");
try { try {
thespian::tcp::connector_connect( thespian::tcp::connector_connect(
reinterpret_cast<connector_impl *>(handle), // NOLINT reinterpret_cast<connector_impl *>(handle), // NOLINT
@ -98,6 +107,7 @@ auto thespian_tcp_connector_connect(
auto thespian_tcp_connector_cancel(struct thespian_tcp_connector_handle *handle) auto thespian_tcp_connector_cancel(struct thespian_tcp_connector_handle *handle)
-> int { -> int {
ASSERT_HANDLE(handle, "thespian_tcp_connector_cancel");
try { try {
thespian::tcp::connector_cancel( thespian::tcp::connector_cancel(
reinterpret_cast<connector_impl *>(handle)); // NOLINT reinterpret_cast<connector_impl *>(handle)); // NOLINT
@ -113,6 +123,7 @@ auto thespian_tcp_connector_cancel(struct thespian_tcp_connector_handle *handle)
void thespian_tcp_connector_destroy( void thespian_tcp_connector_destroy(
struct thespian_tcp_connector_handle *handle) { struct thespian_tcp_connector_handle *handle) {
ASSERT_HANDLE(handle, "thespian_tcp_connector_destroy");
try { try {
thespian::tcp::destroy_connector( thespian::tcp::destroy_connector(
reinterpret_cast<connector_impl *>(handle)); // NOLINT reinterpret_cast<connector_impl *>(handle)); // NOLINT

View file

@ -2,6 +2,12 @@
#include <thespian/c/unx.h> #include <thespian/c/unx.h>
#include <thespian/unx.hpp> #include <thespian/unx.hpp>
#include <cassert>
#define ASSERT_HANDLE(h, fn) \
assert((h) != nullptr && "null handle passed to " fn ": was it created outside an actor?")
using thespian::unx::acceptor_impl; using thespian::unx::acceptor_impl;
using thespian::unx::connector_impl; using thespian::unx::connector_impl;
using thespian::unx::mode; using thespian::unx::mode;
@ -31,6 +37,7 @@ auto thespian_unx_acceptor_create(const char *tag)
auto thespian_unx_acceptor_listen(struct thespian_unx_acceptor_handle *handle, auto thespian_unx_acceptor_listen(struct thespian_unx_acceptor_handle *handle,
const char *path, thespian_unx_mode m) const char *path, thespian_unx_mode m)
-> int { -> int {
ASSERT_HANDLE(handle, "thespian_unx_acceptor_listen");
try { try {
thespian::unx::acceptor_listen( thespian::unx::acceptor_listen(
reinterpret_cast<acceptor_impl *>(handle), // NOLINT reinterpret_cast<acceptor_impl *>(handle), // NOLINT
@ -47,6 +54,7 @@ auto thespian_unx_acceptor_listen(struct thespian_unx_acceptor_handle *handle,
auto thespian_unx_acceptor_close(struct thespian_unx_acceptor_handle *handle) auto thespian_unx_acceptor_close(struct thespian_unx_acceptor_handle *handle)
-> int { -> int {
ASSERT_HANDLE(handle, "thespian_unx_acceptor_close");
try { try {
thespian::unx::acceptor_close( thespian::unx::acceptor_close(
reinterpret_cast<acceptor_impl *>(handle)); // NOLINT reinterpret_cast<acceptor_impl *>(handle)); // NOLINT
@ -62,6 +70,7 @@ auto thespian_unx_acceptor_close(struct thespian_unx_acceptor_handle *handle)
void thespian_unx_acceptor_destroy( void thespian_unx_acceptor_destroy(
struct thespian_unx_acceptor_handle *handle) { struct thespian_unx_acceptor_handle *handle) {
ASSERT_HANDLE(handle, "thespian_unx_acceptor_destroy");
try { try {
thespian::unx::destroy_acceptor( thespian::unx::destroy_acceptor(
reinterpret_cast<acceptor_impl *>(handle)); // NOLINT reinterpret_cast<acceptor_impl *>(handle)); // NOLINT
@ -90,6 +99,7 @@ auto thespian_unx_connector_create(const char *tag)
auto thespian_unx_connector_connect( auto thespian_unx_connector_connect(
struct thespian_unx_connector_handle *handle, const char *path, struct thespian_unx_connector_handle *handle, const char *path,
thespian_unx_mode m) -> int { thespian_unx_mode m) -> int {
ASSERT_HANDLE(handle, "thespian_unx_connector_connect");
try { try {
thespian::unx::connector_connect( thespian::unx::connector_connect(
reinterpret_cast<connector_impl *>(handle), // NOLINT reinterpret_cast<connector_impl *>(handle), // NOLINT
@ -106,6 +116,7 @@ auto thespian_unx_connector_connect(
auto thespian_unx_connector_cancel(struct thespian_unx_connector_handle *handle) auto thespian_unx_connector_cancel(struct thespian_unx_connector_handle *handle)
-> int { -> int {
ASSERT_HANDLE(handle, "thespian_unx_connector_cancel");
try { try {
thespian::unx::connector_cancel( thespian::unx::connector_cancel(
reinterpret_cast<connector_impl *>(handle)); // NOLINT reinterpret_cast<connector_impl *>(handle)); // NOLINT
@ -121,6 +132,7 @@ auto thespian_unx_connector_cancel(struct thespian_unx_connector_handle *handle)
void thespian_unx_connector_destroy( void thespian_unx_connector_destroy(
struct thespian_unx_connector_handle *handle) { struct thespian_unx_connector_handle *handle) {
ASSERT_HANDLE(handle, "thespian_unx_connector_destroy");
try { try {
thespian::unx::destroy_connector( thespian::unx::destroy_connector(
reinterpret_cast<connector_impl *>(handle)); // NOLINT reinterpret_cast<connector_impl *>(handle)); // NOLINT