mirror of
https://github.com/cuberite/libevent.git
synced 2025-09-08 20:07:56 -04:00
limit the amount of data bufferevents are going to consume to something
reasonable; in some circumstances it could happen that libevent happily allocated 100MB in read buffers without telling the user; found by christopher maxwell - parts of these changes are from his patch. svn:r200
This commit is contained in:
parent
f296e6336a
commit
7b78c82823
15
buffer.c
15
buffer.c
@ -340,8 +340,21 @@ evbuffer_read(struct evbuffer *buf, int fd, int howmuch)
|
||||
#endif
|
||||
|
||||
#ifdef FIONREAD
|
||||
if (ioctl(fd, FIONREAD, &n) == -1 || n == 0)
|
||||
if (ioctl(fd, FIONREAD, &n) == -1 || n == 0) {
|
||||
n = EVBUFFER_MAX_READ;
|
||||
} else if (n > EVBUFFER_MAX_READ && n > howmuch) {
|
||||
/*
|
||||
* It's possible that a lot of data is available for
|
||||
* reading. We do not want to exhaust resources
|
||||
* before the reader has a chance to do something
|
||||
* about it. If the reader does not tell us how much
|
||||
* data we should read, we artifically limit it.
|
||||
*/
|
||||
if (n > buf->totallen << 2)
|
||||
n = buf->totallen << 2;
|
||||
if (n < EVBUFFER_MAX_READ)
|
||||
n = EVBUFFER_MAX_READ;
|
||||
}
|
||||
#endif
|
||||
if (howmuch < 0 || howmuch > n)
|
||||
howmuch = n;
|
||||
|
10
evbuffer.c
10
evbuffer.c
@ -92,13 +92,21 @@ bufferevent_readcb(int fd, short event, void *arg)
|
||||
int res = 0;
|
||||
short what = EVBUFFER_READ;
|
||||
size_t len;
|
||||
int howmuch = -1;
|
||||
|
||||
if (event == EV_TIMEOUT) {
|
||||
what |= EVBUFFER_TIMEOUT;
|
||||
goto error;
|
||||
}
|
||||
|
||||
res = evbuffer_read(bufev->input, fd, -1);
|
||||
/*
|
||||
* If we have a high watermark configured then we don't want to
|
||||
* read more data than would make us reach the watermark.
|
||||
*/
|
||||
if (bufev->wm_read.high != 0)
|
||||
howmuch = bufev->wm_read.high;
|
||||
|
||||
res = evbuffer_read(bufev->input, fd, howmuch);
|
||||
if (res == -1) {
|
||||
if (errno == EAGAIN || errno == EINTR)
|
||||
goto reschedule;
|
||||
|
Loading…
x
Reference in New Issue
Block a user