Skip to content

Commit

Permalink
tls: Fix coverity issues
Browse files Browse the repository at this point in the history
Issues 319212 and 319248: bad check of error condition in SSL write

Issue 319221: overlapping memcpy when creating an incomming message
  • Loading branch information
Danielius1922 committed Jun 22, 2023
1 parent eac6f2a commit 346dd6d
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 134 deletions.
25 changes: 18 additions & 7 deletions port/android/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,17 +76,28 @@ oc_storage_read(const char *store, uint8_t *buf, size_t size)
return -EINVAL;
}

fseek(fp, 0, SEEK_END);
size_t fsize = ftell(fp);
if (fsize > size) {
fclose(fp);
return -EINVAL;
if (fseek(fp, 0, SEEK_END) != 0) {
goto error;
}
long fsize = ftell(fp);
if (fsize < 0) {
goto error;
}
if ((size_t)fsize > size) {
errno = EINVAL;
goto error;
}
if (fseek(fp, 0, SEEK_SET) != 0) {
goto error;
}
fseek(fp, 0, SEEK_SET);

size = fread(buf, 1, size, fp);
fclose(fp);
return size;
return (long)size;

error:
fclose(fp);
return -errno;
}

long
Expand Down
23 changes: 17 additions & 6 deletions port/linux/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,28 @@ oc_storage_read(const char *store, uint8_t *buf, size_t size)
return -EINVAL;
}

fseek(fp, 0, SEEK_END);
size_t fsize = ftell(fp);
if (fsize > size) {
fclose(fp);
return -EINVAL;
if (fseek(fp, 0, SEEK_END) != 0) {
goto error;
}
long fsize = ftell(fp);
if (fsize < 0) {
goto error;
}
if ((size_t)fsize > size) {
errno = EINVAL;
goto error;
}
if (fseek(fp, 0, SEEK_SET) != 0) {
goto error;
}
fseek(fp, 0, SEEK_SET);

size = fread(buf, 1, size, fp);
fclose(fp);
return (long)size;

error:
fclose(fp);
return -errno;
}

static long
Expand Down
23 changes: 17 additions & 6 deletions port/windows/storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,28 @@ oc_storage_read(const char *store, uint8_t *buf, size_t size)
return -EINVAL;
}

fseek(fp, 0, SEEK_END);
size_t fsize = ftell(fp);
if (fsize > size) {
fclose(fp);
return -EINVAL;
if (fseek(fp, 0, SEEK_END) != 0) {
goto error;
}
long fsize = ftell(fp);
if (fsize < 0) {
goto error;
}
if ((size_t)fsize > size) {
errno = EINVAL;
goto error;
}
if (fseek(fp, 0, SEEK_SET) != 0) {
goto error;
}
fseek(fp, 0, SEEK_SET);

size = fread(buf, 1, size, fp);
fclose(fp);
return (long)size;

error:
fclose(fp);
return -errno;
}

long
Expand Down
Loading

0 comments on commit 346dd6d

Please sign in to comment.