Merge "LocIpc could be using data member from a deleted obj"

This commit is contained in:
qctecmdr Service 2019-01-17 14:19:17 -08:00 committed by Gerrit - the friendly Code Review server
commit 47b591383b
2 changed files with 53 additions and 67 deletions

View file

@ -69,8 +69,9 @@ bool LocIpc::startListeningNonBlocking(const std::string& name) {
} }
bool LocIpc::startListeningBlocking(const std::string& name) { bool LocIpc::startListeningBlocking(const std::string& name) {
bool stopRequested = false;
int fd = socket(AF_UNIX, SOCK_DGRAM, 0); int fd = socket(AF_UNIX, SOCK_DGRAM, 0);
if (fd < 0) { if (fd < 0) {
LOC_LOGe("create socket error. reason:%s", strerror(errno)); LOC_LOGe("create socket error. reason:%s", strerror(errno));
return false; return false;
@ -87,11 +88,7 @@ bool LocIpc::startListeningBlocking(const std::string& name) {
if (::bind(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) { if (::bind(fd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
LOC_LOGe("bind socket error. reason:%s", strerror(errno)); LOC_LOGe("bind socket error. reason:%s", strerror(errno));
::close(fd); } else {
fd = -1;
return false;
}
mIpcFd = fd; mIpcFd = fd;
// inform that the socket is ready to receive message // inform that the socket is ready to receive message
@ -102,15 +99,17 @@ bool LocIpc::startListeningBlocking(const std::string& name) {
std::string abort = LOC_MSG_ABORT; std::string abort = LOC_MSG_ABORT;
while (1) { while (1) {
msg.resize(LOC_MSG_BUF_LEN); msg.resize(LOC_MSG_BUF_LEN);
nBytes = ::recvfrom(mIpcFd, (void*)(msg.data()), msg.size(), 0, NULL, NULL); nBytes = ::recvfrom(fd, (void*)(msg.data()), msg.size(), 0, NULL, NULL);
if (nBytes < 0) { if (nBytes < 0) {
LOC_LOGe("cannot read socket. reason:%s", strerror(errno));
break; break;
} else if (nBytes == 0) { } else if (0 == nBytes) {
continue; continue;
} }
if (strncmp(msg.data(), abort.c_str(), abort.length()) == 0) { if (strncmp(msg.data(), abort.c_str(), abort.length()) == 0) {
LOC_LOGi("recvd abort msg.data %s", msg.data()); LOC_LOGi("recvd abort msg.data %s", msg.data());
stopRequested = true;
break; break;
} }
@ -125,51 +124,39 @@ bool LocIpc::startListeningBlocking(const std::string& name) {
msg.resize(msgLen); msg.resize(msgLen);
size_t msgLenReceived = 0; size_t msgLenReceived = 0;
while ((msgLenReceived < msgLen) && (nBytes > 0)) { while ((msgLenReceived < msgLen) && (nBytes > 0)) {
nBytes = recvfrom(mIpcFd, (void*)&(msg[msgLenReceived]), nBytes = recvfrom(fd, (void*)&(msg[msgLenReceived]),
msg.size() - msgLenReceived, 0, NULL, NULL); msg.size() - msgLenReceived, 0, NULL, NULL);
msgLenReceived += nBytes; msgLenReceived += nBytes;
} }
if (nBytes > 0) { if (nBytes > 0) {
onReceive(msg); onReceive(msg);
} else { } else {
LOC_LOGe("cannot read socket. reason:%s", strerror(errno));
break; break;
} }
} }
} }
if (mStopRequested) {
mStopRequested = false;
return true;
} else {
LOC_LOGe("cannot read socket. reason:%s", strerror(errno));
(void)::close(mIpcFd);
mIpcFd = -1;
return false;
} }
if (::close(fd)) {
LOC_LOGe("cannot close socket:%s", strerror(errno));
}
unlink(name.c_str());
return stopRequested;
} }
void LocIpc::stopListening() { void LocIpc::stopListening() {
const char *socketName = nullptr; const char *socketName = nullptr;
mStopRequested = true;
if (mRunnable) { if (mIpcFd >= 0) {
std::string abort = LOC_MSG_ABORT; std::string abort = LOC_MSG_ABORT;
socketName = (reinterpret_cast<LocIpcRunnable *>(mRunnable))->mIpcName.c_str(); socketName = (reinterpret_cast<LocIpcRunnable *>(mRunnable))->mIpcName.c_str();
send(socketName, abort); send(socketName, abort);
mRunnable = nullptr;
}
if (mIpcFd >= 0) {
if (::close(mIpcFd)) {
LOC_LOGe("cannot close socket:%s", strerror(errno));
}
mIpcFd = -1; mIpcFd = -1;
} }
if (mRunnable) {
//delete from the file system at the end mRunnable = nullptr;
if (socketName) {
unlink(socketName);
} }
} }

View file

@ -44,7 +44,7 @@ class LocIpcSender;
class LocIpc { class LocIpc {
friend LocIpcSender; friend LocIpcSender;
public: public:
inline LocIpc() : mIpcFd(-1), mStopRequested(false), mRunnable(nullptr) {} inline LocIpc() : mIpcFd(-1), mRunnable(nullptr) {}
inline virtual ~LocIpc() { stopListening(); } inline virtual ~LocIpc() { stopListening(); }
// Listen for new messages in current thread. Calling this funciton will // Listen for new messages in current thread. Calling this funciton will
@ -93,7 +93,6 @@ private:
const uint8_t data[], uint32_t length); const uint8_t data[], uint32_t length);
int mIpcFd; int mIpcFd;
bool mStopRequested;
LocThread mThread; LocThread mThread;
LocRunnable *mRunnable; LocRunnable *mRunnable;
}; };