Skip to content

Commit 71b74bd

Browse files
Fixed error handling during loading of generation configuration
Fixed incorrect error message Made other error messages more descriptive Made error handling more consistent Report all errors at once rather than having to run multiple times to fix each error Put error checking all in one place Check for empty values more consistently Also fixed types of abbreviations to elements in this structure to be const references Also fixed type of config_path to be std::string
1 parent edd1635 commit 71b74bd

File tree

3 files changed

+81
-42
lines changed

3 files changed

+81
-42
lines changed

src/java-class-info/generation_configuration.cpp

+51-4
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,67 @@
22

33
#include "generation_configuration.h"
44
#include <boost/assign.hpp>
5+
#include <boost/filesystem.hpp>
56
#include <list>
7+
#include <sstream>
68
#include <util/json.h>
79

810
generation_configurationt::generation_configurationt(const jsont &json)
911
{
1012
detected_entry_points_path =
1113
json[json_namest::detected_entry_points_path].value;
14+
for(const jsont &element : json[json_namest::pattern_group].array)
15+
{
16+
pattern_group.emplace_back(element);
17+
}
18+
if(!pattern_group.empty())
19+
{
20+
if(detected_entry_points_path.empty())
21+
{
22+
std::stringstream error;
23+
error << "The configuration file contained pattern groups but not a "
24+
<< json_namest::detected_entry_points_path << " element.";
25+
errors.push_back(error.str());
26+
}
27+
else if(boost::filesystem::is_directory(detected_entry_points_path))
28+
{
29+
std::stringstream error;
30+
error << "The output path '" << detected_entry_points_path
31+
<< "' references an existing directory.";
32+
errors.push_back(error.str());
33+
}
34+
}
35+
1236
collected_classes_info_path =
1337
json[json_namest::collected_classes_info_path].value;
14-
collected_classes_path = json[json_namest::collected_classes_path].value;
15-
di_configuration_path = json[json_namest::di_configuration_path].value;
38+
if(collected_classes_info_path.empty())
39+
{
40+
std::stringstream error;
41+
error << "The configuration file did not contain a "
42+
<< json_namest::collected_classes_info_path << " element.";
43+
}
44+
else if(boost::filesystem::is_directory(collected_classes_info_path))
45+
{
46+
std::stringstream error;
47+
error << "The output path '" << collected_classes_info_path
48+
<< "' references an existing directory.";
49+
}
1650

17-
for(const jsont &element : json[json_namest::pattern_group].array)
51+
collected_classes_path = json[json_namest::collected_classes_path].value;
52+
if(collected_classes_path.empty())
1853
{
19-
pattern_group.emplace_back(element);
54+
std::stringstream error;
55+
error << "The configuration file did not contain a "
56+
<< json_namest::collected_classes_path << " element.";
57+
errors.push_back(error.str());
58+
}
59+
else if(!boost::filesystem::is_regular_file(collected_classes_path))
60+
{
61+
std::stringstream error;
62+
error << "The input file '" << collected_classes_path
63+
<< "' does not exist.";
64+
errors.push_back(error.str());
2065
}
66+
67+
di_configuration_path = json[json_namest::di_configuration_path].value;
2168
}

src/java-class-info/generation_configuration.h

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#define SECURITY_SCANNER_GENERATION_CONFIGURATIONT_H
55

66
#include "pattern_group.h"
7+
#include <optional.h>
78

89
/// Object representation of the main configuration file used to drive
910
/// java_class-info.
@@ -18,6 +19,13 @@ class generation_configurationt
1819
std::string detected_entry_points_path;
1920
std::string di_configuration_path;
2021

22+
optionalt<std::vector<std::string>> get_parse_errors()
23+
{
24+
if(errors.empty())
25+
return {};
26+
return errors;
27+
}
28+
2129
private:
2230
class json_namest
2331
{
@@ -31,6 +39,8 @@ class generation_configurationt
3139
"detectedEntryPointsPath";
3240
static constexpr const char *di_configuration_path = "diConfigurationPath";
3341
};
42+
43+
std::vector<std::string> errors;
3444
};
3545

