From 16b31985c3245d5cb3be26f3cc0aa06bc6acb139 Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Tue, 20 Jul 2021 07:45:31 +0200 Subject: cleanup: replace membuffer by membufferpp in C-code Thus, the membuffer data is automatically freed when going out of scope - one thing less to worry about. This fixes one use-after-free bug in uploadDiveLogsDE.cpp and one extremely questionable practice in divetooltipitem.cpp: The membuffer was a shared instance across all instances of the DiveToolTipItem. Remves unnecessary #include directives in files that didn't even use membuffer. Signed-off-by: Berthold Stoeger --- backend-shared/exportfuncs.cpp | 6 ++---- core/divesitehelpers.cpp | 1 - core/qthelper.cpp | 1 - core/uploadDiveLogsDE.cpp | 9 +++------ core/uploadDiveShare.cpp | 3 +-- desktop-widgets/subsurfacewebservices.cpp | 1 - desktop-widgets/tab-widgets/TabDivePhotos.cpp | 5 ++--- mobile-widgets/qmlmanager.cpp | 1 - profile-widget/divetooltipitem.cpp | 3 +-- 9 files changed, 9 insertions(+), 21 deletions(-) diff --git a/backend-shared/exportfuncs.cpp b/backend-shared/exportfuncs.cpp index 7f15886db..8bcdfdcb0 100644 --- a/backend-shared/exportfuncs.cpp +++ b/backend-shared/exportfuncs.cpp @@ -65,7 +65,7 @@ void export_TeX(const char *filename, bool selected_only, bool plain, ExportCall int i; bool need_pagebreak = false; - struct membuffer buf = {}; + struct membufferpp buf; if (plain) { ssrf = ""; @@ -253,7 +253,6 @@ void export_TeX(const char *filename, bool selected_only, bool plain, ExportCall flush_buffer(&buf, f); /*check for writing errors? */ fclose(f); } - free_buffer(&buf); cb.setProgress(1000); } @@ -265,7 +264,7 @@ void export_depths(const char *filename, bool selected_only) int i; const char *unit = NULL; - struct membuffer buf = {}; + struct membufferpp buf; for_each_dive (i, dive) { if (selected_only && !dive->selected) @@ -291,7 +290,6 @@ void export_depths(const char *filename, bool selected_only) flush_buffer(&buf, f); /*check for writing errors? */ fclose(f); } - free_buffer(&buf); } #endif /* ! SUBSURFACE_MOBILE */ diff --git a/core/divesitehelpers.cpp b/core/divesitehelpers.cpp index bb4764274..5a36d63ca 100644 --- a/core/divesitehelpers.cpp +++ b/core/divesitehelpers.cpp @@ -9,7 +9,6 @@ #include "errorhelper.h" #include "subsurface-string.h" #include "qthelper.h" -#include "membuffer.h" #include #include #include diff --git a/core/qthelper.cpp b/core/qthelper.cpp index 185e46bbc..d14276031 100644 --- a/core/qthelper.cpp +++ b/core/qthelper.cpp @@ -7,7 +7,6 @@ #include "subsurface-string.h" #include "gettextfromc.h" #include "statistics.h" -#include "membuffer.h" #include "version.h" #include "errorhelper.h" #include "planner.h" diff --git a/core/uploadDiveLogsDE.cpp b/core/uploadDiveLogsDE.cpp index 6dc764ed1..980800149 100644 --- a/core/uploadDiveLogsDE.cpp +++ b/core/uploadDiveLogsDE.cpp @@ -97,7 +97,7 @@ bool uploadDiveLogsDE::prepareDives(const QString &tempfile, bool selected) const char *membuf; xmlDoc *transformed; struct zip_source *s; - struct membuffer mb = {}; + struct membufferpp mb; struct xml_params *params = alloc_xml_params(); /* @@ -109,9 +109,7 @@ bool uploadDiveLogsDE::prepareDives(const QString &tempfile, bool selected) continue; } - /* make sure the buffer is empty and add the dive */ - mb.len = 0; - + /* add the dive */ struct dive_site *ds = dive->dive_site; if (ds) { @@ -140,7 +138,7 @@ bool uploadDiveLogsDE::prepareDives(const QString &tempfile, bool selected) put_format(&mb, "\n"); } membuf = mb_cstring(&mb); - streamsize = strlen(membuf); + streamsize = mb.len; /* * Parse the memory buffer into XML document and * transform it to divelogs.de format, finally dumping @@ -156,7 +154,6 @@ bool uploadDiveLogsDE::prepareDives(const QString &tempfile, bool selected) free_xml_params(params); return false; } - free_buffer(&mb); xml_params_add_int(params, "allcylinders", prefs.display_unused_tanks); transformed = xsltApplyStylesheet(xslt, doc, xml_params_get(params)); diff --git a/core/uploadDiveShare.cpp b/core/uploadDiveShare.cpp index 8d5858e00..87c3d0b24 100644 --- a/core/uploadDiveShare.cpp +++ b/core/uploadDiveShare.cpp @@ -27,10 +27,9 @@ uploadDiveShare::uploadDiveShare(): void uploadDiveShare::doUpload(bool selected, const QString &uid, bool noPublic) { //generate json - struct membuffer buf = {}; + struct membufferpp buf; export_list(&buf, NULL, selected, false); QByteArray json_data(buf.buffer, buf.len); - free_buffer(&buf); //Request to server QNetworkRequest request; diff --git a/desktop-widgets/subsurfacewebservices.cpp b/desktop-widgets/subsurfacewebservices.cpp index af171e3ba..34efd098e 100644 --- a/desktop-widgets/subsurfacewebservices.cpp +++ b/desktop-widgets/subsurfacewebservices.cpp @@ -13,7 +13,6 @@ #include "desktop-widgets/mapwidget.h" #include "desktop-widgets/tab-widgets/maintab.h" #include "core/selection.h" -#include "core/membuffer.h" #include "core/cloudstorage.h" #include "core/subsurface-string.h" #include "core/uploadDiveLogsDE.h" diff --git a/desktop-widgets/tab-widgets/TabDivePhotos.cpp b/desktop-widgets/tab-widgets/TabDivePhotos.cpp index 03351184f..87203c74c 100644 --- a/desktop-widgets/tab-widgets/TabDivePhotos.cpp +++ b/desktop-widgets/tab-widgets/TabDivePhotos.cpp @@ -128,13 +128,12 @@ void TabDivePhotos::saveSubtitles() // Only videos have non-zero duration if (!duration) continue; - struct membuffer b = { 0 }; + struct membufferpp b; save_subtitles_buffer(&b, current_dive, offset, duration); - char *data = detach_cstring(&b); + const char *data = mb_cstring(&b); subtitlefile.open(QIODevice::WriteOnly); subtitlefile.write(data, strlen(data)); subtitlefile.close(); - free(data); } } } diff --git a/mobile-widgets/qmlmanager.cpp b/mobile-widgets/qmlmanager.cpp index 878d4618c..d3ea320c2 100644 --- a/mobile-widgets/qmlmanager.cpp +++ b/mobile-widgets/qmlmanager.cpp @@ -34,7 +34,6 @@ #include "core/qt-gui.h" #include "core/git-access.h" #include "core/cloudstorage.h" -#include "core/membuffer.h" #include "core/downloadfromdcthread.h" #include "core/subsurfacestartup.h" // for ignore_bt flag #include "core/subsurface-string.h" diff --git a/profile-widget/divetooltipitem.cpp b/profile-widget/divetooltipitem.cpp index 5cc6eecad..3a0a4de58 100644 --- a/profile-widget/divetooltipitem.cpp +++ b/profile-widget/divetooltipitem.cpp @@ -221,7 +221,7 @@ void ToolTipItem::setTimeAxis(DiveCartesianAxis *axis) void ToolTipItem::refresh(const dive *d, const QPointF &pos, bool inPlanner) { - static struct membuffer mb = {}; + struct membufferpp mb; if(refreshTime.elapsed() < 40) return; @@ -232,7 +232,6 @@ void ToolTipItem::refresh(const dive *d, const QPointF &pos, bool inPlanner) lastTime = time; clear(); - mb.len = 0; int idx = get_plot_details_new(d, &pInfo, time, &mb); tissues.fill(); -- cgit v1.2.3-70-g09d2