From 7f12c664d27676b223a40a31accbd236da03c647 Mon Sep 17 00:00:00 2001 From: Robin Gareus Date: Wed, 13 Apr 2022 17:56:49 +0200 Subject: [PATCH] Consistent checks for peakfile file descriptors This fixes a crash on windows, close(-1) or closing an already closed FD will abort the application (Save-As can trigger the issue in done_with_peakfile_writes). Note that g_open() returns -1 if an error occurred. However other negative number may still be a valid FD. --- libs/ardour/audiosource.cc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/libs/ardour/audiosource.cc b/libs/ardour/audiosource.cc index f50d6e607b..643c648e96 100644 --- a/libs/ardour/audiosource.cc +++ b/libs/ardour/audiosource.cc @@ -122,7 +122,7 @@ AudioSource::~AudioSource () cerr << "AudioSource destroyed with leftover peak data pending" << endl; } - if ((-1) != _peakfile_fd) { + if (-1 != _peakfile_fd) { close (_peakfile_fd); _peakfile_fd = -1; } @@ -828,7 +828,7 @@ int AudioSource::close_peakfile () { WriterLock lp (_lock); - if (_peakfile_fd >= 0) { + if (-1 != _peakfile_fd) { close (_peakfile_fd); _peakfile_fd = -1; } @@ -846,7 +846,7 @@ AudioSource::prepare_for_peakfile_writes () return -1; } - if ((_peakfile_fd = g_open (_peakpath.c_str(), O_CREAT|O_RDWR, 0664)) < 0) { + if ((_peakfile_fd = g_open (_peakpath.c_str(), O_CREAT|O_RDWR, 0664)) == -1) { error << string_compose(_("AudioSource: cannot open _peakpath (c) \"%1\" (%2)"), _peakpath, strerror (errno)) << endmsg; return -1; } @@ -857,7 +857,7 @@ void AudioSource::done_with_peakfile_writes (bool done) { if (_session.deletion_in_progress() || _session.peaks_cleanup_in_progres()) { - if (_peakfile_fd) { + if (-1 != _peakfile_fd) { close (_peakfile_fd); _peakfile_fd = -1; } @@ -868,8 +868,10 @@ AudioSource::done_with_peakfile_writes (bool done) compute_and_write_peaks (0, 0, 0, true, false, _FPP); } - close (_peakfile_fd); - _peakfile_fd = -1; + if (-1 != _peakfile_fd) { + close (_peakfile_fd); + _peakfile_fd = -1; + } if (done) { Glib::Threads::Mutex::Lock lm (_peaks_ready_lock); @@ -900,7 +902,7 @@ AudioSource::compute_and_write_peaks (Sample* buf, samplecnt_t first_sample, sam off_t first_peak_byte; boost::scoped_array buf2; - if (_peakfile_fd < 0) { + if (-1 == _peakfile_fd) { if (prepare_for_peakfile_writes ()) { return -1; } @@ -1079,7 +1081,7 @@ AudioSource::compute_and_write_peaks (Sample* buf, samplecnt_t first_sample, sam void AudioSource::truncate_peakfile () { - if (_peakfile_fd < 0) { + if (-1 == _peakfile_fd) { error << string_compose (_("programming error: %1"), "AudioSource::truncate_peakfile() called without open peakfile descriptor") << endmsg; return;