diff --git a/CMakeLists.txt b/CMakeLists.txt index d22b323d..102a42b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,7 +6,7 @@ endif() project(PIP) set(PIP_MAJOR 5) set(PIP_MINOR 8) -set(PIP_REVISION 0) +set(PIP_REVISION 1) set(PIP_SUFFIX _beta) set(PIP_COMPANY SHS) set(PIP_DOMAIN org.SHS) diff --git a/libs/console/piterminal.cpp b/libs/console/piterminal.cpp index 795d9af3..aeaffd9a 100644 --- a/libs/console/piterminal.cpp +++ b/libs/console/piterminal.cpp @@ -880,7 +880,7 @@ bool PITerminal::initialize() { execvp(argv[0], argv); delete[] argv[0]; delete[] argv; - exit(0); + exit(errno); } else { if (fr < 0 || PRIVATE->fd < 0) { piCoutObj << "forkpty error," << errorString(); diff --git a/libs/main/core/piwaitevent_p.cpp b/libs/main/core/piwaitevent_p.cpp index 3dff58bc..47983a5d 100644 --- a/libs/main/core/piwaitevent_p.cpp +++ b/libs/main/core/piwaitevent_p.cpp @@ -89,12 +89,13 @@ bool PIWaitEvent::wait(int fd, CheckRole role) { FD_SET(pipe_fd[ReadEnd], &(fds[CheckRead])); FD_SET(fd, &(fds[CheckExeption])); if (fd_index != CheckExeption) FD_SET(fd, &(fds[fd_index])); - int sr = ::select(nfds, &(fds[CheckRead]), &(fds[CheckWrite]), &(fds[CheckExeption]), nullptr); + int sr = ::select(nfds, &(fds[CheckRead]), &(fds[CheckWrite]), &(fds[CheckExeption]), nullptr); + if (sr < 0) return false; + errorClear(); int buf = 0; - while (::read(pipe_fd[ReadEnd], &buf, sizeof(buf)) > 0) - ; + while (::read(pipe_fd[ReadEnd], &buf, sizeof(buf)) > 0) {} // piCout << "wait result" << sr << FD_ISSET(fd, &(fds[CheckExeption])) << FD_ISSET(fd, &(fds[fd_index])); - if (sr == EBADF || sr == EINTR) return false; + if (errno == EBADF || errno == EINTR) return false; if (FD_ISSET(fd, &(fds[CheckExeption]))) return true; return FD_ISSET(fd, &(fds[fd_index])); #endif diff --git a/libs/main/io_devices/piethernet.cpp b/libs/main/io_devices/piethernet.cpp index 7cb7a1dc..ad4a08b3 100644 --- a/libs/main/io_devices/piethernet.cpp +++ b/libs/main/io_devices/piethernet.cpp @@ -102,7 +102,11 @@ #ifndef WINDOWS PIString getSockAddr(sockaddr * s) { - return s == 0 ? PIString() : PIStringAscii(inet_ntoa(((sockaddr_in *)s)->sin_addr)); + if (!s) return PIString(); + char buf[INET_ADDRSTRLEN]; + piZeroMemory(buf, sizeof(buf)); + const char * r = inet_ntop(AF_INET, &((sockaddr_in *)s)->sin_addr, buf, sizeof(buf)); + return r ? PIStringAscii(r) : PIString(); } #endif @@ -861,6 +865,7 @@ ssize_t PIEthernet::writeDevice(const void * data, ssize_t max_size) { return -1; } } + ret += sr; remain_data += sr; remain_size -= sr; } @@ -1175,13 +1180,19 @@ PIEthernet::InterfaceList PIEthernet::interfaces() { # else # ifdef ANDROID struct ifconf ifc; - int s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP); + int s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP); + if (s == -1) { + piCout << "[PIEthernet]" + << "Can`t create socket: %1"_tr("PIEthernet").arg(errorString()); + return il; + } ifc.ifc_len = 256; ifc.ifc_buf = new char[ifc.ifc_len]; if (ioctl(s, SIOCGIFCONF, &ifc) < 0) { piCout << "[PIEthernet]" << "Can`t get interfaces: %1"_tr("PIEthernet").arg(errorString()); delete[] ifc.ifc_buf; + ::close(s); return il; } int icnt = ifc.ifc_len / sizeof(ifreq); @@ -1200,7 +1211,8 @@ PIEthernet::InterfaceList PIEthernet::interfaces() { if (ci.address == "127.0.0.1") ci.flags |= PIEthernet::ifLoopback; il << ci; } - delete ifc.ifc_buf; + delete[] ifc.ifc_buf; + ::close(s); # else struct ifaddrs *ret, *cif = 0; int s = ::socket(AF_INET, SOCK_STREAM, IPPROTO_IP); @@ -1291,8 +1303,10 @@ PINetworkAddress PIEthernet::interfaceAddress(const PIString & interface_) { piZeroMemory(ifr); strcpy(ifr.ifr_name, interface_.dataAscii()); int s = ::socket(AF_INET, SOCK_DGRAM, 0); - ioctl(s, SIOCGIFADDR, &ifr); - ::close(s); + if (s != -1) { + ioctl(s, SIOCGIFADDR, &ifr); + ::close(s); + } struct sockaddr_in * sa = (struct sockaddr_in *)&ifr.ifr_addr; return PINetworkAddress(uint(sa->sin_addr.s_addr)); #endif @@ -1413,13 +1427,13 @@ void PIEthernet::ethClosesocket(int sock, bool shutdown) { int PIEthernet::ethSetsockopt(int sock, int level, int optname, const void * optval, int optlen) { if (sock < 0) return -1; auto ret = setsockopt(sock, - level, - optname, + level, + optname, #ifdef WINDOWS - (char *) + (char *) #endif - optval, - optlen); + optval, + optlen); if (ret != 0) piCout << "setsockopt error:" << ethErrorString(); return ret; } diff --git a/libs/main/serialization/pibinarystream.h b/libs/main/serialization/pibinarystream.h index 80dcbe25..0d4d1ab9 100644 --- a/libs/main/serialization/pibinarystream.h +++ b/libs/main/serialization/pibinarystream.h @@ -56,17 +56,17 @@ #else -# define BINARY_STREAM_FRIEND(T) \ - template \ - friend PIBinaryStream

& operator<<(PIBinaryStream

& s, const T & v); \ - template \ - friend PIBinaryStream

& operator>>(PIBinaryStream

& s, T & v); +# define BINARY_STREAM_FRIEND(T) \ + template \ + friend PIBinaryStream

& operator<<(PIBinaryStream

& s, const T & v); \ + template \ + friend PIBinaryStream

& operator>>(PIBinaryStream

& s, T & v); # define BINARY_STREAM_WRITE(T) \ - template \ - inline PIBinaryStream

& operator<<(PIBinaryStream

& s, const T & v) + template \ + inline PIBinaryStream

& operator<<(PIBinaryStream

& s, const T & v) # define BINARY_STREAM_READ(T) \ - template \ - inline PIBinaryStream

& operator>>(PIBinaryStream

& s, T & v) + template \ + inline PIBinaryStream

& operator>>(PIBinaryStream

& s, T & v) #endif @@ -410,7 +410,7 @@ template & operator>>(PIBinaryStream

& s, PIVector & v) { // piCout << ">> vector trivial default"; int sz = s.binaryStreamTakeInt(); - if (s.wasReadError()) { + if (s.wasReadError() || sz < 0) { fprintf(stderr, "error with PIVector<%s>\n", __PIP_TYPENAME__(T)); v.clear(); return s; @@ -433,7 +433,7 @@ template & operator>>(PIBinaryStream

& s, PIVector & v) { // piCout << ">> vector trivial custom"; int sz = s.binaryStreamTakeInt(); - if (s.wasReadError()) { + if (s.wasReadError() || sz < 0) { fprintf(stderr, "error with PIVector<%s>\n", __PIP_TYPENAME__(T)); v.clear(); return s; @@ -462,7 +462,7 @@ template & operator>>(PIBinaryStream

& s, PIDeque & v) { // piCout << ">> deque trivial default"; int sz = s.binaryStreamTakeInt(); - if (s.wasReadError()) { + if (s.wasReadError() || sz < 0) { fprintf(stderr, "error with PIDeque<%s>\n", __PIP_TYPENAME__(T)); v.clear(); return s; @@ -485,7 +485,7 @@ template & operator>>(PIBinaryStream

& s, PIDeque & v) { // piCout << ">> deque trivial custom"; int sz = s.binaryStreamTakeInt(); - if (s.wasReadError()) { + if (s.wasReadError() || sz < 0) { fprintf(stderr, "error with PIDeque<%s>\n", __PIP_TYPENAME__(T)); v.clear(); return s; @@ -516,7 +516,7 @@ inline PIBinaryStream

& operator>>(PIBinaryStream

& s, PIVector2D & v) int r, c; r = s.binaryStreamTakeInt(); c = s.binaryStreamTakeInt(); - if (s.wasReadError()) { + if (s.wasReadError() || r < 0 || c < 0) { fprintf(stderr, "error with PIVector2D<%s>\n", __PIP_TYPENAME__(T)); v.clear(); return s; @@ -542,6 +542,11 @@ inline PIBinaryStream

& operator>>(PIBinaryStream

& s, PIVector2D & v) PIVector tmp; r = s.binaryStreamTakeInt(); c = s.binaryStreamTakeInt(); + if (s.wasReadError() || r < 0 || c < 0) { + fprintf(stderr, "error with PIVector2D<%s>\n", __PIP_TYPENAME__(T)); + v.clear(); + return s; + } s >> tmp; if (s.wasReadError()) { fprintf(stderr, "error with PIVector2D<%s>\n", __PIP_TYPENAME__(T)); @@ -618,7 +623,7 @@ template & operator>>(PIBinaryStream

& s, PIVector & v) { int sz = s.binaryStreamTakeInt(); - if (s.wasReadError()) { + if (s.wasReadError() || sz < 0) { fprintf(stderr, "error with PIVector<%s>\n", __PIP_TYPENAME__(T)); v.clear(); return s; @@ -641,7 +646,7 @@ template & operator>>(PIBinaryStream

& s, PIDeque & v) { int sz = s.binaryStreamTakeInt(); - if (s.wasReadError()) { + if (s.wasReadError() || sz < 0) { fprintf(stderr, "error with PIDeque<%s>\n", __PIP_TYPENAME__(T)); v.clear(); return s; @@ -667,6 +672,11 @@ inline PIBinaryStream

& operator>>(PIBinaryStream

& s, PIVector2D & v) PIVector tmp; r = s.binaryStreamTakeInt(); c = s.binaryStreamTakeInt(); + if (s.wasReadError() || r < 0 || c < 0) { + fprintf(stderr, "error with PIVector2D<%s>\n", __PIP_TYPENAME__(T)); + v.clear(); + return s; + } s >> tmp; if (s.wasReadError()) { fprintf(stderr, "error with PIVector2D<%s>\n", __PIP_TYPENAME__(T)); @@ -700,7 +710,7 @@ template //! \~russian Восстанавливает ключи и значения %PIMap. inline PIBinaryStream

& operator>>(PIBinaryStream

& s, PIMap & v) { int sz = s.binaryStreamTakeInt(); - if (s.wasReadError()) { + if (s.wasReadError() || sz < 0) { fprintf(stderr, "error with PIMap<%s, %s>\n", __PIP_TYPENAME__(Key), __PIP_TYPENAME__(T)); v.clear(); return s; @@ -749,7 +759,7 @@ template //! \~russian Восстанавливает ключи %PISet. inline PIBinaryStream

& operator>>(PIBinaryStream

& s, PISet & v) { int sz = s.binaryStreamTakeInt(); - if (s.wasReadError()) { + if (s.wasReadError() || sz < 0) { fprintf(stderr, "error with PISet<%s>\n", __PIP_TYPENAME__(Key)); v.clear(); return s; diff --git a/libs/main/system/piprocess.cpp b/libs/main/system/piprocess.cpp index aa4c4713..ffa6a871 100644 --- a/libs/main/system/piprocess.cpp +++ b/libs/main/system/piprocess.cpp @@ -215,10 +215,10 @@ PRIVATE_DEFINITION_START(PIProcess) PeekNamedPipe(pipes[pipe_type][PipeRead], nullptr, 0, nullptr, &available, nullptr); if (available > 0) { BOOL ok = ReadFile(pipes[pipe_type][PipeRead], - read_buffer.data(offset), - piMini(available, read_buffer.size() - offset), - &bytes_read, - nullptr); + read_buffer.data(offset), + piMini(available, read_buffer.size() - offset), + &bytes_read, + nullptr); if (!ok) bytes_read = 0; } # else @@ -293,10 +293,10 @@ void PIProcess::startProc(bool detached) { si.dwFlags |= STARTF_USESTDHANDLES; const auto cmd = convertWindowsCmd(args); if (CreateProcessA(0, // No module name (use command line) - (LPSTR)cmd.data(), // Command line + (LPSTR)cmd.data(), // Command line 0, // Process handle not inheritable 0, // Thread handle not inheritable - true, // Set handle inheritance to FALSE + true, // Set handle inheritance to FALSE detached ? DETACHED_PROCESS /*CREATE_NEW_CONSOLE*/ : 0, // Creation flags 0, // Use environment wd.isEmpty() ? 0 : wd.data(), // Use working directory @@ -322,7 +322,7 @@ void PIProcess::startProc(bool detached) { if (!detached) PRIVATE->pid = pid_; if (pid_ == 0) { if (!wd.isEmpty()) { - if (!chdir(wd.data())) piCoutObj << "Error while set working directory"; + if (chdir(wd.data()) != 0) piCoutObj << "Error while set working directory"; } PRIVATE->closePipe(StdIn, PipeWrite); PRIVATE->closePipe(StdOut, PipeRead);