summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/io/qdatastream.cpp55
-rw-r--r--src/corelib/io/qdatastream.h3
-rw-r--r--tests/auto/qdatastream/tst_qdatastream.cpp51
3 files changed, 89 insertions, 20 deletions
diff --git a/src/corelib/io/qdatastream.cpp b/src/corelib/io/qdatastream.cpp
index dffb3c6..52ee252 100644
--- a/src/corelib/io/qdatastream.cpp
+++ b/src/corelib/io/qdatastream.cpp
@@ -243,6 +243,11 @@ QT_BEGIN_NAMESPACE
}
#endif
+#define CHECK_STREAM_WRITE_PRECOND(retVal) \
+ CHECK_STREAM_PRECOND(retVal) \
+ if (q_status != Ok) \
+ return retVal;
+
enum {
DefaultStreamVersion = QDataStream::Qt_4_6
};
@@ -995,8 +1000,9 @@ int QDataStream::readRawData(char *s, int len)
QDataStream &QDataStream::operator<<(qint8 i)
{
- CHECK_STREAM_PRECOND(*this)
- dev->putChar(i);
+ CHECK_STREAM_WRITE_PRECOND(*this)
+ if (!dev->putChar(i))
+ q_status = WriteFailed;
return *this;
}
@@ -1018,11 +1024,12 @@ QDataStream &QDataStream::operator<<(qint8 i)
QDataStream &QDataStream::operator<<(qint16 i)
{
- CHECK_STREAM_PRECOND(*this)
+ CHECK_STREAM_WRITE_PRECOND(*this)
if (!noswap) {
i = qbswap(i);
}
- dev->write((char *)&i, sizeof(qint16));
+ if (dev->write((char *)&i, sizeof(qint16)) != sizeof(qint16))
+ q_status = WriteFailed;
return *this;
}
@@ -1035,11 +1042,12 @@ QDataStream &QDataStream::operator<<(qint16 i)
QDataStream &QDataStream::operator<<(qint32 i)
{
- CHECK_STREAM_PRECOND(*this)
+ CHECK_STREAM_WRITE_PRECOND(*this)
if (!noswap) {
i = qbswap(i);
}
- dev->write((char *)&i, sizeof(qint32));
+ if (dev->write((char *)&i, sizeof(qint32)) != sizeof(qint32))
+ q_status = WriteFailed;
return *this;
}
@@ -1060,7 +1068,7 @@ QDataStream &QDataStream::operator<<(qint32 i)
QDataStream &QDataStream::operator<<(qint64 i)
{
- CHECK_STREAM_PRECOND(*this)
+ CHECK_STREAM_WRITE_PRECOND(*this)
if (version() < 6) {
quint32 i1 = i & 0xffffffff;
quint32 i2 = i >> 32;
@@ -1069,7 +1077,8 @@ QDataStream &QDataStream::operator<<(qint64 i)
if (!noswap) {
i = qbswap(i);
}
- dev->write((char *)&i, sizeof(qint64));
+ if (dev->write((char *)&i, sizeof(qint64)) != sizeof(qint64))
+ q_status = WriteFailed;
}
return *this;
}
@@ -1089,8 +1098,9 @@ QDataStream &QDataStream::operator<<(qint64 i)
QDataStream &QDataStream::operator<<(bool i)
{
- CHECK_STREAM_PRECOND(*this)
- dev->putChar(qint8(i));
+ CHECK_STREAM_WRITE_PRECOND(*this)
+ if (!dev->putChar(qint8(i)))
+ q_status = WriteFailed;
return *this;
}
@@ -1111,7 +1121,7 @@ QDataStream &QDataStream::operator<<(float f)
return *this;
}
- CHECK_STREAM_PRECOND(*this)
+ CHECK_STREAM_WRITE_PRECOND(*this)
float g = f; // fixes float-on-stack problem
if (!noswap) {
union {
@@ -1122,7 +1132,8 @@ QDataStream &QDataStream::operator<<(float f)
x.val2 = qbswap(x.val2);
g = x.val1;
}
- dev->write((char *)&g, sizeof(float));
+ if (dev->write((char *)&g, sizeof(float)) != sizeof(float))
+ q_status = WriteFailed;
return *this;
}
@@ -1144,10 +1155,11 @@ QDataStream &QDataStream::operator<<(double f)
return *this;
}
- CHECK_STREAM_PRECOND(*this)
+ CHECK_STREAM_WRITE_PRECOND(*this)
#ifndef Q_DOUBLE_FORMAT
if (noswap) {
- dev->write((char *)&f, sizeof(double));
+ if (dev->write((char *)&f, sizeof(double)) != sizeof(double))
+ q_status = WriteFailed;
} else {
union {
double val1;
@@ -1155,7 +1167,8 @@ QDataStream &QDataStream::operator<<(double f)
} x;
x.val1 = f;
x.val2 = qbswap(x.val2);
- dev->write((char *)&x.val2, sizeof(double));
+ if (dev->write((char *)&x.val2, sizeof(double)) != sizeof(double))
+ q_status = WriteFailed;
}
#else
union {
@@ -1184,7 +1197,8 @@ QDataStream &QDataStream::operator<<(double f)
b[Q_DF(1)] = *p++;
b[Q_DF(0)] = *p;
}
- dev->write(b, 8);
+ if (dev->write(b, 8) != 8)
+ q_status = WriteFailed;
#endif
return *this;
}
@@ -1224,7 +1238,7 @@ QDataStream &QDataStream::operator<<(const char *s)
QDataStream &QDataStream::writeBytes(const char *s, uint len)
{
- CHECK_STREAM_PRECOND(*this)
+ CHECK_STREAM_WRITE_PRECOND(*this)
*this << (quint32)len; // write length specifier
if (len)
writeRawData(s, len);
@@ -1242,8 +1256,11 @@ QDataStream &QDataStream::writeBytes(const char *s, uint len)
int QDataStream::writeRawData(const char *s, int len)
{
- CHECK_STREAM_PRECOND(-1)
- return dev->write(s, len);
+ CHECK_STREAM_WRITE_PRECOND(-1)
+ int ret = dev->write(s, len);
+ if (ret != len)
+ q_status = WriteFailed;
+ return ret;
}
/*!
diff --git a/src/corelib/io/qdatastream.h b/src/corelib/io/qdatastream.h
index 774c4bc..05248ac 100644
--- a/src/corelib/io/qdatastream.h
+++ b/src/corelib/io/qdatastream.h
@@ -101,7 +101,8 @@ public:
enum Status {
Ok,
ReadPastEnd,
- ReadCorruptData
+ ReadCorruptData,
+ WriteFailed
};
enum FloatingPointPrecision {
diff --git a/tests/auto/qdatastream/tst_qdatastream.cpp b/tests/auto/qdatastream/tst_qdatastream.cpp
index c03bc71..898fb84 100644
--- a/tests/auto/qdatastream/tst_qdatastream.cpp
+++ b/tests/auto/qdatastream/tst_qdatastream.cpp
@@ -168,6 +168,8 @@ private slots:
void stream_atEnd_data();
void stream_atEnd();
+ void stream_writeError();
+
void stream_QByteArray2();
void setVersion_data();
@@ -2345,6 +2347,55 @@ void tst_QDataStream::stream_atEnd()
}
}
+class FakeBuffer : public QBuffer
+{
+protected:
+ qint64 writeData(const char *c, qint64 i) { return m_lock ? 0 : QBuffer::writeData(c, i); }
+public:
+ FakeBuffer(bool locked = false) : m_lock(locked) {}
+ void setLocked(bool locked) { m_lock = locked; }
+private:
+ bool m_lock;
+};
+
+#define TEST_WRITE_ERROR(op) \
+ { \
+ FakeBuffer fb(false); \
+ QVERIFY(fb.open(QBuffer::ReadWrite)); \
+ QDataStream fs(&fb); \
+ fs.writeRawData("hello", 5); \
+ /* first write some initial content */ \
+ QCOMPARE(fs.status(), QDataStream::Ok); \
+ QCOMPARE(fb.data(), QByteArray("hello")); \
+ /* then test that writing can cause an error */ \
+ fb.setLocked(true); \
+ fs op; \
+ QCOMPARE(fs.status(), QDataStream::WriteFailed); \
+ QCOMPARE(fb.data(), QByteArray("hello")); \
+ /* finally test that writing after an error doesn't change the stream any more */ \
+ fb.setLocked(false); \
+ fs op; \
+ QCOMPARE(fs.status(), QDataStream::WriteFailed); \
+ QCOMPARE(fb.data(), QByteArray("hello")); \
+ }
+
+void tst_QDataStream::stream_writeError()
+{
+ TEST_WRITE_ERROR(<< true)
+ TEST_WRITE_ERROR(<< (qint8)1)
+ TEST_WRITE_ERROR(<< (quint8)1)
+ TEST_WRITE_ERROR(<< (qint16)1)
+ TEST_WRITE_ERROR(<< (quint16)1)
+ TEST_WRITE_ERROR(<< (qint32)1)
+ TEST_WRITE_ERROR(<< (quint32)1)
+ TEST_WRITE_ERROR(<< (qint64)1)
+ TEST_WRITE_ERROR(<< (quint64)1)
+ TEST_WRITE_ERROR(<< "hello")
+ TEST_WRITE_ERROR(<< (float)1.0)
+ TEST_WRITE_ERROR(<< (double)1.0)
+ TEST_WRITE_ERROR(.writeRawData("test", 4))
+}
+
void tst_QDataStream::stream_QByteArray2()
{
QByteArray ba;