Skip to content

Commit 3a87c57

Browse files
Merge pull request #820 from ejohnstown/cov
Fixing a batch of issues reported by Coverity
2 parents 8c0c7fd + 903bbc7 commit 3a87c57

6 files changed

Lines changed: 100 additions & 78 deletions

File tree

apps/wolfssh/common.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static int load_der_file(const char* filename, byte** out, word32* outSz)
7676
{
7777
WFILE* file;
7878
byte* in;
79-
word32 inSz;
79+
long inSz;
8080
int ret;
8181

8282
if (filename == NULL || out == NULL || outSz == NULL)
@@ -90,10 +90,10 @@ static int load_der_file(const char* filename, byte** out, word32* outSz)
9090
WFCLOSE(NULL, file);
9191
return -1;
9292
}
93-
inSz = (word32)WFTELL(NULL, file);
93+
inSz = WFTELL(NULL, file);
9494
WREWIND(NULL, file);
9595

96-
if (inSz == 0) {
96+
if (inSz <= 0) {
9797
WFCLOSE(NULL, file);
9898
return -1;
9999
}
@@ -105,7 +105,7 @@ static int load_der_file(const char* filename, byte** out, word32* outSz)
105105
}
106106

107107
ret = (int)WFREAD(NULL, in, 1, inSz, file);
108-
if (ret <= 0 || (word32)ret != inSz) {
108+
if (ret <= 0 || ret != inSz) {
109109
ret = -1;
110110
WFREE(in, NULL, 0);
111111
in = 0;
@@ -115,7 +115,7 @@ static int load_der_file(const char* filename, byte** out, word32* outSz)
115115
ret = 0;
116116

117117
*out = in;
118-
*outSz = inSz;
118+
*outSz = (word32)inSz;
119119

120120
WFCLOSE(NULL, file);
121121

apps/wolfsshd/wolfsshd.c

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ static byte* getBufferFromFile(const char* fileName, word32* bufSz, void* heap)
242242
{
243243
FILE* file;
244244
byte* buf = NULL;
245-
word32 fileSz;
245+
long fileSz;
246246
word32 readSz;
247247

248248
WOLFSSH_UNUSED(heap);
@@ -252,13 +252,17 @@ static byte* getBufferFromFile(const char* fileName, word32* bufSz, void* heap)
252252
if (WFOPEN(NULL, &file, fileName, "rb") != 0)
253253
return NULL;
254254
WFSEEK(NULL, file, 0, WSEEK_END);
255-
fileSz = (word32)WFTELL(NULL, file);
255+
fileSz = WFTELL(NULL, file);
256+
if (fileSz < 0) {
257+
WFCLOSE(NULL, file);
258+
return NULL;
259+
}
256260
WREWIND(NULL, file);
257261

258262
buf = (byte*)WMALLOC(fileSz + 1, heap, DYNTYPE_SSHD);
259263
if (buf != NULL) {
260264
readSz = (word32)WFREAD(NULL, buf, 1, fileSz, file);
261-
if (readSz < fileSz) {
265+
if (readSz < (size_t)fileSz) {
262266
WFCLOSE(NULL, file);
263267
WFREE(buf, heap, DYNTYPE_SSHD);
264268
return NULL;
@@ -1347,20 +1351,19 @@ static int SHELL_Subsystem(WOLFSSHD_CONNECTION* conn, WOLFSSH* ssh,
13471351
setenv("LOGNAME", pPasswd->pw_name, 1);
13481352
setenv("SHELL", pPasswd->pw_shell, 1);
13491353

1350-
if (pPasswd->pw_shell) {
1351-
if (WSTRLEN(pPasswd->pw_shell) < sizeof(shell)) {
1352-
char* cursor;
1353-
char* start;
1354+
if (WSTRLEN(pPasswd->pw_shell) < sizeof(shell)) {
1355+
char* cursor;
1356+
char* start;
13541357

1355-
WSTRNCPY(shell, pPasswd->pw_shell, sizeof(shell));
1356-
cursor = shell;
1357-
do {
1358-
start = cursor;
1359-
*cursor = '-';
1360-
cursor = WSTRCHR(start, '/');
1361-
} while (cursor && *cursor != '\0');
1362-
args[0] = start;
1363-
}
1358+
WSTRNCPY(shell, pPasswd->pw_shell, sizeof(shell)-1);
1359+
shell[sizeof(shell)-1] = 0;
1360+
cursor = shell;
1361+
do {
1362+
start = cursor;
1363+
*cursor = '-';
1364+
cursor = WSTRCHR(start, '/');
1365+
} while (cursor && *cursor != '\0');
1366+
args[0] = start;
13641367
}
13651368

13661369
rc = chdir(pPasswd->pw_dir);

examples/client/common.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ static int load_der_file(const char* filename, byte** out, word32* outSz,
262262
{
263263
WFILE* file;
264264
byte* in;
265-
word32 inSz;
265+
long inSz;
266266
int ret;
267267

268268
if (filename == NULL || out == NULL || outSz == NULL)
@@ -276,13 +276,12 @@ static int load_der_file(const char* filename, byte** out, word32* outSz,
276276
WFCLOSE(NULL, file);
277277
return -1;
278278
}
279-
inSz = (word32)WFTELL(NULL, file);
280-
WREWIND(NULL, file);
281-
282-
if (inSz == 0) {
279+
inSz = WFTELL(NULL, file);
280+
if (inSz <= 0) {
283281
WFCLOSE(NULL, file);
284282
return -1;
285283
}
284+
WREWIND(NULL, file);
286285

287286
in = (byte*)WMALLOC(inSz, heap, 0);
288287
if (in == NULL) {
@@ -291,7 +290,7 @@ static int load_der_file(const char* filename, byte** out, word32* outSz,
291290
}
292291

293292
ret = (int)WFREAD(NULL, in, 1, inSz, file);
294-
if (ret <= 0 || (word32)ret != inSz) {
293+
if (ret <= 0 || ret != inSz) {
295294
ret = -1;
296295
WFREE(in, heap, 0);
297296
in = 0;
@@ -301,7 +300,7 @@ static int load_der_file(const char* filename, byte** out, word32* outSz,
301300
ret = 0;
302301

303302
*out = in;
304-
*outSz = inSz;
303+
*outSz = (word32)inSz;
305304

306305
WFCLOSE(NULL, file);
307306

examples/sftpclient/sftpclient.c

Lines changed: 43 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,15 @@ static void sig_handler(const int sig)
249249
static void clean_path(char* path)
250250
{
251251
int i;
252-
long sz = (long)WSTRLEN(path);
252+
long sz;
253253
byte found;
254254

255+
if (path == NULL) {
256+
return;
257+
}
258+
259+
sz = (long)WSTRLEN(path);
260+
255261
/* remove any double '/' chars */
256262
for (i = 0; i < sz; i++) {
257263
if (path[i] == '/' && path[i+1] == '/') {
@@ -272,51 +278,49 @@ static void clean_path(char* path)
272278
}
273279
}
274280

275-
if (path != NULL) {
276-
/* go through path until no cases are found */
277-
do {
278-
int prIdx = 0; /* begin of cut */
279-
int enIdx = 0; /* end of cut */
280-
sz = (long)WSTRLEN(path);
281-
282-
found = 0;
283-
for (i = 0; i < sz; i++) {
284-
if (path[i] == '/') {
285-
int z;
286-
287-
/* if next two chars are .. then delete */
288-
if (path[i+1] == '.' && path[i+2] == '.') {
289-
enIdx = i + 3;
290-
291-
/* start at one char before / and retrace path */
292-
for (z = i - 1; z > 0; z--) {
293-
if (path[z] == '/') {
294-
prIdx = z;
295-
break;
296-
}
281+
/* go through path until no cases are found */
282+
do {
283+
int prIdx = 0; /* begin of cut */
284+
int enIdx = 0; /* end of cut */
285+
sz = (long)WSTRLEN(path);
286+
287+
found = 0;
288+
for (i = 0; i < sz; i++) {
289+
if (path[i] == '/') {
290+
int z;
291+
292+
/* if next two chars are .. then delete */
293+
if (path[i+1] == '.' && path[i+2] == '.') {
294+
enIdx = i + 3;
295+
296+
/* start at one char before / and retrace path */
297+
for (z = i - 1; z > 0; z--) {
298+
if (path[z] == '/') {
299+
prIdx = z;
300+
break;
297301
}
302+
}
298303

299-
/* cut out .. and previous */
300-
WMEMMOVE(path + prIdx, path + enIdx, sz - enIdx);
301-
path[sz - (enIdx - prIdx)] = '\0';
302-
303-
if (enIdx == sz) {
304-
path[prIdx] = '\0';
305-
}
304+
/* cut out .. and previous */
305+
WMEMMOVE(path + prIdx, path + enIdx, sz - enIdx);
306+
path[sz - (enIdx - prIdx)] = '\0';
306307

307-
/* case of at / */
308-
if (WSTRLEN(path) == 0) {
309-
path[0] = '/';
310-
path[1] = '\0';
311-
}
308+
if (enIdx == sz) {
309+
path[prIdx] = '\0';
310+
}
312311

313-
found = 1;
314-
break;
312+
/* case of at / */
313+
if (WSTRLEN(path) == 0) {
314+
path[0] = '/';
315+
path[1] = '\0';
315316
}
317+
318+
found = 1;
319+
break;
316320
}
317321
}
318-
} while (found);
319-
}
322+
}
323+
} while (found);
320324
}
321325

322326
#define WS_MAX_EXAMPLE_RW 1024

src/ssh.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1658,9 +1658,13 @@ static int DoSshPubKey(const byte* in, word32 inSz, byte** out,
16581658
/*
16591659
SSH format is:
16601660
type AAAABASE64ENCODEDKEYDATA comment
1661+
1662+
allocate a copy to tokenize, add a null terminator.
16611663
*/
1662-
c = WSTRDUP((const char*)in, heap, DYNTYPE_STRING);
1664+
c = (char*)WMALLOC(inSz + 1, heap, DYNTYPE_STRING);
16631665
if (c != NULL) {
1666+
WMEMCPY(c, in, inSz);
1667+
c[inSz-1] = 0;
16641668
type = WSTRTOK(c, " \n", &last);
16651669
key = WSTRTOK(NULL, " \n", &last);
16661670
}

tests/api.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -504,26 +504,38 @@ static int load_file(const char* filename, byte** buf, word32* bufSz)
504504
}
505505

506506
if (ret == 0) {
507-
fseek(f, 0, XSEEK_END);
508-
*bufSz = (word32)ftell(f);
509-
rewind(f);
507+
ret = fseek(f, 0, XSEEK_END);
508+
if (ret < 0)
509+
ret = -3;
510+
}
511+
512+
if (ret == 0) {
513+
long sz = ftell(f);
514+
if (sz < 0)
515+
ret = -4;
516+
else
517+
*bufSz = (word32)sz;
510518
}
511519

512520
if (ret == 0) {
521+
rewind(f);
513522
*buf = (byte*)malloc(*bufSz);
514523
if (*buf == NULL)
515-
ret = -3;
524+
ret = -5;
516525
}
517526

518527
if (ret == 0) {
519-
int readSz;
520-
readSz = (int)fread(*buf, 1, *bufSz, f);
521-
if (readSz < (int)*bufSz)
522-
ret = -4;
528+
size_t readSz;
529+
readSz = fread(*buf, 1, *bufSz, f);
530+
if (readSz < *bufSz)
531+
ret = -6;
523532
}
524533

525-
if (f != NULL)
526-
fclose(f);
534+
if (f != NULL) {
535+
ret = fclose(f);
536+
if (ret < 0)
537+
ret = -7;
538+
}
527539

528540
return ret;
529541
}

0 commit comments

Comments
 (0)