Skip to content

Commit 1278370

Browse files
committed
Fix cjson leaks in Settings.cpp
1 parent 203b833 commit 1278370

File tree

3 files changed

+56
-8
lines changed

3 files changed

+56
-8
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ add_executable(Finestray WIN32
9797
src/Bitmap.h
9898
src/BitmapHandleWrapper.h
9999
src/BrushHandleWrapper.h
100+
src/CJsonWrapper.h
100101
src/COMLibraryWrapper.h
101102
src/ContextMenu.cpp
102103
src/ContextMenu.h

src/CJsonWrapper.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2020 Benbuck Nason
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
// cJSON
18+
#include <cJSON.h>
19+
20+
// Standard library
21+
#include <string>
22+
23+
class CJsonWrapper
24+
{
25+
public:
26+
explicit CJsonWrapper(cJSON * cjson)
27+
: cjson_(cjson)
28+
{
29+
}
30+
31+
~CJsonWrapper()
32+
{
33+
if (cjson_) {
34+
cJSON_Delete(cjson_);
35+
}
36+
}
37+
38+
operator cJSON *() const { return cjson_; }
39+
40+
std::string print()
41+
{
42+
char * rawJson = cJSON_Print(cjson_);
43+
std::string json(rawJson);
44+
free(rawJson);
45+
return json;
46+
}
47+
48+
private:
49+
cJSON * cjson_ {};
50+
};

src/Settings.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,13 @@
1616

1717
// App
1818
#include "AppName.h"
19+
#include "CJsonWrapper.h"
1920
#include "File.h"
2021
#include "Hotkey.h"
2122
#include "Log.h"
2223
#include "Path.h"
2324
#include "StringUtility.h"
2425

25-
// cJSON
26-
#include <cJSON.h>
27-
2826
// Windows
2927
#include <Windows.h>
3028
#include <shellapi.h>
@@ -250,13 +248,13 @@ void Settings::addAutoTray(const std::string & executable, const std::string & w
250248

251249
bool Settings::parseJson(const std::string & json)
252250
{
253-
const cJSON * cjson = cJSON_Parse(json.c_str());
251+
CJsonWrapper cjson(cJSON_Parse(json.c_str()));
254252
if (!cjson) {
255253
WARNING_PRINTF("failed to parse settings JSON:\n%s\n", cJSON_GetErrorPtr());
256254
return false;
257255
}
258256

259-
DEBUG_PRINTF("parsed settings JSON:\n%s\n", cJSON_Print(cjson));
257+
DEBUG_PRINTF("parsed settings JSON:\n%s\n", cjson.print().c_str());
260258

261259
startWithWindows_ = getBool(cjson, settingKeys_[SK_StartWithWindows], startWithWindows_);
262260
showWindowsInMenu_ = getBool(cjson, settingKeys_[SK_ShowWindowsInMenu], showWindowsInMenu_);
@@ -300,7 +298,7 @@ bool Settings::fileExists(const std::string & fileName)
300298

301299
std::string Settings::constructJSON()
302300
{
303-
cJSON * cjson = cJSON_CreateObject();
301+
CJsonWrapper cjson(cJSON_CreateObject());
304302
if (!cjson) {
305303
WARNING_PRINTF("failed to create settings JSON object\n");
306304
return std::string();
@@ -385,11 +383,10 @@ std::string Settings::constructJSON()
385383

386384
if (fail) {
387385
WARNING_PRINTF("failed to construct json settings\n");
388-
cJSON_Delete(cjson);
389386
return std::string();
390387
}
391388

392-
return cJSON_Print(cjson);
389+
return cjson.print();
393390
}
394391

395392
bool Settings::autoTrayItemCallback(const cJSON * cjson, void * userData)

0 commit comments

Comments
 (0)