Skip to content

Commit

Permalink
Fix some Coverity advisories (#1913)
Browse files Browse the repository at this point in the history
[scan:coverity]
  • Loading branch information
mikee47 authored and slaff committed Oct 27, 2019
1 parent c17aeb4 commit 40168cb
Show file tree
Hide file tree
Showing 15 changed files with 62 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Sming/Arch/Esp8266/Components/driver/SerialBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct SerialBuffer {
public:
~SerialBuffer()
{
delete buffer;
delete[] buffer;
}

size_t getSize()
Expand Down
8 changes: 4 additions & 4 deletions Sming/Arch/Host/Components/driver/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ static bool realloc_buffer(SerialBuffer*& buffer, size_t new_size)
{
if(buffer != nullptr) {
if(new_size == 0) {
uart_disable_interrupts();
(void)uart_disable_interrupts();
delete buffer;
buffer = nullptr;
uart_restore_interrupts();
Expand Down Expand Up @@ -207,7 +207,7 @@ size_t uart_rx_available(uart_t* uart)
return 0;
}

uart_disable_interrupts();
(void)uart_disable_interrupts();

size_t avail = 0;

Expand Down Expand Up @@ -260,7 +260,7 @@ size_t uart_tx_free(uart_t* uart)
return 0;
}

uart_disable_interrupts();
(void)uart_disable_interrupts();

size_t space = 0;
if(uart->tx_buffer != nullptr) {
Expand Down Expand Up @@ -324,7 +324,7 @@ void uart_flush(uart_t* uart, uart_mode_t mode)
bool flushRx = mode != UART_TX_ONLY && uart->mode != UART_TX_ONLY;
bool flushTx = mode != UART_RX_ONLY && uart->mode != UART_RX_ONLY;

uart_disable_interrupts();
(void)uart_disable_interrupts();
if(flushRx && uart->rx_buffer != nullptr) {
uart->rx_buffer->clear();
}
Expand Down
3 changes: 2 additions & 1 deletion Sming/Arch/Host/Components/hostlib/hostlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ int msleep(unsigned ms)

void getHostAppDir(char* path, size_t bufSize)
{
size_t len __attribute__((unused));
size_t len;
char sep;
#ifdef __WIN32
len = GetModuleFileName(NULL, path, bufSize);
Expand All @@ -40,6 +40,7 @@ void getHostAppDir(char* path, size_t bufSize)
len = readlink("/proc/self/exe", path, bufSize);
sep = '/';
#endif
path[len] = '\0';
char* p = strrchr(path, sep);
if(p) {
p[1] = '\0';
Expand Down
4 changes: 2 additions & 2 deletions Sming/Arch/Host/Components/hostlib/keyb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ void keyb_restore()
attr.c_lflag |= ICANON | ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &attr);

fcntl(0, F_SETFL, g_orig_flags);
(void)fcntl(0, F_SETFL, g_orig_flags);
g_values_changed = false;
}
#endif
Expand All @@ -250,7 +250,7 @@ void keyb_raw()
}

// make stdin non-blocking
fcntl(0, F_SETFL, g_orig_flags | O_NONBLOCK);
(void)fcntl(0, F_SETFL, g_orig_flags | O_NONBLOCK);
//
struct termios attr = g_orig_attr;
attr.c_lflag &= ~(ICANON | ECHO);
Expand Down
2 changes: 1 addition & 1 deletion Sming/Arch/Host/Components/hostlib/sockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ CSocket* CServerSocket::try_connect()
struct sockaddr sa;
socklen_t len = sizeof(sa);
int fd = ::accept(m_fd, &sa, &len);
if(fd <= 0) {
if(fd < 0) {
return nullptr;
}

Expand Down
2 changes: 1 addition & 1 deletion Sming/Arch/Host/Components/hostlib/startup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static void pause(int secs)
{
if(secs == 0) {
hostmsg("Hit ENTER to continue.");
getchar();
(void)getchar();
} else if(secs > 0) {
hostmsg("Waiting for %u seconds...", secs);
msleep(secs * 1000);
Expand Down
18 changes: 16 additions & 2 deletions Sming/Arch/Host/Components/lwip/.patches/lwip.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,22 @@
diff --git a/contrib/ports/unix/port/sys_arch.c b/contrib/ports/unix/port/sys_arch.c
index a56d7de1..dd3f16e1 100644
index a56d7de1..6fc9f434 100644
--- a/contrib/ports/unix/port/sys_arch.c
+++ b/contrib/ports/unix/port/sys_arch.c
@@ -730,13 +730,3 @@ sys_arch_unprotect(sys_prot_t pval)
@@ -68,10 +68,12 @@
#include "lwip/stats.h"
#include "lwip/tcpip.h"

+extern u32_t os_random(void);
+
u32_t
lwip_port_rand(void)
{
- return (u32_t)rand();
+ return os_random();
}

static void
@@ -730,13 +732,3 @@ sys_arch_unprotect(sys_prot_t pval)
}
#endif /* SYS_LIGHTWEIGHT_PROT */

Expand Down
12 changes: 8 additions & 4 deletions Sming/Arch/Host/Components/lwip/Linux/host_lwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,17 @@ static void getMacAddress(const char* ifname, uint8_t hwaddr[6])
{
struct ifreq ifr = {0};
ifr.ifr_addr.sa_family = AF_INET;
strcpy(ifr.ifr_name, ifname);
strncpy(ifr.ifr_name, ifname, IFNAMSIZ);

int fd = socket(AF_INET, SOCK_DGRAM, 0);
ioctl(fd, SIOCGIFHWADDR, &ifr);
int res = ioctl(fd, SIOCGIFHWADDR, &ifr);
close(fd);

memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, 6);
if(res == 0) {
memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, 6);
} else {
memset(hwaddr, 0, 6);
}
}

/**
Expand Down Expand Up @@ -127,7 +131,7 @@ bool host_lwip_init(const struct lwip_param* param)

if(param->ipaddr == NULL) {
// Choose a default IP address
IP4_ADDR(&netcfg.ipaddr, ip4_addr1(&netcfg.gw), ip4_addr2(&netcfg.gw), ip4_addr3(&netcfg.gw), 10);
IP4_ADDR(&netcfg.ipaddr, (uint32_t)ip4_addr1(&netcfg.gw), (uint32_t)ip4_addr2(&netcfg.gw), (uint32_t)ip4_addr3(&netcfg.gw), 10U);
} else if(ip4addr_aton(param->ipaddr, &netcfg.ipaddr) != 1) {
hostmsg("Failed to parse provided IP address '%s'", param->ipaddr);
return false;
Expand Down
4 changes: 2 additions & 2 deletions Sming/Arch/Host/Components/spi_flash/flashmem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ static const char* defaultFlashFileName = "flash.bin";
bool host_flashmem_init(FlashmemConfig& config)
{
if(config.filename != nullptr) {
strcpy(flashFileName, config.filename);
strncpy(flashFileName, config.filename, sizeof(flashFileName));
} else {
getHostAppDir(flashFileName, sizeof(flashFileName));
strcat(flashFileName, defaultFlashFileName);
strcpy(flashFileName, defaultFlashFileName);
config.filename = flashFileName;
}

Expand Down
2 changes: 1 addition & 1 deletion Sming/Arch/Host/Core/DigitalHooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,6 @@ void DigitalHooks::pullup(uint16_t pin, bool enable)

unsigned long DigitalHooks::pulseIn(uint16_t pin, uint8_t state, unsigned long timeout)
{
host_printfp("pin: %u, state: %u, timeout: %u\n", __FUNCTION__, pin, state, timeout);
host_printfp("pin: %u, state: %u, timeout: %lu\n", __FUNCTION__, pin, state, timeout);
return 0;
}
2 changes: 1 addition & 1 deletion Sming/Arch/Host/Core/HardwarePWM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

#include <HardwarePWM.h>

HardwarePWM::HardwarePWM(uint8_t* pins, uint8_t no_of_pins) : channel_count(no_of_pins)
HardwarePWM::HardwarePWM(uint8_t* pins, uint8_t no_of_pins) : channel_count(no_of_pins), maxduty(0)
{
}

Expand Down
20 changes: 18 additions & 2 deletions Sming/Components/.patches/ws_parser.patch
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
diff --git a/ws_parser.c b/ws_parser.c
index 88e4810..3da68e1 100644
index 88e4810..e2d1147 100644
--- a/ws_parser.c
+++ b/ws_parser.c
@@ -244,7 +244,8 @@ ws_parser_execute(ws_parser_t* parser, /* mutates! */ char* buff, size_t len)
@@ -3,6 +3,7 @@
#endif

#include "ws_parser.h"
+#include <string.h>

enum {
S_OPCODE = 0,
@@ -27,6 +28,7 @@ enum {
void
ws_parser_init(ws_parser_t* parser, const ws_parser_callbacks_t* callbacks)
{
+ memset(parser, 0, sizeof(ws_parser_t));
parser->user_data = NULL;
parser->callbacks = callbacks;
parser->state = S_OPCODE;
@@ -244,7 +246,8 @@ ws_parser_execute(ws_parser_t* parser, /* mutates! */ char* buff, size_t len)
}

if(parser->mask_flag) {
Expand Down
4 changes: 2 additions & 2 deletions Sming/Core/Data/Stream/FileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ uint16_t FileStream::readMemoryBlock(char* data, int bufSize)
}

int available = fileRead(handle, data, std::min(size - pos, size_t(bufSize)));
check(available);
(void)check(available);

// Don't move cursor now (waiting seek)
fileSeek(handle, pos, eSO_FileStart);
(void)fileSeek(handle, pos, eSO_FileStart);

return available > 0 ? available : 0;
}
Expand Down
4 changes: 2 additions & 2 deletions Sming/Core/Data/Stream/GdbFileStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ uint16_t GdbFileStream::readMemoryBlock(char* data, int bufSize)
}

int available = gdb_syscall_read(handle, data, std::min(size - pos, size_t(bufSize)));
check(available);
(void)check(available);

// Don't move cursor now (waiting seek)
gdb_syscall_lseek(handle, pos, SEEK_SET);
(void)gdb_syscall_lseek(handle, pos, SEEK_SET);

return available > 0 ? available : 0;
}
Expand Down
2 changes: 1 addition & 1 deletion Sming/Core/HardwareSerial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ void HardwareSerial::systemDebugOutput(bool enabled)

void HardwareSerial::invokeCallbacks()
{
uart_disable_interrupts();
(void)uart_disable_interrupts();
auto status = callbackStatus;
callbackStatus = 0;
callbackQueued = false;
Expand Down

0 comments on commit 40168cb

Please sign in to comment.