From e8909dcfd0b673d42c745395d2303eea2a7cc726 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Thu, 7 Sep 2023 20:49:02 +0300 Subject: QSystemError: pass errno when calling stdString() errno should be queried immediately after a libc function call fails, calling it later on in QSystemError::stdString() may be too late. Part of the goal of this, and similar, changes is removing the default value of the stdString() method's parameter, to signify to users of that method that errno should be stored ASAP if there is an error. If there is any intervening code that may call a system/libc function store errno in a local int var, otherwise use it directly in the QSystemError::stdString() call, which takes by value. Task-number: QTBUG-115199 Change-Id: If3f601a023ed0014e260089771220668dad88be8 Reviewed-by: Thiago Macieira Reviewed-by: Edward Welbourne --- src/corelib/io/qfsfileengine.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'src/corelib/io/qfsfileengine.cpp') diff --git a/src/corelib/io/qfsfileengine.cpp b/src/corelib/io/qfsfileengine.cpp index 2c86e85dd5b..253e28cdd40 100644 --- a/src/corelib/io/qfsfileengine.cpp +++ b/src/corelib/io/qfsfileengine.cpp @@ -273,7 +273,7 @@ bool QFSFileEnginePrivate::openFh(QIODevice::OpenMode openMode, FILE *fh) if (ret != 0) { q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, - QSystemError::stdString()); + QSystemError::stdString(errno)); this->openMode = QIODevice::NotOpen; this->fh = nullptr; @@ -335,7 +335,7 @@ bool QFSFileEnginePrivate::openFd(QIODevice::OpenMode openMode, int fd) if (ret == -1) { q->setError(errno == EMFILE ? QFile::ResourceError : QFile::OpenError, - QSystemError::stdString()); + QSystemError::stdString(errno)); this->openMode = QIODevice::NotOpen; this->fd = -1; @@ -394,7 +394,7 @@ bool QFSFileEnginePrivate::closeFdFh() if (!flushed || !closed) { if (flushed) { // If not flushed, we want the flush error to fall through. - q->setError(QFile::UnspecifiedError, QSystemError::stdString()); + q->setError(QFile::UnspecifiedError, QSystemError::stdString(errno)); } return false; } @@ -446,7 +446,7 @@ bool QFSFileEnginePrivate::flushFh() if (ret != 0) { q->setError(errno == ENOSPC ? QFile::ResourceError : QFile::WriteError, - QSystemError::stdString()); + QSystemError::stdString(errno)); return false; } return true; @@ -561,14 +561,14 @@ bool QFSFileEnginePrivate::seekFdFh(qint64 pos) } while (ret != 0 && errno == EINTR); if (ret != 0) { - q->setError(QFile::ReadError, QSystemError::stdString()); + q->setError(QFile::ReadError, QSystemError::stdString(errno)); return false; } } else { // Unbuffered stdio mode. if (QT_LSEEK(fd, QT_OFF_T(pos), SEEK_SET) == -1) { + q->setError(QFile::PositionError, QSystemError::stdString(errno)); qWarning("QFile::at: Cannot set file position %lld", pos); - q->setError(QFile::PositionError, QSystemError::stdString()); return false; } } @@ -623,7 +623,7 @@ qint64 QFSFileEnginePrivate::readFdFh(char *data, qint64 len) size_t result; do { result = fread(data + readBytes, 1, size_t(len - readBytes), fh); - eof = feof(fh); + eof = feof(fh); // Doesn't change errno if (eof && result == 0) { // On OS X, this is needed, e.g., if a file was written to // through another stream since our last read. See test @@ -654,7 +654,7 @@ qint64 QFSFileEnginePrivate::readFdFh(char *data, qint64 len) if (!eof && readBytes == 0) { readBytes = -1; - q->setError(QFile::ReadError, QSystemError::stdString()); + q->setError(QFile::ReadError, QSystemError::stdString(errno)); } return readBytes; @@ -699,8 +699,8 @@ qint64 QFSFileEnginePrivate::readLineFdFh(char *data, qint64 maxlen) // does the same, so we'd get two '\0' at the end - passing maxlen + 1 // solves this. if (!fgets(data, int(maxlen + 1), fh)) { - if (!feof(fh)) - q->setError(QFile::ReadError, QSystemError::stdString()); + if (!feof(fh)) // Doesn't change errno + q->setError(QFile::ReadError, QSystemError::stdString(errno)); return -1; // error } @@ -777,7 +777,8 @@ qint64 QFSFileEnginePrivate::writeFdFh(const char *data, qint64 len) if (len && writtenBytes == 0) { writtenBytes = -1; - q->setError(errno == ENOSPC ? QFile::ResourceError : QFile::WriteError, QSystemError::stdString()); + q->setError(errno == ENOSPC ? QFile::ResourceError : QFile::WriteError, + QSystemError::stdString(errno)); } else { // reset the cached size, if any metaData.clearFlags(QFileSystemMetaData::SizeAttribute); -- cgit v1.2.3