commit 5d6c2088a3eb3f06195f5ac576f9c4fd673fae3b from: Omar Polo date: Mon Jul 19 10:31:49 2021 UTC don't crash on invalid tab ids in handle_imsg_buf My original idea was to crash when receiving data for an invalid tab. This idea just doesn't hold well in an asynchronous context. If we change page before the current tab finished loading we issue tab_stop, but even after that we still could receive a IMSG_BUF for that old tab id. commit - 52b1043cbe49b88c7dc8b0baa3bb374206a59dd1 commit + 5d6c2088a3eb3f06195f5ac576f9c4fd673fae3b blob - 521e743886854c1cb162a20563cd4a7dfabc322f blob + 64512510844f0c1de085546ef2e52d3731b501b2 --- telescope.c +++ telescope.c @@ -136,7 +136,7 @@ die(void) } static struct tab * -tab_by_id(uint32_t id) +tab_by_id_try(uint32_t id) { struct tab *t; @@ -145,9 +145,19 @@ tab_by_id(uint32_t id) return t; } - die(); + return NULL; } +static struct tab * +tab_by_id(uint32_t id) +{ + struct tab *t; + + if ((t = tab_by_id_try(id)) == NULL) + die(); + return t; +} + static void handle_imsg_err(struct imsg *imsg, size_t datalen) { @@ -432,7 +442,16 @@ handle_imsg_buf(struct imsg *imsg, size_t datalen) int l; char *page, buf[FMT_SCALED_STRSIZE] = {0}; - tab = tab_by_id(imsg->hdr.peerid); + if ((tab = tab_by_id(imsg->hdr.peerid)) == NULL) { + /* + * Since messages between ui and net are asynchronous, + * we could be in a case where we sent a tab_stop + * command, but still receive data for the old id. My + * initial idea of crashing on invalid tab ids just + * doesn't scale well in an asynchronous world. + */ + return; + } tab->bytes += datalen; if (tab->fd == -1) {