3646
#endif //SECURITY_SCANNER_GENERATION_CONFIGURATIONT_H

src/java-class-info/java_class_info.cpp

+20-38
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <util/file_util.h>
1919
#include <util/irep_ids.h>
2020
#include <util/parse_options.h>
21+
#include <util/string_join.h>
2122
#include <util/ui_message.h>
2223
#include <utility>
2324
#include <utils.h>
@@ -304,30 +305,37 @@ int cmdline_optionst::doit()
304305
}
305306
if(!cmdline.isset("configuration-path"))
306307
{
307-
msg.error() << "Please set --configuration-path to a valid configuration "
308-
"file."
308+
msg.error() << "--configuration-path wasn't provided. "
309+
"Use it to provide a valid configuration file."
309310
<< messaget::eom;
310311
return -1;
311312
}
312313

313-
boost::filesystem::path config_path = cmdline.get_value("configuration-path");
314+
std::string config_path = cmdline.get_value("configuration-path");
315+
if(config_path.empty())
316+
{
317+
msg.error() << "--configuration-path was empty. "
318+
"Use it to provide a valid configuration file."
319+
<< messaget::eom;
320+
return -1;
321+
}
314322
jsont config_json;
315-
if(parse_json(config_path.string(), ui_message_handler, config_json))
323+
if(parse_json(config_path, ui_message_handler, config_json))
316324
{
317325
msg.error() << "ERROR: The input file '" << config_path
318326
<< "' is not a valid JSON file." << messaget::eom;
319327
return -3;
320328
}
321329

322330
generation_configurationt config = generation_configurationt(config_json);
323-
const std::string in_json_pathname = config.collected_classes_path;
324-
if(!boost::filesystem::is_regular_file(in_json_pathname))
331+
optionalt<std::vector<std::string>> parse_errors = config.get_parse_errors();
332+
if(parse_errors)
325333
{
326-
msg.error()
327-
<< "ERROR: The input file '" << in_json_pathname
328-
<< "' does not exist." << messaget::eom;
334+
msg.error() << "ERROR: " << join(*parse_errors, "\n") << messaget::eom;
329335
return -2;
330336
}
337+
338+
const std::string &in_json_pathname = config.collected_classes_path;
331339
jsont cfg;
332340
if(parse_json(in_json_pathname, ui_message_handler, cfg))
333341
{
@@ -345,21 +353,9 @@ int cmdline_optionst::doit()
345353
<< "class files)." << messaget::eom;
346354
return -4;
347355
}
348-
if(config.collected_classes_info_path.empty())
349-
{
350-
msg.status() << "ERROR: collected_classes_info_path is empty.";
351-
return -5;
352-
}
353-
const std::string out_json_pathname = config.collected_classes_info_path;
354-
if(boost::filesystem::is_directory(out_json_pathname))
355-
{
356-
msg.error()
357-
<< "ERROR: The output path '" << out_json_pathname
358-
<< "' references an existing directory." << messaget::eom;
359-
return -6;
360-
}
356+
const std::string &out_json_pathname = config.collected_classes_info_path;
361357

362-
const std::string di_out_path = config.di_configuration_path;
358+
const std::string &di_out_path = config.di_configuration_path;
363359
bool enable_di_scan = !di_out_path.empty();
364360
json_objectt results;
365361
json_arrayt errors;
@@ -605,21 +601,7 @@ int cmdline_optionst::doit()
605601

606602
if(ep_generator.enabled())
607603
{
608-
const std::string detected_ep_path = config.detected_entry_points_path;
609-
610-
if(detected_ep_path.empty())
611-
{
612-
msg.status() << "ERROR: collected_classes_info_path is empty.";
613-
return -9;
614-
}
615-
if(boost::filesystem::is_directory(detected_ep_path))
616-
{
617-
msg.error()
618-
<< "ERROR: The output path '" << detected_ep_path
619-
<< "' references an existing directory." << messaget::eom;
620-
return -10;
621-
}
622-
604+
const std::string &detected_ep_path = config.detected_entry_points_path;
623605
std::ofstream epp_file(detected_ep_path);
624606
if(!epp_file)
625607
{

0 commit comments

Comments
 (0)