From f080d232ef072976b28ca4b448dcb210b5941b2e Mon Sep 17 00:00:00 2001 From: Oswald Buddenhagen Date: Mon, 2 Nov 2009 22:30:09 +0100 Subject: fix canReadLine() erroneously returning true the indexOf() call did not consider actualReadBufferSize and thus scanned uninitialized memory for newlines. Reviewed-by: phartman --- src/corelib/tools/qringbuffer_p.h | 27 +++++++++++++++++++++++++++ src/network/socket/qlocalsocket_win.cpp | 3 ++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/corelib/tools/qringbuffer_p.h b/src/corelib/tools/qringbuffer_p.h index c44346c..7c766cb 100644 --- a/src/corelib/tools/qringbuffer_p.h +++ b/src/corelib/tools/qringbuffer_p.h @@ -287,6 +287,33 @@ public: return -1; } + inline int indexOf(char c, int maxLength) const { + int index = 0; + int remain = qMin(size(), maxLength); + for (int i = 0; remain && i < buffers.size(); ++i) { + int start = 0; + int end = buffers.at(i).size(); + + if (i == 0) + start = head; + if (i == tailBuffer) + end = tail; + if (remain < end - start) { + end = start + remain; + remain = 0; + } else { + remain -= end - start; + } + const char *ptr = buffers.at(i).data() + start; + for (int j = start; j < end; ++j) { + if (*ptr++ == c) + return index; + ++index; + } + } + return -1; + } + inline int read(char *data, int maxLength) { int bytesToRead = qMin(size(), maxLength); int readSoFar = 0; diff --git a/src/network/socket/qlocalsocket_win.cpp b/src/network/socket/qlocalsocket_win.cpp index 8a745ab..d812d88 100644 --- a/src/network/socket/qlocalsocket_win.cpp +++ b/src/network/socket/qlocalsocket_win.cpp @@ -363,7 +363,8 @@ bool QLocalSocket::canReadLine() const Q_D(const QLocalSocket); if (state() != ConnectedState) return false; - return (d->readBuffer.indexOf('\n') != -1 || QIODevice::canReadLine()); + return (QIODevice::canReadLine() + || d->readBuffer.indexOf('\n', d->actualReadBufferSize) != -1); } void QLocalSocket::close() -- cgit v0.12