Fix some malloc/delete mismatches

This commit is contained in:
2021-12-08 17:35:24 -05:00
parent 9ebba34061
commit 2e6a3ab74e
3 changed files with 48 additions and 43 deletions

5
luprex/.gitignore vendored
View File

@@ -19,3 +19,8 @@ luajit/src/lj_recdef.h
luajit/src/host/buildvm_arch.h
luajit/src/jit/vmdef.lua
luajit/src/libluajit.a
eris-master/src/lua
eris-master/src/luac
eris-master/test/persist
eris-master/test/unpersist

View File

@@ -57,59 +57,59 @@ public:
bool connected_[MAX_CHAN];
bool short_sleep_;
std::map<int, SOCKET> listen_sockets_;
std::unique_ptr<char> chbuf;
std::unique_ptr<char[]> chbuf;
int64_t basetime_;
int64_t get_now() {
struct timeval tv;
gettimeofday(&tv, nullptr);
int64_t tv_sec = tv.tv_sec;
int64_t tv_usec = tv.tv_usec;
return tv_sec * 1000000 + tv_usec;
struct timeval tv;
gettimeofday(&tv, nullptr);
int64_t tv_sec = tv.tv_sec;
int64_t tv_usec = tv.tv_usec;
return tv_sec * 1000000 + tv_usec;
}
SOCKET open_connection(const std::string &target, std::string &err) {
struct addrinfo *addrs = nullptr;
struct addrinfo *goodaddr = nullptr;
struct addrinfo hints;
SOCKET sock = INVALID_SOCKET;
struct addrinfo hints;
SOCKET sock = INVALID_SOCKET;
std::string host, port;
char errbuf[1024];
char errbuf[1024];
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_NUMERICSERV;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;
hints.ai_flags = AI_NUMERICSERV;
err = "";
util::split_host_port(target, host, port);
int status = getaddrinfo(host.c_str(), port.c_str(), &hints, &addrs);
if (status != 0) {
err = gai_strerror(status);
if (status != 0) {
err = gai_strerror(status);
goto error;
}
if (addrs == nullptr) {
if (addrs == nullptr) {
err = "no such host found";
goto error;
}
goodaddr = addrs;
assert(goodaddr->ai_family == AF_INET);
assert(goodaddr->ai_socktype == SOCK_STREAM);
assert(goodaddr->ai_protocol == IPPROTO_TCP);
goodaddr = addrs;
assert(goodaddr->ai_family == AF_INET);
assert(goodaddr->ai_socktype == SOCK_STREAM);
assert(goodaddr->ai_protocol == IPPROTO_TCP);
sock = socket(goodaddr->ai_family, goodaddr->ai_socktype, goodaddr->ai_protocol);
assert(sock > 0);
set_nonblocking(sock);
status = connect(sock, goodaddr->ai_addr, goodaddr->ai_addrlen);
if ((status != 0) && (errno != EINPROGRESS)) {
goto error_errno;
goto error_errno;
}
freeaddrinfo(addrs);
return sock;
error_errno:
strerror_r(errno, errbuf, 1024);
err = errbuf;
strerror_r(errno, errbuf, 1024);
err = errbuf;
error:
if (sock != INVALID_SOCKET) close(sock);
if (addrs != nullptr) freeaddrinfo(addrs);
@@ -121,7 +121,7 @@ public:
err = "";
SOCKET sock = socket(AF_INET, SOCK_STREAM, 0);
assert(sock > 0);
struct sockaddr_in server;
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
@@ -200,19 +200,19 @@ public:
while (true) {
driven_->drv_peek_outgoing(0, &nbytes, &bytes);
if (nbytes == 0) break;
int nwrote = write(1, bytes, nbytes);
assert(nwrote > 0);
driven_->drv_sent_outgoing(0, nwrote);
int nwrote = write(1, bytes, nbytes);
assert(nwrote > 0);
driven_->drv_sent_outgoing(0, nwrote);
}
}
void handle_console_input() {
char buffer[32];
while (true) {
int nread = read(0, buffer, 32);
if (nread == 0) break;
assert(nread > 0);
driven_->drv_recv_incoming(0, nread, buffer);
char buffer[32];
while (true) {
int nread = read(0, buffer, 32);
if (nread == 0) break;
assert(nread > 0);
driven_->drv_recv_incoming(0, nread, buffer);
}
}
@@ -240,9 +240,9 @@ public:
short_sleep_ = true;
continue;
}
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
return;
}
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
return;
}
if (errno == ECONNABORTED) {
// The remote disconnected before we had a chance to accept.
// Just pretend it never happened.
@@ -286,8 +286,8 @@ public:
timeout.tv_sec = mstimeout / 1000;
timeout.tv_usec = (mstimeout - (timeout.tv_sec*1000)) * 1000;
int status = select(nfds, &rfds, &wfds, &efds, &timeout);
assert(status >= 0);
assert(status >= 0);
for (auto &p : listen_sockets_) {
if (FD_ISSET(p.second, &rfds) || FD_ISSET(p.second, &efds)) {
accept_connections(p.first, p.second);
@@ -324,8 +324,8 @@ public:
}
void drive(DrivenEngine *de, int argc, char *argv[]) {
enableRawMode();
init(de);
enableRawMode();
init(de);
DrivenEngine::set(de);
basetime_ = get_now();
driven_->drv_set_lua_source(util::read_lua_source("lua"));

View File

@@ -31,7 +31,7 @@ StreamBuffer::StreamBuffer(const char *s, int64_t size) {
}
StreamBuffer::~StreamBuffer() {
if (owned_ && (buf_lo_ != 0)) delete buf_lo_;
if (owned_ && (buf_lo_ != 0)) free(buf_lo_);
}
int64_t StreamBuffer::total_reads() const {