Skip to content

Commit f24c3c2

Browse files
committed
Coverity: Dereference before null check
1. When cleaning the path, check that the path pointer is not null before using it. 2. Move the strlen of path. 3. Remove the second check of path, and just loop over it. Fixed CID: 572847
1 parent cc83fc3 commit f24c3c2

1 file changed

Lines changed: 43 additions & 39 deletions

File tree

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

0 commit comments

Comments
 (0)