Skip to content

Commit e9a1ef4

Browse files
committed
(maint) Fix include file from file
Including a file from another file caused a crash because we were creating a shared_ptr from a `parseable` value. The include context assumes the parseable it's created from will will continue to exist, meaning we can't copy parseables. Fix instances where we were creating copies of parseables. Fixes #75.
1 parent 5b9d76e commit e9a1ef4

File tree

6 files changed

+23
-23
lines changed

6 files changed

+23
-23
lines changed

lib/inc/internal/parseable.hpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@
99
namespace hocon {
1010

1111
class config_document;
12-
class parseable_file;
13-
class parseable_string;
14-
class parseable_not_found;
1512

1613
class parseable : public config_parseable, public std::enable_shared_from_this<parseable> {
1714
public:
18-
static parseable_file new_file(std::string input_file_path, config_parse_options options);
19-
static parseable_string new_string(std::string s, config_parse_options options);
20-
static parseable_not_found new_not_found(std::string what_not_found, std::string message,
21-
config_parse_options options);
15+
static std::shared_ptr<parseable> new_file(std::string input_file_path, config_parse_options options);
16+
static std::shared_ptr<parseable> new_string(std::string s, config_parse_options options);
17+
static std::shared_ptr<parseable> new_not_found(std::string what_not_found, std::string message,
18+
config_parse_options options);
2219

2320
static config_syntax syntax_from_extension(std::string name);
2421

@@ -43,6 +40,11 @@ namespace hocon {
4340

4441
std::string to_string() const;
4542

43+
// Disable copy constructors, as include_context assumes it can hold a reference to parseable.
44+
parseable() = default;
45+
parseable(parseable const&) = delete;
46+
parseable& operator=(parseable const&) = delete;
47+
4648
private:
4749
std::shared_ptr<config_document> parse_document(config_parse_options const& base_options) const;
4850
std::shared_ptr<config_document> parse_document(shared_origin origin,

lib/src/config.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace hocon {
4040

4141
shared_config config::parse_string(string s, config_parse_options options)
4242
{
43-
return parseable::new_string(move(s), move(options)).parse()->to_config();
43+
return parseable::new_string(move(s), move(options))->parse()->to_config();
4444
}
4545

4646
shared_config config::parse_string(string s)

lib/src/config_document_factory.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ using namespace std;
66
namespace hocon { namespace config_document_factory {
77

88
shared_ptr<config_document> parse_file(string input_file_path, config_parse_options options) {
9-
return parseable::new_file(move(input_file_path), move(options)).parse_config_document();
9+
return parseable::new_file(move(input_file_path), move(options))->parse_config_document();
1010
}
1111

1212
shared_ptr<config_document> parse_file(string input_file_path) {
1313
return parse_file(move(input_file_path), config_parse_options());
1414
}
1515

1616
shared_ptr<config_document> parse_string(string s, config_parse_options options) {
17-
return parseable::new_string(move(s), move(options)).parse_config_document();
17+
return parseable::new_string(move(s), move(options))->parse_config_document();
1818
}
1919

2020
shared_ptr<config_document> parse_string(string s) {

lib/src/parseable.cc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,17 @@ namespace hocon {
2727

2828
const int parseable::MAX_INCLUDE_DEPTH = 50;
2929

30-
parseable_file parseable::new_file(std::string input_file_path, config_parse_options options) {
31-
return parseable_file(move(input_file_path), move(options));
30+
shared_ptr<parseable> parseable::new_file(std::string input_file_path, config_parse_options options) {
31+
return make_shared<parseable_file>(move(input_file_path), move(options));
3232
}
3333

34-
parseable_string parseable::new_string(std::string s, config_parse_options options) {
35-
return parseable_string(move(s), move(options));
34+
shared_ptr<parseable> parseable::new_string(std::string s, config_parse_options options) {
35+
return make_shared<parseable_string>(move(s), move(options));
3636
}
3737

38-
parseable_not_found parseable::new_not_found(std::string what_not_found, std::string message,
38+
shared_ptr<parseable> parseable::new_not_found(std::string what_not_found, std::string message,
3939
config_parse_options options) {
40-
return parseable_not_found(move(what_not_found), move(message), move(options));
40+
return make_shared<parseable_not_found>(move(what_not_found), move(message), move(options));
4141
}
4242

4343
void parseable::post_construct(config_parse_options const& base_options) {

lib/src/simple_includer.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,15 @@ namespace hocon {
144144
auto p = _context->relative_to(name);
145145
if (p == nullptr) {
146146
// avoid returning null
147-
return make_shared<parseable_not_found>(
148-
parseable::new_not_found(name, _("include was not found: '{1}'", name), move(parse_options)));
147+
return parseable::new_not_found(name, _("include was not found: '{1}'", name), move(parse_options));
149148
} else {
150149
return p;
151150
}
152151
}
153152

154153
/** File name source */
155154
shared_parseable file_name_source::name_to_parseable(std::string name, config_parse_options parse_options) const {
156-
return make_shared<parseable_file>(
157-
parseable::new_file(move(name), move(parse_options)));
155+
return parseable::new_file(move(name), move(parse_options));
158156
}
159157

160158
/** Proxy */

lib/tests/conf_parser_test.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static shared_value parse_without_resolving(string s) {
2323
auto options = config_parse_options()
2424
.set_origin_description(make_shared<string>("test conf string"))
2525
.set_syntax(config_syntax::CONF);
26-
return parseable::new_string(move(s), move(options)).parse_value();
26+
return parseable::new_string(move(s), move(options))->parse_value();
2727
}
2828

2929
static shared_value parse(string s) {
@@ -221,7 +221,7 @@ TEST_CASE("implied comma handling") {
221221
[&](string const &s) { return drop_curlies(s); }
222222
};
223223

224-
auto tested = 0;
224+
auto tested = 0u;
225225
for (auto v : valids) {
226226
for (auto change : changes) {
227227
++tested;
@@ -306,7 +306,7 @@ TEST_CASE("line numbers in errors (pending)", "[!shouldfail]") {
306306
TEST_CASE("to string for parseables") {
307307
// just be sure the to_string don't throw, to get test coverage
308308
auto options = config_parse_options();
309-
parseable::new_file("foo", options).to_string();
309+
parseable::new_file("foo", options)->to_string();
310310
// TODO: are other APIs needed?
311311
}
312312

0 commit comments

Comments
 (0)