From 7b777dc539e0416d69fa538601c3c224d1062cab Mon Sep 17 00:00:00 2001 From: AsiiaPine Date: Wed, 27 Aug 2025 12:30:57 +0300 Subject: [PATCH] fix getErrorCode --- platform_specific/socketcan/socketcan.c | 26 ++++++++++++------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/platform_specific/socketcan/socketcan.c b/platform_specific/socketcan/socketcan.c index 9298d9a..91f2cf1 100644 --- a/platform_specific/socketcan/socketcan.c +++ b/platform_specific/socketcan/socketcan.c @@ -19,27 +19,25 @@ #include #include #include +#include +#include +#include /// Returns the current errno as negated int16_t static int16_t getErrorCode() { - const int out = -abs(errno); - if (out < 0) - { - if (out >= INT16_MIN) - { - return (int16_t)out; - } - else - { - return INT16_MIN; - } - } - else + const int errno_val = errno; + if (errno_val == 0) { assert(false); // Requested an error when errno is zero? return INT16_MIN; } + + if (errno_val > INT16_MAX || errno_val < INT16_MIN) { + assert(false); // Requested an error when errno is zero? + return INT16_MIN; + } + return -(int16_t)errno_val; } int16_t socketcanInit(SocketCANInstance* out_ins, const char* can_iface_name) @@ -177,4 +175,4 @@ int16_t socketcanReceive(SocketCANInstance* ins, CanardCANFrame* out_frame, int3 int socketcanGetSocketFileDescriptor(const SocketCANInstance* ins) { return ins->fd; -} \ No newline at end of file +}