From d64ba87edae62894ca74cfb9d6d8cdd88c213397 Mon Sep 17 00:00:00 2001 From: Paul LeoNerd Evans Date: Wed, 18 Jan 2012 10:36:50 +0000 Subject: [PATCH] Ensure that termkey_advisereadable() just fails with errno=ENOMEM rather than trying to read() zero bytes --- termkey.c | 6 ++++++ termkey_advisereadable.3 | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/termkey.c b/termkey.c index 7770d83..a55b106 100644 --- a/termkey.c +++ b/termkey.c @@ -949,6 +949,12 @@ TermKeyResult termkey_advisereadable(TermKey *tk) tk->buffstart = 0; } + /* Not expecting it ever to be greater but doesn't hurt to handle that */ + if(tk->buffcount >= tk->buffsize) { + errno = ENOMEM; + return TERMKEY_RES_ERROR; + } + retry: len = read(tk->fd, tk->buffer + tk->buffcount, tk->buffsize - tk->buffcount); diff --git a/termkey_advisereadable.3 b/termkey_advisereadable.3 index fe70125..06e0467 100644 --- a/termkey_advisereadable.3 +++ b/termkey_advisereadable.3 @@ -10,7 +10,7 @@ termkey_advisereadable \- read more bytes from the underlying terminal .sp Link with \fI-ltermkey\fP. .SH DESCRIPTION -\fBtermkey_advisereadable\fP() informs the instance that new input may be available on the underlying file descriptor and so it should call \fBread\fP(2) to obtain it. If at least one more byte was read it will return \fBTERMKEY_RES_AGAIN\fP to indicate it may be useful to call \fBtermkey_getkey\fP(3) again. If no more input was read then \fBTERMKEY_RES_NONE\fP is returned. +\fBtermkey_advisereadable\fP() informs the instance that new input may be available on the underlying file descriptor and so it should call \fBread\fP(2) to obtain it. If at least one more byte was read it will return \fBTERMKEY_RES_AGAIN\fP to indicate it may be useful to call \fBtermkey_getkey\fP(3) again. If no more input was read then \fBTERMKEY_RES_NONE\fP is returned. If there was no buffer space remaining, then \fBTERMKEY_RES_ERROR\fP is returned with \fIerrno\fP set to \fBENOMEM\fP. .PP This function, along with \fBtermkey_getkey\fP(3) make it possible to use the termkey instance in an asynchronous program. .PP