Skip to content

Commit 2db03ce

Browse files
jukkargalak
authored andcommitted
samples: net: socket: can: Do cleanup if failure
If bind() fails or TX thread cannot start, then cleanup the socket by calling close() Coverity-CID: 191003 Fixes #13824 Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
1 parent 4bbcf39 commit 2db03ce

File tree

1 file changed

+9
-3
lines changed
  • samples/net/sockets/can/src

1 file changed

+9
-3
lines changed

samples/net/sockets/can/src/main.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ static int setup_socket(void)
120120

121121
ret = bind(fd, (struct sockaddr *)&can_addr, sizeof(can_addr));
122122
if (ret < 0) {
123-
ret = errno;
123+
ret = -errno;
124124
LOG_ERR("Cannot bind CAN socket (%d)", -ret);
125-
return -ret;
125+
goto cleanup;
126126
}
127127

128128
setsockopt(fd, SOL_CAN_RAW, CAN_RAW_FILTER, &filter, sizeof(filter));
@@ -133,13 +133,19 @@ static int setup_socket(void)
133133
(k_thread_entry_t)tx, INT_TO_POINTER(fd),
134134
NULL, NULL, PRIORITY, 0, K_SECONDS(1));
135135
if (!tx_tid) {
136+
ret = -ENOENT;
137+
errno = -ret;
136138
LOG_ERR("Cannot create TX thread!");
137-
return -ENOENT;
139+
goto cleanup;
138140
}
139141

140142
LOG_DBG("Started socket CAN TX thread");
141143

142144
return fd;
145+
146+
cleanup:
147+
(void)close(fd);
148+
return ret;
143149
}
144150

145151
void main(void)

0 commit comments

Comments
 (0)