Fix coverity issues (#20656)
* Check in case wth is null (CID 466304, 466311) Switch to ssize_t for total_read (CID 466312) * On purpose ignore return code (CID 466314) * wth cant be NULL (already dereferenced CID 466310)
Stelios Fragkakis committed
Jul 11, 2025 at 17:34 UTC
d6a4507ecec1d3c3156813fccded5de20dfd60c2
3 files changed
+11
-13
src/web/websocket/websocket-handshake.c
+4
-6
@@ -171,12 +171,10 @@ undo:
171
// Roll back the client count increment since assignment failed
172
wsc->wth = NULL;
173
174
- if(wth) {
175
- spinlock_lock(&wth->clients_spinlock);
176
- if (wth->clients_current > 0)
177
- wth->clients_current--;
178
- spinlock_unlock(&wth->clients_spinlock);
179
- }
174
+ spinlock_lock(&wth->clients_spinlock);
175
+ if (wth->clients_current > 0)
176
+ wth->clients_current--;
177
+ spinlock_unlock(&wth->clients_spinlock);
178
179
return NULL;
180
}
src/web/websocket/websocket-thread.c
+6
-6
@@ -108,7 +108,7 @@ struct pipe_header {
108
// Send command to a thread
109
bool websocket_thread_send_command(WEBSOCKET_THREAD *wth, uint8_t cmd, uint32_t id) {
110
if(!wth || wth->cmd.pipe[PIPE_WRITE] == -1) {
111
- netdata_log_error("WEBSOCKET[%zu]: Failed to send command - pipe is not initialized", wth->id);
111
+ netdata_log_error("WEBSOCKET[%zu]: Failed to send command - pipe is not initialized", wth ? wth->id : 0);
112
return false;
113
}
114
@@ -137,7 +137,7 @@ bool websocket_thread_send_command(WEBSOCKET_THREAD *wth, uint8_t cmd, uint32_t
137
138
bool websocket_thread_send_broadcast(WEBSOCKET_THREAD *wth, WEBSOCKET_OPCODE opcode, const char *message) {
139
if(!wth || wth->cmd.pipe[PIPE_WRITE] == -1) {
140
- netdata_log_error("WEBSOCKET[%zu]: Failed to send command - pipe is not initialized", wth->id);
140
+ netdata_log_error("WEBSOCKET[%zu]: Failed to send command - pipe is not initialized", wth ? wth->id : 0);
141
return false;
142
}
143
@@ -184,7 +184,7 @@ bool websocket_thread_send_broadcast(WEBSOCKET_THREAD *wth, WEBSOCKET_OPCODE opc
184
185
static ssize_t read_pipe_block(int fd, void *buffer, size_t size) {
186
char *buf = buffer;
187
- size_t total_read = 0;
187
+ ssize_t total_read = 0;
188
189
while (total_read < size) {
190
ssize_t bytes = read(fd, buf + total_read, size - total_read);
@@ -192,7 +192,7 @@ static ssize_t read_pipe_block(int fd, void *buffer, size_t size) {
192
if (bytes < 0) {
193
if (errno == EAGAIN || errno == EWOULDBLOCK) {
194
// Non-blocking case, return what we've read so far
195
- return (ssize_t)total_read;
195
+ return total_read;
196
}
197
198
// Real error occurred
@@ -200,12 +200,12 @@ static ssize_t read_pipe_block(int fd, void *buffer, size_t size) {
200
201
}
202
else if (bytes == 0)
203
- return (ssize_t)total_read;
203
+ return total_read;
204
205
total_read += bytes;
206
}
207
208
- return (ssize_t)total_read;
208
+ return total_read;
209
}
210
211
// Process a thread's command pipe
src/web/websocket/websocket.c
+1
-1
@@ -157,7 +157,7 @@ void websocket_client_free(WS_CLIENT *wsc) {
157
// We MUST make sure the socket is not in the poll before closing it
158
// otherwise kernel structures may be corrupted due to socket reuse
159
if(wsc->wth && wsc->wth->ndpl && wsc->sock.fd >= 0)
160
- nd_poll_del(wsc->wth->ndpl, wsc->sock.fd);
160
+ (void) nd_poll_del(wsc->wth->ndpl, wsc->sock.fd);
161
162
// Close socket using ND_SOCK abstraction
163
nd_sock_close(&wsc->sock);