XQuartz: console_redirect: Set the correct location for reading into the buffer

Prior to this change, it was possible that a large message would have some
of its data prepended to subsequent messages due to our not incorrectly
setting the location to write into the buffer.

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
This commit is contained in:
Jeremy Huddleston Sequoia 2012-08-16 15:43:34 -07:00
parent ad5fe2d961
commit a32e01802f

View File

@ -110,27 +110,33 @@ _read_redirect(int fd, int flush)
/* Increment our returned number read */
total_read += nbytes;
nbytes += (aslr->w - aslr->buf);
aslr->buf[nbytes] = '\0';
/* Increment our write location */
aslr->w += nbytes;
aslr->w[0] = '\0';
/* One line at a time */
for (p = aslr->buf; *p && (p - aslr->buf) < nbytes; p = s + 1) {
for (p = aslr->buf; p < aslr->w; p = s + 1) {
// Find null or \n
for (s = p; *s && *s != '\n'; s++) ;
if (*s == '\n') {
*s = '\0';
}
if (s < aslr->w || aslr->buf == p) {
/* Either the first of multiple messages or one message which is larger than our buffer */
asl_log(aslr->asl, aslr->msg, aslr->level, "%s", p);
}
else if (aslr->buf != p) {
else {
/* We reached the end of the buffer, move this chunk to the start. */
memmove(aslr->buf, p, BUF_SIZE - (p - aslr->buf));
aslr->w = aslr->buf + (s - p);
break;
}
else if (nbytes == BUF_SIZE - 1) {
asl_log(aslr->asl, aslr->msg, aslr->level, "%s", p);
aslr->w = aslr->buf;
break;
}
}
if (p == aslr->w) {
/* Start writing at the beginning in the case where we flushed */
aslr->w = aslr->buf;
}
}