Skip to content

Commit

Permalink
SonarCloud: don't use variable tainted by fread
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 authored and Daniel Adam committed Sep 26, 2023
1 parent 43443b6 commit eede00d
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 17 deletions.
4 changes: 3 additions & 1 deletion api/oc_server_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,9 @@ oc_add_collection_v1(oc_resource_t *collection)
void
oc_add_collection(oc_resource_t *collection)
{
oc_add_collection_v1(collection);
if (!oc_add_collection_v1(collection)) {
OC_ERR("failed to add collection");
}
}

oc_resource_t *
Expand Down
3 changes: 2 additions & 1 deletion apps/client_certification_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -603,7 +603,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
5 changes: 3 additions & 2 deletions apps/cloud_certification_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (pem_len > (long)*buffer_len) {
if (pem_len >= (long)*buffer_len) {
OC_PRINTF("ERROR: buffer provided too small\n");
fclose(fp);
return -1;
Expand All @@ -554,7 +554,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
5 changes: 3 additions & 2 deletions apps/cloud_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (pem_len > (long)*buffer_len) {
if (pem_len >= (long)*buffer_len) {
OC_PRINTF("ERROR: buffer provided too small\n");
fclose(fp);
return -1;
Expand All @@ -325,7 +325,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
5 changes: 3 additions & 2 deletions apps/cloud_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1634,7 +1634,7 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (pem_len > (long)*buffer_len) {
if (pem_len >= (long)*buffer_len) {
OC_PRINTF("ERROR: buffer provided too small\n");
fclose(fp);
return -1;
Expand All @@ -1644,7 +1644,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
5 changes: 3 additions & 2 deletions apps/cloud_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (pem_len > (long)*buffer_len) {
if (pem_len >= (long)*buffer_len) {
OC_PRINTF("ERROR: buffer provided too small\n");
fclose(fp);
return -1;
Expand All @@ -838,7 +838,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
3 changes: 2 additions & 1 deletion apps/server_certification_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -2023,7 +2023,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
3 changes: 2 additions & 1 deletion apps/smart_home_server_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
printf("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
3 changes: 2 additions & 1 deletion apps/smart_home_server_with_mock_swupdate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
5 changes: 3 additions & 2 deletions onboarding_tool/obtmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -1760,7 +1760,7 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (pem_len > (long)*buffer_len) {
if (pem_len >= (long)*buffer_len) {
OC_PRINTF("ERROR: buffer provided too small\n");
fclose(fp);
return -1;
Expand All @@ -1770,7 +1770,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down
5 changes: 3 additions & 2 deletions python/oc_python.c
Original file line number Diff line number Diff line change
Expand Up @@ -1492,7 +1492,7 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (pem_len > (long)*buffer_len) {
if (pem_len >= (long)*buffer_len) {
OC_PRINTF("[C]ERROR: buffer provided too small\n");
fclose(fp);
return -1;
Expand All @@ -1502,7 +1502,8 @@ read_pem(const char *file_path, char *buffer, size_t *buffer_len)
fclose(fp);
return -1;
}
if (fread(buffer, 1, pem_len, fp) < (size_t)pem_len) {
size_t to_read = (size_t)pem_len;
if (fread(buffer, 1, to_read, fp) < (size_t)pem_len) {
OC_PRINTF("[C]ERROR: unable to read PEM\n");
fclose(fp);
return -1;
Expand Down

0 comments on commit eede00d

Please sign in to comment.