From 6cce0dae838a08464661f56177c3474d4477e236 Mon Sep 17 00:00:00 2001 From: sudden6 Date: Wed, 31 Oct 2018 16:28:25 +0100 Subject: [PATCH] fix(toxoptions): handle nullptr in parameters Use the c-toxcore default settings in case we don't have own settings. --- src/core/toxoptions.cpp | 48 ++++++++++++++++++++++++----------------- 1 file changed, 28 insertions(+), 20 deletions(-) diff --git a/src/core/toxoptions.cpp b/src/core/toxoptions.cpp index 176fe58f6..ffc403818 100644 --- a/src/core/toxoptions.cpp +++ b/src/core/toxoptions.cpp @@ -3,12 +3,11 @@ #include "src/core/icoresettings.h" #include "src/core/toxlogger.h" +#include + #include #include -// TODO(sudden6): replace this constant with the function from toxcore 0.2.3 -static const int MAX_PROXY_ADDRESS_LENGTH = 255; - /** * @brief The ToxOptions class wraps the Tox_Options struct and the matching * proxy address data. This is needed to ensure both have equal lifetime and @@ -51,6 +50,31 @@ ToxOptions::operator Tox_Options*() std::unique_ptr ToxOptions::makeToxOptions(const QByteArray& savedata, const ICoreSettings* s) { + Tox_Options* tox_opts = tox_options_new(nullptr); + + if (!tox_opts) { + qWarning() << "failed to create Tox_Options"; + return {}; + } + + // need to init proxyAddr here, because we need it to construct ToxOptions + const QString proxyAddr = s == nullptr ? QString{} : s->getProxyAddr(); + + auto toxOptions = std::unique_ptr(new ToxOptions(tox_opts, proxyAddr.toUtf8())); + // register log first, to get messages as early as possible + tox_options_set_log_callback(*toxOptions, ToxLogger::onLogMessage); + + // savedata + tox_options_set_savedata_type(*toxOptions, savedata.isNull() ? TOX_SAVEDATA_TYPE_NONE + : TOX_SAVEDATA_TYPE_TOX_SAVE); + tox_options_set_savedata_data(*toxOptions, reinterpret_cast(savedata.data()), + savedata.size()); + + if(s == nullptr) { + qDebug() << "Using Tox default settings"; + return toxOptions; + } + // IPv6 needed for LAN discovery, but can crash some weird routers. On by default, can be // disabled in options. const bool enableIPv6 = s->getEnableIPv6(); @@ -59,7 +83,6 @@ std::unique_ptr ToxOptions::makeToxOptions(const QByteArray& savedat const bool enableLanDiscovery = s->getEnableLanDiscovery() && !forceTCP; ICoreSettings::ProxyType proxyType = s->getProxyType(); quint16 proxyPort = s->getProxyPort(); - QString proxyAddr = s->getProxyAddr(); if (!enableLanDiscovery) { qWarning() << "Core starting without LAN discovery. Peers can only be found through DHT."; @@ -70,28 +93,13 @@ std::unique_ptr ToxOptions::makeToxOptions(const QByteArray& savedat qWarning() << "Core starting with IPv6 disabled. LAN discovery may not work properly."; } - Tox_Options* tox_opts = tox_options_new(nullptr); - - if (!tox_opts) { - return {}; - } - - auto toxOptions = std::unique_ptr(new ToxOptions(tox_opts, proxyAddr.toUtf8())); - // register log first, to get messages as early as possible - tox_options_set_log_callback(*toxOptions, ToxLogger::onLogMessage); - - // savedata - tox_options_set_savedata_type(*toxOptions, !savedata.isNull() ? TOX_SAVEDATA_TYPE_TOX_SAVE - : TOX_SAVEDATA_TYPE_NONE); - tox_options_set_savedata_data(*toxOptions, reinterpret_cast(savedata.data()), - savedata.size()); // No proxy by default tox_options_set_proxy_type(*toxOptions, TOX_PROXY_TYPE_NONE); tox_options_set_proxy_host(*toxOptions, nullptr); tox_options_set_proxy_port(*toxOptions, 0); if (proxyType != ICoreSettings::ProxyType::ptNone) { - if (proxyAddr.length() > MAX_PROXY_ADDRESS_LENGTH) { + if (static_cast(proxyAddr.length()) > tox_max_hostname_length()) { qWarning() << "proxy address" << proxyAddr << "is too long"; } else if (!proxyAddr.isEmpty() && proxyPort > 0) { qDebug() << "using proxy" << proxyAddr << ":" << proxyPort;