Skip to content

Commit 403e7f1

Browse files
authored
lib/addoninfo.cpp: When loading a JSON addon, test 'script' key. (#5797)
In case a user accidentally uses a wrong JSON file (e.g. naming.json, which is the config file for namingng.py), the code could give a confusing exception. This happens when the key 'script' is not defined as a string. This is solved by testing the key for existence and type. In case 'script' is not a key or refers to a type other than a string, a clear error is given, stating for example: 'Loading naming.json failed. script must be set to a string value.' The message is kept in line with other messages. Maybe it can be clarified further, e.g. 'Loading naming.json failed. A key "script" must be set with a string value referring to a Python script.' - in which case the errors relating to other keys may also be clarified.
1 parent a7baf88 commit 403e7f1

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

lib/addoninfo.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ static std::string parseAddonInfo(AddonInfo& addoninfo, const picojson::value &j
8787
return "";
8888
}
8989

90+
if (!obj.count("script") || !obj["script"].is<std::string>())
91+
{
92+
return "Loading " + fileName + " failed. script must be set to a string value.";
93+
}
94+
9095
return addoninfo.getAddonInfo(obj["script"].get<std::string>(), exename);
9196
}
9297

test/cli/test-other.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,22 @@ def test_addon_threadsafety(tmpdir):
278278
assert stderr == '{}:4:12: warning: strerror is MT-unsafe [threadsafety-unsafe-call]\n'.format(test_file)
279279

280280

281+
def test_addon_invalidjson(tmpdir):
282+
addon_file = os.path.join(tmpdir, 'invalid.json')
283+
with open(addon_file, 'wt') as f:
284+
f.write("""
285+
{
286+
"Script": "addons/something.py"
287+
}
288+
""")
289+
290+
args = ['--addon={}'.format(addon_file), '--enable=all', 'nonexistent.cpp']
291+
292+
exitcode, stdout, stderr = cppcheck(args)
293+
assert exitcode != 0
294+
assert stdout == 'Loading {} failed. script must be set to a string value.\n'.format(addon_file)
295+
296+
281297
def test_addon_naming(tmpdir):
282298
# the addon does nothing without a config
283299
addon_file = os.path.join(tmpdir, 'naming1.json')

0 commit comments

Comments
 (0)