Check that QIODevice::open() is successful, and explicitly call close()

This commit is contained in:
Jonas Kvinge 2021-07-14 20:52:57 +02:00
parent f64c1dd9e5
commit 2eab763d74
15 changed files with 74 additions and 49 deletions

View File

@ -790,6 +790,7 @@ SongList CollectionWatcher::ScanNewFile(const QString &file, const QString &path
// Playlist parser for CUEs considers every entry in sheet valid and we don't want invalid media getting into collection!
QString file_nfd = file.normalized(QString::NormalizationForm_D);
SongList cue_congs = cue_parser_->Load(&cue_file, matching_cue, path, false);
cue_file.close();
songs.reserve(cue_congs.count());
for (Song &cue_song : cue_congs) {
cue_song.set_source(source_);

View File

@ -340,11 +340,11 @@ bool CommandlineOptions::contains_play_options() const {
QByteArray CommandlineOptions::Serialize() const {
QBuffer buf;
buf.open(QIODevice::WriteOnly);
QDataStream s(&buf);
s << *this;
buf.close();
if (buf.open(QIODevice::WriteOnly)) {
QDataStream s(&buf);
s << *this;
buf.close();
}
return buf.data().toBase64();
@ -354,10 +354,10 @@ void CommandlineOptions::Load(const QByteArray &serialized) {
QByteArray copy = QByteArray::fromBase64(serialized);
QBuffer buf(&copy);
buf.open(QIODevice::ReadOnly);
QDataStream s(&buf);
s >> *this;
if (buf.open(QIODevice::ReadOnly)) {
QDataStream s(&buf);
s >> *this;
}
}

View File

@ -387,9 +387,12 @@ void Database::ExecSchemaCommandsFromFile(QSqlDatabase &db, const QString &filen
// Open and read the database schema
QFile schema_file(filename);
if (!schema_file.open(QIODevice::ReadOnly))
if (!schema_file.open(QIODevice::ReadOnly)) {
qFatal("Couldn't open schema file %s", filename.toUtf8().constData());
return;
}
ExecSchemaCommands(db, QString::fromUtf8(schema_file.readAll()), schema_version, in_transaction);
schema_file.close();
}

View File

@ -358,10 +358,13 @@ void SongLoader::EffectiveSongLoad(Song *song) {
}
void SongLoader::LoadPlaylist(ParserBase *parser, const QString &filename) {
QFile file(filename);
file.open(QIODevice::ReadOnly);
songs_ = parser->Load(&file, filename, QFileInfo(filename).path());
file.close();
if (file.open(QIODevice::ReadOnly)) {
songs_ = parser->Load(&file, filename, QFileInfo(filename).path());
file.close();
}
}
static bool CompareSongs(const Song &left, const Song &right) {
@ -429,9 +432,10 @@ void SongLoader::StopTypefind() {
// Parse the playlist
QBuffer buf(&buffer_);
buf.open(QIODevice::ReadOnly);
songs_ = parser_->Load(&buf);
buf.close();
if (buf.open(QIODevice::ReadOnly)) {
songs_ = parser_->Load(&buf);
buf.close();
}
}
else if (success_) {

View File

@ -341,8 +341,9 @@ void LocalRedirectServer::ReadyRead() {
void LocalRedirectServer::WriteTemplate() const {
QFile page_file(":/html/oauthsuccess.html");
page_file.open(QIODevice::ReadOnly);
if (!page_file.open(QIODevice::ReadOnly)) return;
QString page_data = QString::fromUtf8(page_file.readAll());
page_file.close();
QRegularExpression tr_regexp("tr\\(\"([^\"]+)\"\\)");
int offset = 0;
@ -359,13 +360,15 @@ void LocalRedirectServer::WriteTemplate() const {
}
QBuffer image_buffer;
image_buffer.open(QIODevice::ReadWrite);
QApplication::style()
->standardIcon(QStyle::SP_DialogOkButton)
.pixmap(16)
.toImage()
.save(&image_buffer, "PNG");
page_data.replace("@IMAGE_DATA@", image_buffer.data().toBase64());
if (image_buffer.open(QIODevice::ReadWrite)) {
QApplication::style()
->standardIcon(QStyle::SP_DialogOkButton)
.pixmap(16)
.toImage()
.save(&image_buffer, "PNG");
page_data.replace("@IMAGE_DATA@", image_buffer.data().toBase64());
image_buffer.close();
}
socket_->write("HTTP/1.0 200 OK\r\n");
socket_->write("Content-type: text/html;charset=UTF-8\r\n");

View File

@ -222,7 +222,9 @@ int main(int argc, char *argv[]) {
// Create the file if it doesn't exist already
if (!QFile::exists(s.fileName())) {
QFile file(s.fileName());
file.open(QIODevice::WriteOnly);
if (file.open(QIODevice::WriteOnly)) {
file.close();
}
}
// Set -rw-------

View File

@ -113,6 +113,7 @@ MoodbarLoader::Result MoodbarLoader::Load(const QUrl &url, QByteArray *data, Moo
if (f.open(QIODevice::ReadOnly)) {
qLog(Info) << "Loading moodbar data from" << possible_mood_file;
*data = f.readAll();
f.close();
return Loaded;
}
}
@ -184,6 +185,7 @@ void MoodbarLoader::RequestFinished(MoodbarPipeline *request, const QUrl &url) {
QFile mood_file(mood_filename);
if (mood_file.open(QIODevice::WriteOnly)) {
mood_file.write(request->data());
mood_file.close();
#ifdef Q_OS_WIN32
if (!SetFileAttributes((LPCTSTR)mood_filename.utf16(), FILE_ATTRIBUTE_HIDDEN)) {

View File

@ -1240,7 +1240,7 @@ QMimeData *Playlist::mimeData(const QModelIndexList &indexes) const {
}
QBuffer buf;
buf.open(QIODevice::WriteOnly);
if (!buf.open(QIODevice::WriteOnly)) return nullptr;
QDataStream stream(&buf);
const Playlist *self = this;

View File

@ -294,6 +294,7 @@ PlaylistItemPtr PlaylistBackend::RestoreCueData(PlaylistItemPtr item, std::share
if (!cue_file.open(QIODevice::ReadOnly)) return item;
song_list = cue_parser.Load(&cue_file, cue_path, QDir(cue_path.section('/', 0, -2)));
cue_file.close();
state->cached_cues_[cue_path] = song_list;
}
else {

View File

@ -63,15 +63,15 @@ SongList ASXParser::Load(QIODevice *device, const QString &playlist_path, const
}
QBuffer buffer(&data);
buffer.open(QIODevice::ReadOnly);
SongList ret;
if (!buffer.open(QIODevice::ReadOnly)) return SongList();
QXmlStreamReader reader(&buffer);
if (!Utilities::ParseUntilElementCI(&reader, "asx")) {
return ret;
buffer.close();
return SongList();
}
SongList ret;
while (!reader.atEnd() && Utilities::ParseUntilElementCI(&reader, "entry")) {
Song song = ParseTrack(&reader, dir, collection_search);
if (song.is_valid()) {
@ -79,6 +79,8 @@ SongList ASXParser::Load(QIODevice *device, const QString &playlist_path, const
}
}
buffer.close();
return ret;
}

View File

@ -46,8 +46,6 @@ SongList M3UParser::Load(QIODevice *device, const QString &playlist_path, const
Q_UNUSED(playlist_path);
SongList ret;
M3UType type = STANDARD;
Metadata current_metadata;
@ -56,7 +54,7 @@ SongList M3UParser::Load(QIODevice *device, const QString &playlist_path, const
data.replace("\n\n", "\n");
QByteArray bytes = data.toUtf8();
QBuffer buffer(&bytes);
buffer.open(QIODevice::ReadOnly);
if (!buffer.open(QIODevice::ReadOnly)) return SongList();
QString line = QString::fromUtf8(buffer.readLine()).trimmed();
if (line.startsWith("#EXTM3U")) {
@ -65,6 +63,7 @@ SongList M3UParser::Load(QIODevice *device, const QString &playlist_path, const
line = QString::fromUtf8(buffer.readLine()).trimmed();
}
SongList ret;
forever {
if (line.startsWith('#')) {
// Extended info or comment.
@ -95,6 +94,8 @@ SongList M3UParser::Load(QIODevice *device, const QString &playlist_path, const
line = QString::fromUtf8(buffer.readLine()).trimmed();
}
buffer.close();
return ret;
}

View File

@ -166,9 +166,12 @@ SongList PlaylistParser::LoadFromFile(const QString &filename) const {
// Open the file
QFile file(filename);
file.open(QIODevice::ReadOnly);
if (!file.open(QIODevice::ReadOnly)) return SongList();
return parser->Load(&file, filename, info.absolutePath());
SongList ret = parser->Load(&file, filename, info.absolutePath());
file.close();
return ret;
}

View File

@ -342,12 +342,12 @@ QMimeData *Queue::mimeData(const QModelIndexList &indexes) const {
}
QBuffer buf;
buf.open(QIODevice::WriteOnly);
QDataStream stream(&buf);
stream << rows;
buf.close();
data->setData(kRowsMimetype, buf.data());
if (buf.open(QIODevice::WriteOnly)) {
QDataStream stream(&buf);
stream << rows;
buf.close();
data->setData(kRowsMimetype, buf.data());
}
return data;

View File

@ -120,6 +120,7 @@ ContextSettingsPage::ContextSettingsPage(SettingsDialog *dialog, QWidget *parent
QString text = file.readAll();
ui_->preview_headline->setText(text);
ui_->preview_normal->setText(text);
file.close();
}
}

View File

@ -124,14 +124,16 @@ SmartPlaylistSearchTermWidget::SmartPlaylistSearchTermWidget(CollectionBackend *
// Set stylesheet
QFile stylesheet_file(":/style/smartplaylistsearchterm.css");
stylesheet_file.open(QIODevice::ReadOnly);
QString stylesheet = QString::fromLatin1(stylesheet_file.readAll());
const QColor base(222, 97, 97, 128);
stylesheet.replace("%light2", Utilities::ColorToRgba(base.lighter(140)));
stylesheet.replace("%light", Utilities::ColorToRgba(base.lighter(120)));
stylesheet.replace("%dark", Utilities::ColorToRgba(base.darker(120)));
stylesheet.replace("%base", Utilities::ColorToRgba(base));
setStyleSheet(stylesheet);
if (stylesheet_file.open(QIODevice::ReadOnly)) {
QString stylesheet = QString::fromLatin1(stylesheet_file.readAll());
stylesheet_file.close();
const QColor base(222, 97, 97, 128);
stylesheet.replace("%light2", Utilities::ColorToRgba(base.lighter(140)));
stylesheet.replace("%light", Utilities::ColorToRgba(base.lighter(120)));
stylesheet.replace("%dark", Utilities::ColorToRgba(base.darker(120)));
stylesheet.replace("%base", Utilities::ColorToRgba(base));
setStyleSheet(stylesheet);
}
}