diff --git a/libs/client_server/piclientserver_server.cpp b/libs/client_server/piclientserver_server.cpp index edfd2083..aaea5d21 100644 --- a/libs/client_server/piclientserver_server.cpp +++ b/libs/client_server/piclientserver_server.cpp @@ -37,6 +37,7 @@ PIClientServer::Server::Server() { auto sc = client_factory(); if (!sc) { piCout << "ClientFactory returns nullptr!"_tr("PIClientServer"); + delete c; return; } sc->createForServer(this, c); diff --git a/libs/main/core/piwaitevent_p.cpp b/libs/main/core/piwaitevent_p.cpp index 47983a5d..36ec80bc 100644 --- a/libs/main/core/piwaitevent_p.cpp +++ b/libs/main/core/piwaitevent_p.cpp @@ -65,9 +65,9 @@ void PIWaitEvent::destroy() { } #else for (int i = 0; i < 2; ++i) { - if (pipe_fd[i] != 0) { + if (pipe_fd[i] != -1) { ::close(pipe_fd[i]); - pipe_fd[i] = 0; + pipe_fd[i] = -1; } } #endif @@ -140,7 +140,7 @@ bool PIWaitEvent::isCreate() const { #ifdef WINDOWS return event; #else - return pipe_fd[ReadEnd] != 0; + return pipe_fd[ReadEnd] != -1; #endif } diff --git a/libs/main/core/piwaitevent_p.h b/libs/main/core/piwaitevent_p.h index 9814d449..1af19ac1 100644 --- a/libs/main/core/piwaitevent_p.h +++ b/libs/main/core/piwaitevent_p.h @@ -55,7 +55,7 @@ private: #ifdef WINDOWS void * event = nullptr; #else - int pipe_fd[2] = {0, 0}; + int pipe_fd[2] = {-1, -1}; fd_set fds[3]; enum { ReadEnd = 0, diff --git a/libs/main/io_devices/pican.cpp b/libs/main/io_devices/pican.cpp index 2d6fe04e..6d1e2295 100644 --- a/libs/main/io_devices/pican.cpp +++ b/libs/main/io_devices/pican.cpp @@ -24,6 +24,7 @@ # define PIP_CAN #endif #ifdef PIP_CAN +# include # include # include # include @@ -49,7 +50,7 @@ PICAN::PICAN(const PIString & path, PIIODevice::DeviceMode mode): PIIODevice(pat setThreadedReadBufferSize(256); setPath(path); can_id = 0; - sock = 0; + sock = -1; PRIVATE->event.create(); } @@ -67,19 +68,25 @@ bool PICAN::openDevice() { sock = socket(PF_CAN, SOCK_RAW, CAN_RAW); if (sock < 0) { piCoutObj << "Error! while opening socket"; + sock = -1; return false; } + fcntl(sock, F_SETFL, fcntl(sock, F_GETFL) | O_NONBLOCK); ifreq ifr; strcpy(ifr.ifr_name, path().dataAscii()); piCout << "PICAN try to get interface index..."; if (ioctl(sock, SIOCGIFINDEX, &ifr) < 0) { piCoutObj << "Error! while determin the interface ioctl"; + ::close(sock); + sock = -1; return false; } struct timeval tv; tv.tv_sec = 1; tv.tv_usec = 0; - setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof tv); + if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof tv) < 0) { + piCoutObj << "Error! while setting socket receive timeout"; + } // bind socket to all CAN interface sockaddr_can addr; addr.can_family = AF_CAN; @@ -87,6 +94,8 @@ bool PICAN::openDevice() { piCout << "PICAN try to bind socket to interface" << ifr.ifr_ifindex; if (bind(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0) { piCoutObj << "Error! while binding socket"; + ::close(sock); + sock = -1; return false; } piCout << "PICAN Open OK!"; @@ -101,7 +110,11 @@ bool PICAN::openDevice() { bool PICAN::closeDevice() { #ifdef PIP_CAN interrupt(); - if (sock > 0) ::close(sock); + if (sock != -1) { + ::shutdown(sock, SHUT_RDWR); + ::close(sock); + sock = -1; + } #endif return true; } @@ -109,6 +122,7 @@ bool PICAN::closeDevice() { ssize_t PICAN::readDevice(void * read_to, ssize_t max_size) { #ifdef PIP_CAN + if (sock == -1) return -1; // piCout << "PICAN read"; can_frame frame; ssize_t ret = 0; @@ -127,6 +141,7 @@ ssize_t PICAN::readDevice(void * read_to, ssize_t max_size) { ssize_t PICAN::writeDevice(const void * data, ssize_t max_size) { #ifdef PIP_CAN + if (sock == -1) return -1; // piCout << "PICAN write" << can_id << max_size; if (max_size > 8) { piCoutObj << "Can't send CAN frame bigger than 8 bytes (requested " << max_size << ")!"; diff --git a/libs/main/io_devices/pican.h b/libs/main/io_devices/pican.h index 93bef921..37938818 100644 --- a/libs/main/io_devices/pican.h +++ b/libs/main/io_devices/pican.h @@ -73,7 +73,7 @@ protected: private: PRIVATE_DECLARATION(PIP_EXPORT) - int sock; + int sock = -1; int can_id, readed_id; }; diff --git a/libs/main/io_devices/piethernet.cpp b/libs/main/io_devices/piethernet.cpp index ad4a08b3..a1245cc1 100644 --- a/libs/main/io_devices/piethernet.cpp +++ b/libs/main/io_devices/piethernet.cpp @@ -383,7 +383,7 @@ void PIEthernet::applyBuffers() { void PIEthernet::applyTimeout(int fd, int opt, PISystemTime tm) { if (fd == 0) return; - // piCoutObj << "setReadIsBlocking" << yes; + // piCoutObj << "setReadIsBlocking" << yes; #ifdef WINDOWS DWORD _tm = tm.toMilliseconds(); #else @@ -1234,7 +1234,7 @@ PIEthernet::InterfaceList PIEthernet::interfaces() { # ifdef QNX # ifndef BLACKBERRY int fd = ::open((PIString("/dev/io-net/") + ci.name).dataAscii(), O_RDONLY); - if (fd != 0) { + if (fd >= 0) { nic_config_t nic; devctl(fd, DCMD_IO_NET_GET_CONFIG, &nic, sizeof(nic), 0); ::close(fd); diff --git a/libs/main/io_devices/piiobytearray.cpp b/libs/main/io_devices/piiobytearray.cpp index 3b6e8374..699bd1e9 100644 --- a/libs/main/io_devices/piiobytearray.cpp +++ b/libs/main/io_devices/piiobytearray.cpp @@ -62,8 +62,7 @@ ssize_t PIIOByteArray::readDevice(void * read_to, ssize_t size) { if (ret <= 0) return -1; memcpy(read_to, data_->data(pos), ret); // piCout << "readed" << ret; - pos += size; - if (pos > data_->size_s()) pos = data_->size_s(); + pos += ret; return ret; } diff --git a/libs/main/io_devices/pipeer.cpp b/libs/main/io_devices/pipeer.cpp index c121dfea..0cf8bd6a 100644 --- a/libs/main/io_devices/pipeer.cpp +++ b/libs/main/io_devices/pipeer.cpp @@ -623,7 +623,10 @@ bool PIPeer::dataRead(const uchar * readed, ssize_t size) { return true; } cnt++; - if (cnt > _PIPEER_MSG_TTL || from == dp->name) return true; + if (cnt > _PIPEER_MSG_TTL || from == dp->name) { + eth_mutex.unlock(); + return true; + } sba << type << from << to << cnt << pba; // piCout << "translate packet" << from << "->" << to << ", ttl =" << cnt; sendToNeighbour(dp, sba); diff --git a/libs/main/io_devices/piserial.cpp b/libs/main/io_devices/piserial.cpp index 9859ef68..401d5dfc 100644 --- a/libs/main/io_devices/piserial.cpp +++ b/libs/main/io_devices/piserial.cpp @@ -513,7 +513,10 @@ bool PISerial::read(void * data, int size, double timeout_ms) { all = readDevice(data, 1); while (all < size) { ret = readDevice(&((uchar *)data)[all], size - all); - if (ret > 0) all += ret; + if (ret > 0) + all += ret; + else + break; } setOption(BlockingRead, br); received(data, all); @@ -1235,8 +1238,8 @@ PIVector PISerial::availableDevicesInfo(bool test) { for (const auto & e: de) { // TODO changes in FileInfo for (const auto & p: prefixes) { if (e.name().startsWith(p)) { - di = DeviceInfo(); - di.path = e.path; + di = DeviceInfo(); + di.path = e.path; # ifdef LINUX ssize_t lsz = readlink(("/sys/class/tty/" + e.name()).dataAscii(), linkbuf, 1024); if (lsz > 0) { diff --git a/libs/main/io_utils/pifiletransfer.cpp b/libs/main/io_utils/pifiletransfer.cpp index 7389a769..acdeac73 100644 --- a/libs/main/io_utils/pifiletransfer.cpp +++ b/libs/main/io_utils/pifiletransfer.cpp @@ -131,6 +131,12 @@ bool PIFileTransfer::sendFiles(const PIVector & files) { void PIFileTransfer::processFile(int id, ullong start, PIByteArray & data) { // piCout << "processFile" << id << files_.size(); + if (id <= 0 || id > files_.size_s()) { + cur_file_string = "Error: Invalid file id " + PIString::fromNumber(id); + piCoutObj << cur_file_string; + stopReceive(); + return; + } PFTFileInfo fi = files_[id - 1]; bytes_file_all = fi.size; bytes_file_cur = start; diff --git a/libs/main/system/piprocess.cpp b/libs/main/system/piprocess.cpp index ffa6a871..115b9a69 100644 --- a/libs/main/system/piprocess.cpp +++ b/libs/main/system/piprocess.cpp @@ -319,6 +319,13 @@ void PIProcess::startProc(bool detached) { auto largs = convertToCharArrays(args); auto lenv = convertToCharArrays(env); int pid_ = fork(); + if (pid_ < 0) { + piCoutObj << "\"fork\" error: " << errorString(); + PRIVATE->closeAllPipes(); + delete[] largs; + delete[] lenv; + return; + } if (!detached) PRIVATE->pid = pid_; if (pid_ == 0) { if (!wd.isEmpty()) { diff --git a/libs/main/text/pistring.cpp b/libs/main/text/pistring.cpp index db3b9da8..65bf8035 100644 --- a/libs/main/text/pistring.cpp +++ b/libs/main/text/pistring.cpp @@ -671,7 +671,7 @@ PIString & PIString::operator+=(const PIConstChars & str) { if (!str.isEmpty()) { size_t os = d.size(); d.enlarge(str.size()); - for (size_t l = 0; l < d.size(); ++l) { + for (size_t l = 0; l < str.size(); ++l) { d[os + l] = str[l]; } } @@ -1763,7 +1763,7 @@ PIString PIString::toLowerCase() const { char PIString::toChar() const { - char v; + char v = 0; sscanf(dataAscii(), "%c", &v); return v; } diff --git a/libs/mqtt_client/pimqttclient.cpp b/libs/mqtt_client/pimqttclient.cpp index 60ffb958..133639e5 100644 --- a/libs/mqtt_client/pimqttclient.cpp +++ b/libs/mqtt_client/pimqttclient.cpp @@ -60,7 +60,7 @@ STATIC_INITIALIZER_END PRIVATE_DEFINITION_START(PIMQTT::Client) MQTTClient client = nullptr; - bool connected = false; + std::atomic connected{false}; PIProtectedVariable endpoints;