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 <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-07-20 07:45:31 +02:00 committed by Dirk Hohndel
parent f142e9a9c6
commit 16b31985c3
9 changed files with 9 additions and 21 deletions

View file

@ -65,7 +65,7 @@ void export_TeX(const char *filename, bool selected_only, bool plain, ExportCall
int i; int i;
bool need_pagebreak = false; bool need_pagebreak = false;
struct membuffer buf = {}; struct membufferpp buf;
if (plain) { if (plain) {
ssrf = ""; 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? */ flush_buffer(&buf, f); /*check for writing errors? */
fclose(f); fclose(f);
} }
free_buffer(&buf);
cb.setProgress(1000); cb.setProgress(1000);
} }
@ -265,7 +264,7 @@ void export_depths(const char *filename, bool selected_only)
int i; int i;
const char *unit = NULL; const char *unit = NULL;
struct membuffer buf = {}; struct membufferpp buf;
for_each_dive (i, dive) { for_each_dive (i, dive) {
if (selected_only && !dive->selected) 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? */ flush_buffer(&buf, f); /*check for writing errors? */
fclose(f); fclose(f);
} }
free_buffer(&buf);
} }
#endif /* ! SUBSURFACE_MOBILE */ #endif /* ! SUBSURFACE_MOBILE */

View file

@ -9,7 +9,6 @@
#include "errorhelper.h" #include "errorhelper.h"
#include "subsurface-string.h" #include "subsurface-string.h"
#include "qthelper.h" #include "qthelper.h"
#include "membuffer.h"
#include <QDebug> #include <QDebug>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonArray> #include <QJsonArray>

View file

@ -7,7 +7,6 @@
#include "subsurface-string.h" #include "subsurface-string.h"
#include "gettextfromc.h" #include "gettextfromc.h"
#include "statistics.h" #include "statistics.h"
#include "membuffer.h"
#include "version.h" #include "version.h"
#include "errorhelper.h" #include "errorhelper.h"
#include "planner.h" #include "planner.h"

View file

@ -97,7 +97,7 @@ bool uploadDiveLogsDE::prepareDives(const QString &tempfile, bool selected)
const char *membuf; const char *membuf;
xmlDoc *transformed; xmlDoc *transformed;
struct zip_source *s; struct zip_source *s;
struct membuffer mb = {}; struct membufferpp mb;
struct xml_params *params = alloc_xml_params(); struct xml_params *params = alloc_xml_params();
/* /*
@ -109,9 +109,7 @@ bool uploadDiveLogsDE::prepareDives(const QString &tempfile, bool selected)
continue; continue;
} }
/* make sure the buffer is empty and add the dive */ /* add the dive */
mb.len = 0;
struct dive_site *ds = dive->dive_site; struct dive_site *ds = dive->dive_site;
if (ds) { if (ds) {
@ -140,7 +138,7 @@ bool uploadDiveLogsDE::prepareDives(const QString &tempfile, bool selected)
put_format(&mb, "</divelog>\n"); put_format(&mb, "</divelog>\n");
} }
membuf = mb_cstring(&mb); membuf = mb_cstring(&mb);
streamsize = strlen(membuf); streamsize = mb.len;
/* /*
* Parse the memory buffer into XML document and * Parse the memory buffer into XML document and
* transform it to divelogs.de format, finally dumping * transform it to divelogs.de format, finally dumping
@ -156,7 +154,6 @@ bool uploadDiveLogsDE::prepareDives(const QString &tempfile, bool selected)
free_xml_params(params); free_xml_params(params);
return false; return false;
} }
free_buffer(&mb);
xml_params_add_int(params, "allcylinders", prefs.display_unused_tanks); xml_params_add_int(params, "allcylinders", prefs.display_unused_tanks);
transformed = xsltApplyStylesheet(xslt, doc, xml_params_get(params)); transformed = xsltApplyStylesheet(xslt, doc, xml_params_get(params));

View file

@ -27,10 +27,9 @@ uploadDiveShare::uploadDiveShare():
void uploadDiveShare::doUpload(bool selected, const QString &uid, bool noPublic) void uploadDiveShare::doUpload(bool selected, const QString &uid, bool noPublic)
{ {
//generate json //generate json
struct membuffer buf = {}; struct membufferpp buf;
export_list(&buf, NULL, selected, false); export_list(&buf, NULL, selected, false);
QByteArray json_data(buf.buffer, buf.len); QByteArray json_data(buf.buffer, buf.len);
free_buffer(&buf);
//Request to server //Request to server
QNetworkRequest request; QNetworkRequest request;

View file

@ -13,7 +13,6 @@
#include "desktop-widgets/mapwidget.h" #include "desktop-widgets/mapwidget.h"
#include "desktop-widgets/tab-widgets/maintab.h" #include "desktop-widgets/tab-widgets/maintab.h"
#include "core/selection.h" #include "core/selection.h"
#include "core/membuffer.h"
#include "core/cloudstorage.h" #include "core/cloudstorage.h"
#include "core/subsurface-string.h" #include "core/subsurface-string.h"
#include "core/uploadDiveLogsDE.h" #include "core/uploadDiveLogsDE.h"

View file

@ -128,13 +128,12 @@ void TabDivePhotos::saveSubtitles()
// Only videos have non-zero duration // Only videos have non-zero duration
if (!duration) if (!duration)
continue; continue;
struct membuffer b = { 0 }; struct membufferpp b;
save_subtitles_buffer(&b, current_dive, offset, duration); save_subtitles_buffer(&b, current_dive, offset, duration);
char *data = detach_cstring(&b); const char *data = mb_cstring(&b);
subtitlefile.open(QIODevice::WriteOnly); subtitlefile.open(QIODevice::WriteOnly);
subtitlefile.write(data, strlen(data)); subtitlefile.write(data, strlen(data));
subtitlefile.close(); subtitlefile.close();
free(data);
} }
} }
} }

View file

@ -34,7 +34,6 @@
#include "core/qt-gui.h" #include "core/qt-gui.h"
#include "core/git-access.h" #include "core/git-access.h"
#include "core/cloudstorage.h" #include "core/cloudstorage.h"
#include "core/membuffer.h"
#include "core/downloadfromdcthread.h" #include "core/downloadfromdcthread.h"
#include "core/subsurfacestartup.h" // for ignore_bt flag #include "core/subsurfacestartup.h" // for ignore_bt flag
#include "core/subsurface-string.h" #include "core/subsurface-string.h"

View file

@ -221,7 +221,7 @@ void ToolTipItem::setTimeAxis(DiveCartesianAxis *axis)
void ToolTipItem::refresh(const dive *d, const QPointF &pos, bool inPlanner) void ToolTipItem::refresh(const dive *d, const QPointF &pos, bool inPlanner)
{ {
static struct membuffer mb = {}; struct membufferpp mb;
if(refreshTime.elapsed() < 40) if(refreshTime.elapsed() < 40)
return; return;
@ -232,7 +232,6 @@ void ToolTipItem::refresh(const dive *d, const QPointF &pos, bool inPlanner)
lastTime = time; lastTime = time;
clear(); clear();
mb.len = 0;
int idx = get_plot_details_new(d, &pInfo, time, &mb); int idx = get_plot_details_new(d, &pInfo, time, &mb);
tissues.fill(); tissues.fill();