Handle null state, don't read/write on error

Need to guard against errors when reading/writing probably, if there is
an error, forgo those events.

Guard against null state (and the byproduct, a segfault), check if the
state is null before utilizing it.
This commit is contained in:
llmII 2021-09-03 23:22:07 -05:00
parent 4fb2d8d318
commit 1736c9b0f8
No known key found for this signature in database
GPG Key ID: E3AD2E259F58A9A0
1 changed files with 21 additions and 17 deletions

View File

@ -1702,24 +1702,28 @@ void janet_loop1_impl(int has_timeout, JanetTimestamp timeout) {
} else { } else {
JanetStream *stream = p; JanetStream *stream = p;
JanetListenerState *state = stream->state; JanetListenerState *state = stream->state;
state->event = events + i; if (NULL != state) {
JanetAsyncStatus statuses[4]; state->event = events + i;
for (int i = 0; i < 4; i++) JanetAsyncStatus statuses[4];
statuses[i] = JANET_ASYNC_STATUS_NOT_DONE; for (int i = 0; i < 4; i++)
statuses[i] = JANET_ASYNC_STATUS_NOT_DONE;
if (events[i].filter == EVFILT_WRITE) if (!(events[i].flags & EV_ERROR)) {
statuses[0] = state->machine(state, JANET_ASYNC_EVENT_WRITE); if (events[i].filter == EVFILT_WRITE)
if (events[i].filter == EVFILT_READ) statuses[0] = state->machine(state, JANET_ASYNC_EVENT_WRITE);
statuses[1] = state->machine(state, JANET_ASYNC_EVENT_READ); if (events[i].filter == EVFILT_READ)
if (events[i].flags & EV_ERROR) statuses[1] = state->machine(state, JANET_ASYNC_EVENT_READ);
statuses[2] = state->machine(state, JANET_ASYNC_EVENT_ERR); if ((events[i].flags & EV_EOF) && !(events[i].data > 0))
if ((events[i].flags & EV_EOF) && !(events[i].data > 0)) statuses[3] = state->machine(state, JANET_ASYNC_EVENT_HUP);
statuses[3] = state->machine(state, JANET_ASYNC_EVENT_HUP); } else {
if(statuses[0] == JANET_ASYNC_STATUS_DONE || statuses[2] = state->machine(state, JANET_ASYNC_EVENT_ERR);
statuses[1] == JANET_ASYNC_STATUS_DONE || }
statuses[2] == JANET_ASYNC_STATUS_DONE || if(statuses[0] == JANET_ASYNC_STATUS_DONE ||
statuses[3] == JANET_ASYNC_STATUS_DONE) statuses[1] == JANET_ASYNC_STATUS_DONE ||
janet_unlisten(state, 0); statuses[2] == JANET_ASYNC_STATUS_DONE ||
statuses[3] == JANET_ASYNC_STATUS_DONE)
janet_unlisten(state, 0);
}
} }
} }
} }