From 6558d20683079f0c519cd29afe2b55fe55e7ffed Mon Sep 17 00:00:00 2001 From: Pierre Ossman Date: Fri, 11 Jun 2021 17:20:44 +0200 Subject: [PATCH] Keep own memory for duplicate config value Allows the default value to be dynamically generated in a buffer that is reused. --- common/rfb/Configuration.cxx | 15 ++++++++++----- common/rfb/Configuration.h | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/common/rfb/Configuration.cxx b/common/rfb/Configuration.cxx index ceaefccb..00039d96 100644 --- a/common/rfb/Configuration.cxx +++ b/common/rfb/Configuration.cxx @@ -376,7 +376,7 @@ IntParameter::operator int() const { StringParameter::StringParameter(const char* name_, const char* desc_, const char* v, ConfigurationObject co) - : VoidParameter(name_, desc_, co), value(strDup(v)), def_value(v) + : VoidParameter(name_, desc_, co), value(strDup(v)), def_value(strDup(v)) { if (!v) { vlog.error("Default value for %s not allowed",name_); @@ -386,10 +386,12 @@ StringParameter::StringParameter(const char* name_, const char* desc_, StringParameter::~StringParameter() { strFree(value); + strFree(def_value); } void StringParameter::setDefaultStr(const char* v) { - def_value = v; + strFree(def_value); + def_value = strDup(v); strFree(value); value = strDup(v); } @@ -422,16 +424,19 @@ StringParameter::operator const char *() const { BinaryParameter::BinaryParameter(const char* name_, const char* desc_, const void* v, size_t l, ConfigurationObject co) -: VoidParameter(name_, desc_, co), value(0), length(0), def_value((char*)v), def_length(l) { +: VoidParameter(name_, desc_, co), value(0), length(0), def_value(0), def_length(0) { if (l) { value = new char[l]; length = l; memcpy(value, v, l); + def_value = new char[l]; + def_length = l; + memcpy(def_value, v, l); } } BinaryParameter::~BinaryParameter() { - if (value) - delete [] value; + delete [] value; + delete [] def_value; } bool BinaryParameter::setParam(const char* v) { diff --git a/common/rfb/Configuration.h b/common/rfb/Configuration.h index e23e8a51..376805bb 100644 --- a/common/rfb/Configuration.h +++ b/common/rfb/Configuration.h @@ -251,7 +251,7 @@ namespace rfb { char* getData() const { return getValueStr(); } protected: char* value; - const char* def_value; + char* def_value; }; class BinaryParameter : public VoidParameter { -- 2.39.5