mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-18 11:31:00 -05:00
log_proxy_stderr: limit the length of Event Log lines.
If a proxy command jabbers on standard error in a way that doesn't involve any newline characters, we now won't keep buffering data for ever. (Not that I've heard of it happening, but I noticed the theoretical possibility on the way past in a recent cleanup pass.)
This commit is contained in:
101
be_misc.c
101
be_misc.c
@ -59,12 +59,15 @@ void backend_socket_log(Seat *seat, LogContext *logctx,
|
||||
}
|
||||
}
|
||||
|
||||
void log_proxy_stderr(Plug *plug, bufchain *buf, const void *vdata, size_t len)
|
||||
void psb_init(ProxyStderrBuf *psb)
|
||||
{
|
||||
psb->size = 0;
|
||||
}
|
||||
|
||||
void log_proxy_stderr(Plug *plug, ProxyStderrBuf *psb,
|
||||
const void *vdata, size_t len)
|
||||
{
|
||||
const char *data = (const char *)vdata;
|
||||
size_t pos = 0;
|
||||
const char *nlpos;
|
||||
char *msg, *fullmsg;
|
||||
|
||||
/*
|
||||
* This helper function allows us to collect the data written to a
|
||||
@ -72,43 +75,77 @@ void log_proxy_stderr(Plug *plug, bufchain *buf, const void *vdata, size_t len)
|
||||
* happen to get from its pipe, and whenever we have a complete
|
||||
* line, we pass it to plug_log.
|
||||
*
|
||||
* Prerequisites: a plug to log to, and a bufchain stored
|
||||
* somewhere to collect the data in.
|
||||
* (We also do this when the buffer in psb fills up, to avoid just
|
||||
* allocating more and more memory forever, and also to keep Event
|
||||
* Log lines reasonably bounded in size.)
|
||||
*
|
||||
* Prerequisites: a plug to log to, and a ProxyStderrBuf stored
|
||||
* somewhere to collect any not-yet-output partial line.
|
||||
*/
|
||||
|
||||
while (pos < len && (nlpos = memchr(data+pos, '\n', len-pos)) != NULL) {
|
||||
while (len > 0) {
|
||||
/*
|
||||
* Found a newline in the current input buffer. Append it to
|
||||
* the bufchain (which may contain a partial line from last
|
||||
* time).
|
||||
* Copy as much data into psb->buf as will fit.
|
||||
*/
|
||||
bufchain_add(buf, data + pos, nlpos - (data + pos));
|
||||
assert(psb->size < lenof(psb->buf));
|
||||
size_t to_consume = lenof(psb->buf) - psb->size;
|
||||
if (to_consume > len)
|
||||
to_consume = len;
|
||||
memcpy(psb->buf + psb->size, data, to_consume);
|
||||
data += to_consume;
|
||||
len -= to_consume;
|
||||
psb->size += to_consume;
|
||||
|
||||
/*
|
||||
* Collect the resulting line of data and pass it to plug_log.
|
||||
* Output any full lines in psb->buf.
|
||||
*/
|
||||
size_t msglen = bufchain_size(buf);
|
||||
assert(msglen < ~(size_t)0);
|
||||
msg = snewn(msglen+1, char);
|
||||
bufchain_fetch(buf, msg, msglen);
|
||||
bufchain_consume(buf, msglen);
|
||||
while (msglen > 0 && (msg[msglen-1] == '\n' || msg[msglen-1] == '\r'))
|
||||
msglen--;
|
||||
msg[msglen] = '\0';
|
||||
fullmsg = dupprintf("proxy: %s", msg);
|
||||
plug_log(plug, 2, NULL, 0, fullmsg, 0);
|
||||
sfree(fullmsg);
|
||||
sfree(msg);
|
||||
size_t pos = 0;
|
||||
while (pos < psb->size) {
|
||||
char *nlpos = memchr(psb->buf + pos, '\n', psb->size - pos);
|
||||
if (!nlpos)
|
||||
break;
|
||||
|
||||
/*
|
||||
* Found a newline in the buffer, so we can output a line.
|
||||
*/
|
||||
size_t endpos = nlpos - psb->buf;
|
||||
while (endpos > pos && (psb->buf[endpos-1] == '\n' ||
|
||||
psb->buf[endpos-1] == '\r'))
|
||||
endpos--;
|
||||
char *msg = dupprintf(
|
||||
"proxy: %.*s", (int)(endpos - pos), psb->buf + pos);
|
||||
plug_log(plug, 2, NULL, 0, msg, 0);
|
||||
sfree(msg);
|
||||
|
||||
pos = nlpos - psb->buf + 1;
|
||||
assert(pos <= psb->size);
|
||||
}
|
||||
|
||||
/*
|
||||
* Advance past the newline.
|
||||
* If the buffer is completely full and we didn't output
|
||||
* anything, then output the whole thing, flagging it as a
|
||||
* truncated line.
|
||||
*/
|
||||
if (pos == 0 && psb->size == lenof(psb->buf)) {
|
||||
char *msg = dupprintf(
|
||||
"proxy (partial line): %.*s", (int)psb->size, psb->buf);
|
||||
plug_log(plug, 2, NULL, 0, msg, 0);
|
||||
sfree(msg);
|
||||
|
||||
pos = psb->size = 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Now move any remaining data up to the front of the buffer.
|
||||
*/
|
||||
size_t newsize = psb->size - pos;
|
||||
if (newsize)
|
||||
memmove(psb->buf, psb->buf + pos, newsize);
|
||||
psb->size = newsize;
|
||||
|
||||
/*
|
||||
* And loop round again if there's more data to be read from
|
||||
* our input.
|
||||
*/
|
||||
pos += nlpos+1 - (data + pos);
|
||||
}
|
||||
|
||||
/*
|
||||
* Now any remaining data is a partial line, which we save for
|
||||
* next time.
|
||||
*/
|
||||
bufchain_add(buf, data + pos, len - pos);
|
||||
}
|
||||
|
Reference in New Issue
Block a user