Skip to content

Commit 45e4c0b

Browse files
committed
A bunch of bug fixes
1 parent 58e9c6c commit 45e4c0b

7 files changed

Lines changed: 45 additions & 11 deletions

File tree

include/sockpp/canbus_frame.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ class canbusfd_frame : public ::canfd_frame
233233
canbusfd_frame(canid_t canID, const void* data, size_t n) : base{} {
234234
this->can_id = canID;
235235
if (data && n != 0) {
236+
n = std::min(n, size_t(CANFD_MAX_DLEN));
236237
this->len = n;
237238
::memcpy(&this->data, data, n);
238239
}
@@ -241,7 +242,7 @@ class canbusfd_frame : public ::canfd_frame
241242
* Construct an FD frame from a classic CAN 2.0 frame.
242243
* @param frame A classic CAN 2.0 frame.
243244
*/
244-
canbusfd_frame(const canbus_frame& frame) {
245+
canbusfd_frame(const canbus_frame& frame) : base{} {
245246
// TODO: Should we reject RTR or error frames?
246247
std::memcpy(frame_ptr(), frame.frame_ptr(), sizeof(::can_frame));
247248
}

include/sockpp/canbus_socket.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,9 @@ class canbus_socket : public raw_socket
194194
* @return @em true if the filters were set, @em false otherwise.
195195
*/
196196
result<> set_filters(const can_filter* filters, size_t n) {
197-
return set_option(SOL_CAN_RAW, CAN_RAW_FILTER, filters, socklen_t(n));
197+
return set_option(
198+
SOL_CAN_RAW, CAN_RAW_FILTER, filters, socklen_t(n * sizeof(can_filter))
199+
);
198200
}
199201

200202
/**

src/connector.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ result<> connector::connect(const sock_address& addr, microseconds timeout) {
8585
is_non_blocking();
8686
#endif
8787

88-
if (!non_blocking)
89-
set_non_blocking(true);
88+
if (!non_blocking) {
89+
if (auto res = set_non_blocking(true); !res)
90+
return res;
91+
}
9092

9193
auto res = check_res(::connect(handle(), addr.sockaddr_ptr(), addr.size()));
9294

src/linux/canbus_frame.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ canbus_frame::canbus_frame(const canbusfd_frame& fdframe) {
5858
}
5959

6060
canbus_frame::canbus_frame(const canbusfd_frame& fdframe, error_code& ec) {
61-
if (fdframe.len > CAN_MAX_DLEN)
61+
if (fdframe.len > CAN_MAX_DLEN) {
6262
ec = make_error_code(errc::invalid_argument);
63+
return;
64+
}
6365
ec = error_code{};
6466
std::memcpy(frame_ptr(), fdframe.frame_ptr(), SZ);
6567
}

src/linux/canbus_socket.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,12 @@ result<canbus_frame> canbus_socket::recv(int flags /*=0*/) {
8585
return frame;
8686
}
8787

88+
result<canbusfd_frame> canbus_socket::recv_fd(int flags /*=0*/) {
89+
canbusfd_frame frame;
90+
if (auto res = recv(&frame, flags); !res)
91+
return res.error();
92+
return frame;
93+
}
94+
8895
/////////////////////////////////////////////////////////////////////////////
8996
} // namespace sockpp

src/stream_socket.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,14 @@ result<size_t> stream_socket::read_n(void* buf, size_t n) {
7979

8080
while (nx < n) {
8181
auto res = read(b + nx, n - nx);
82-
if (!res && res != errc::interrupted)
82+
if (!res) {
83+
if (res == errc::interrupted)
84+
continue;
8385
return res.error();
84-
85-
nx += size_t(res.value());
86+
}
87+
if (res.value() == 0)
88+
break; // EOF: peer closed connection
89+
nx += res.value();
8690
}
8791

8892
return nx;
@@ -144,9 +148,14 @@ result<size_t> stream_socket::write_n(const void* buf, size_t n) {
144148

145149
while (nx < n) {
146150
auto res = write(b + nx, n - nx);
147-
if (!res && res != errc::interrupted)
151+
if (!res) {
152+
if (res == errc::interrupted)
153+
continue;
148154
return res.error();
149-
nx += size_t(res.value());
155+
}
156+
if (res.value() == 0)
157+
break;
158+
nx += res.value();
150159
}
151160

152161
return nx;

tests/unit/test_canbus_frame.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,19 @@ static const string IFACE{"vcan0"};
5454

5555
// --------------------------------------------------------------------------
5656

57+
TEST_CASE("canbus_frame construction", "[can]") {
58+
SECTION("canbus_frame default constructor") {
59+
canbus_frame frame{};
60+
REQUIRE(frame.id_value() == 0);
61+
REQUIRE(!frame.has_extended_id());
62+
REQUIRE(!frame.is_remote());
63+
REQUIRE(!frame.is_error());
64+
REQUIRE(frame.len == 0);
65+
}
66+
}
67+
5768
TEST_CASE("canbus_frame conversions", "[can]") {
58-
SECTION("standard to FD frames") {
69+
SECTION("classic to FD frames") {
5970
canbus_frame frame{0x42, "hello"s};
6071

6172
canbusfd_frame fdframe{frame};

0 commit comments

Comments
 (0)