Skip to content

Commit 0822e99

Browse files
lePicimichalvasko
authored andcommitted
tests BUGFIX for np2_other_client.c based on Coverity scan
Based on Coverity scan.
1 parent d8dcb7b commit 0822e99

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

tests/np2_other_client.c

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#define _GNU_SOURCE
1818

19+
#include <assert.h>
1920
#include <errno.h>
2021
#include <fcntl.h>
2122
#include <inttypes.h>
@@ -68,6 +69,7 @@ oc_write(struct np_other_client *oc_sess, const char *msg, uint64_t msglen)
6869

6970
msglen = msglen ? msglen : strlen(msg);
7071
do {
72+
interrupted = 0;
7173
cnt = write(oc_sess->unixsock, msg + written, msglen - written);
7274
written += cnt;
7375
if ((cnt < 0) && (errno == EAGAIN)) {
@@ -123,22 +125,28 @@ oc_realloc(struct np_other_client *oc_sess)
123125
*
124126
* @param[in] oc_sess Client session.
125127
* @param[in] flags Option for function (@ref ocreadflags).
126-
* @return positive number representing number of characters written into @p oc_sess buffer.
128+
* @param[out] read_total Optional number of characters written into @p oc_sess buffer.
129+
* @return 0 on success.
127130
* @return negative number on error.
128131
*/
129-
static int64_t
130-
oc_read(struct np_other_client *oc_sess, uint32_t flags)
132+
static int
133+
oc_read(struct np_other_client *oc_sess, uint32_t flags, uint64_t *read_total)
131134
{
132-
int64_t rd, rdall = 0;
135+
int64_t rd;
136+
uint64_t rdall = 0;
133137
time_t tm;
134138
int interrupted;
135139
const char *endtag;
136140

137141
tm = time(NULL);
138142
oc_sess->buf[0] = 0;
143+
if (read_total) {
144+
*read_total = 0;
145+
}
139146

140147
do {
141148
interrupted = 0;
149+
assert(oc_sess->bufsize >= rdall);
142150
rd = read(oc_sess->unixsock, oc_sess->buf + rdall, oc_sess->bufsize - rdall);
143151
if (rd < 0) {
144152
if (errno == EAGAIN) {
@@ -147,7 +155,6 @@ oc_read(struct np_other_client *oc_sess, uint32_t flags)
147155
} else if (errno == EINTR) {
148156
rd = 0;
149157
interrupted = 1;
150-
break;
151158
} else {
152159
fprintf(stderr, "Reading from file descriptor (%d) failed (%s).\n", oc_sess->unixsock, strerror(errno));
153160
return -1;
@@ -194,7 +201,11 @@ oc_read(struct np_other_client *oc_sess, uint32_t flags)
194201
}
195202
} while (1);
196203

197-
return rdall;
204+
if (read_total) {
205+
*read_total = rdall;
206+
}
207+
208+
return 0;
198209
}
199210

200211
/**
@@ -218,7 +229,7 @@ oc_hello_handshake(struct np_other_client *oc_sess)
218229
return rc;
219230
}
220231

221-
return (oc_read(oc_sess, OC_READ_HELLO_MSG) >= 0) ? 0 : -1;
232+
return oc_read(oc_sess, OC_READ_HELLO_MSG, NULL);
222233
}
223234

224235
struct np_other_client *
@@ -231,13 +242,13 @@ oc_connect_unix(const char *address)
231242
oc_sess = calloc(1, sizeof *oc_sess);
232243
if (!oc_sess) {
233244
OC_FAIL_LOG;
234-
return NULL;
245+
goto error;
235246
}
236247

237248
oc_sess->unixsock = socket(AF_UNIX, SOCK_STREAM, 0);
238249
if (oc_sess->unixsock < 0) {
239250
OC_FAIL_LOG;
240-
return NULL;
251+
goto error;
241252
}
242253

243254
memset(&sun, 0, sizeof(sun));
@@ -246,28 +257,33 @@ oc_connect_unix(const char *address)
246257

247258
if (connect(oc_sess->unixsock, (struct sockaddr *)&sun, sizeof(sun)) < 0) {
248259
OC_FAIL_LOG;
249-
return NULL;
260+
goto error;
250261
}
251262

252263
if (fcntl(oc_sess->unixsock, F_SETFL, O_NONBLOCK) < 0) {
253264
OC_FAIL_LOG;
254-
return NULL;
265+
goto error;
255266
}
256267

257268
oc_sess->buf = malloc(2048);
258269
if (!oc_sess->buf) {
259-
return NULL;
270+
goto error;
260271
}
261272
oc_sess->bufsize = 2048;
262273

263274
rc = oc_hello_handshake(oc_sess);
264275
if (rc) {
265-
return NULL;
276+
goto error;
266277
}
267278

268279
oc_sess->msgid = 1;
269280

270281
return oc_sess;
282+
283+
error:
284+
oc_session_free(oc_sess);
285+
286+
return NULL;
271287
}
272288

273289
int
@@ -277,6 +293,8 @@ oc_send_msg(struct np_other_client *oc_sess, const char *msg)
277293
char *starttag = NULL;
278294
uint64_t msglen;
279295

296+
assert(oc_sess && msg);
297+
280298
/* increment message-id but do not increment after initial handshake */
281299
oc_sess->msgid = (oc_sess->msgid != 1) ? oc_sess->msgid + 1 : oc_sess->msgid;
282300

@@ -285,7 +303,6 @@ oc_send_msg(struct np_other_client *oc_sess, const char *msg)
285303
if (!starttag) {
286304
OC_FAIL_LOG;
287305
return -1;
288-
goto cleanup;
289306
}
290307

291308
rc = oc_write(oc_sess, starttag, 0);
@@ -312,18 +329,16 @@ oc_send_msg(struct np_other_client *oc_sess, const char *msg)
312329
int
313330
oc_recv_msg(struct np_other_client *oc_sess, char **msg)
314331
{
315-
int64_t len;
332+
int rc;
333+
uint64_t len;
316334
char *endtag;
317335

318-
len = oc_read(oc_sess, 0);
336+
assert(oc_sess && msg);
319337

320-
if (len < 0) {
338+
*msg = "";
339+
rc = oc_read(oc_sess, 0, &len);
340+
if (rc) {
321341
return -1;
322-
} else if (len == (int64_t)oc_sess->bufsize) {
323-
/* unlikely, though no space for zero character */
324-
if (oc_realloc(oc_sess)) {
325-
return -1;
326-
}
327342
}
328343

329344
/* Delete end tag: \n##\n */

0 commit comments

Comments
 (0)