Skip to content

Commit

Permalink
Fix issues reported by Coverity
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielius1922 committed Sep 26, 2024
1 parent 8650644 commit 1602616
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 8 deletions.
2 changes: 2 additions & 0 deletions api/oc_collection.c
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ oc_get_link_by_uri(oc_collection_t *collection, const char *uri_path,
size_t resource_uri_len = oc_string_len(link->resource->uri);
while (resource_uri[0] == '/') {
resource_uri++;
// overflow check for coverity scan
assert(resource_uri_len > 0);
resource_uri_len--;
}
if (resource_uri_len == uri_path_len &&
Expand Down
5 changes: 5 additions & 0 deletions api/oc_endpoint.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include "util/oc_macros_internal.h"
#include "util/oc_memb.h"

#include <assert.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -167,6 +170,8 @@ oc_endpoint_to_cstring(const oc_endpoint_t *endpoint, char *buffer,
if (written < 0) {
return -1;
}
// overflow check for coverity scan
assert(len <= INT_MAX - written && "Integer overflow detected");
return len + written;
}

Expand Down
9 changes: 5 additions & 4 deletions apps/secure_mcast_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,12 +330,13 @@ discovery(const char *di, const char *uri, oc_string_array_t types,
const oc_endpoint_t *ep = endpoint;
oc_string_t ep_str;
bool supports_mcast = false;
while (ep) {
while (ep != NULL) {
memset(&ep_str, 0, sizeof(oc_string_t));
if (oc_endpoint_to_string(ep, &ep_str) >= 0) {
if ((oc_string_len(ep_str) == 23 &&
if (oc_endpoint_to_string(ep, &ep_str) == 0) {
size_t ep_str_len = oc_string_len(ep_str);
if ((ep_str_len == 23 &&
memcmp(oc_string(ep_str), "coap://224.0.1.187:5683", 23) == 0) ||
(oc_string_len(ep_str) == 23 &&
(ep_str_len == 23 &&
memcmp(oc_string(ep_str), "coap://[ff02::158]:5683", 23) == 0)) {
supports_mcast = true;
}
Expand Down
3 changes: 3 additions & 0 deletions port/linux/ip.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <errno.h>
#include <limits.h>
#include <netinet/in.h>
#include <stdint.h>
#include <string.h>
#include <sys/socket.h>

Expand Down Expand Up @@ -119,6 +120,8 @@ oc_ip_send_msg(int sock, struct sockaddr_storage *receiver,
OC_ERR("sendmsg failed (error %d)", (int)errno);
break;
}
// overflow check for coverity scan
assert(bytes_sent <= SIZE_MAX - (size_t)ret && "Integer overflow detected");
bytes_sent += ret;
}
OC_TRACE("Sent %zu bytes", bytes_sent);
Expand Down
9 changes: 7 additions & 2 deletions port/linux/tcpsession.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#include <fcntl.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>

Expand Down Expand Up @@ -939,8 +940,9 @@ tcp_send_message(int sockfd, const oc_message_t *message)
{
size_t bytes_sent = 0;
do {
ssize_t send_len = send(sockfd, message->data + bytes_sent,
message->length - bytes_sent, MSG_NOSIGNAL);
const void *data = message->data + bytes_sent;
size_t data_length = message->length - bytes_sent;
ssize_t send_len = send(sockfd, data, data_length, MSG_NOSIGNAL);
if (send_len < 0) {
if (errno == EINTR) {
continue;
Expand All @@ -951,6 +953,9 @@ tcp_send_message(int sockfd, const oc_message_t *message)
}
return (int)bytes_sent;
}
// overflow check for coverity scan
assert(bytes_sent <= SIZE_MAX - (size_t)send_len &&
"Integer overflow detected");
bytes_sent += send_len;
} while (bytes_sent < message->length);

Expand Down
9 changes: 7 additions & 2 deletions util/jsmn/jsmn.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

#include <assert.h>
#include <ctype.h>
#include <limits.h>
#include <stdint.h>
#include <string.h>

Expand Down Expand Up @@ -247,6 +248,8 @@ jsmn_parse_next_char(jsmn_parser_t *parser, jsmntok_t *token, const char *js,
if (r < 0) {
return r;
}
// overflow check for coverity scan
assert(count <= INT_MAX - r && "Integer overflow detected");
count += r;
break;
}
Expand Down Expand Up @@ -289,20 +292,22 @@ jsmn_parse(jsmn_parser_t *parser, const char *js, const size_t len,
{
jsmntok_t token;
jsmn_init_token(&token);
unsigned count = 0;
int count = 0;
for (; parser->pos < len && js[parser->pos] != '\0'; parser->pos++) {
int r = jsmn_parse_next_char(parser, &token, js, len, cb, data);
if (r < 0) {
return r;
}
// overflow check for coverity scan
assert(count <= INT_MAX - r && "Integer overflow detected");
count += r;
}

if (parser->depth > 0) {
return JSMN_ERROR_PART;
}

return (int)count;
return count;
}

void
Expand Down

0 comments on commit 1602616

Please sign in to comment.