Skip to content

Commit

Permalink
Fix property_db::write_file_json dangling open FILE in some error con…
Browse files Browse the repository at this point in the history
…ditions.

Also apply some clang-tidy cleanups.
  • Loading branch information
grafikrobot committed Jun 5, 2024
1 parent 5f32bc3 commit ef71f55
Show file tree
Hide file tree
Showing 8 changed files with 379 additions and 364 deletions.
7 changes: 5 additions & 2 deletions src/engine/bindjam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ Distributed under the Boost Software License, Version 1.0.

#include "bindjam.h"

#include "bind.h"
#include "class.h"
#include "frames.h"
#include "function.h"
#include "hash.h"
#include "lists.h"
#include "modules.h"
#include "mp.h"
#include "native.h"
#include "object.h"
#include "optval.h"
#include "output.h"
#include "parse.h"
#include "rules.h"
#include "types.h"
Expand All @@ -36,9 +38,10 @@ Distributed under the Boost Software License, Version 1.0.

#include <cstddef>
#include <cstdint>
#include <memory>
#include <exception>
#include <string>
#include <tuple>
#include <type_traits>
#include <vector>

namespace b2 { namespace jam {
Expand Down
3 changes: 0 additions & 3 deletions src/engine/builtins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,16 @@
#include "md5.h"
#include "native.h"
#include "object.h"
#include "parse.h"
#include "pathsys.h"
#include "regexp.h"
#include "rules.h"
#include "jam_strings.h"
#include "startup.h"
#include "timestamp.h"
#include "variable.h"
#include "output.h"

#include <string>

#include <assert.h>
#include <ctype.h>
#include <stdlib.h>

Expand Down
10 changes: 9 additions & 1 deletion src/engine/mod_command_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,23 @@ Distributed under the Boost Software License, Version 1.0.
#include "command.h"
#include "cwd.h"
#include "events.h"
#include "jam.h"
#include "lists.h"
#include "mod_db.h"
#include "output.h"
#include "pathsys.h"
#include "regexp.h"

#include "ext_bfgroup_lyra.h"
#include "rules.h"
#include "value.h"

#include <cstdint>
#include <functional>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>

namespace b2 { namespace command_db {

Expand Down Expand Up @@ -157,7 +165,7 @@ void declare_args(lyra::cli & cli)
"build-dir.");
}

void set_output_dir(value_ref dirname)
void set_output_dir(const value_ref & dirname)
{
database::get().output_directory = dirname;
}
Expand Down
4 changes: 1 addition & 3 deletions src/engine/mod_command_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ Distributed under the Boost Software License, Version 1.0.
#ifndef B2_COMMAND_DB_H
#define B2_COMMAND_DB_H

#include "config.h"

#include "bind.h"
#include "value.h"

Expand All @@ -22,7 +20,7 @@ namespace command_db {

void declare_args(lyra::cli &);

void set_output_dir(value_ref dirname);
void set_output_dir(const value_ref & dirname);

} // namespace command_db

Expand Down
30 changes: 17 additions & 13 deletions src/engine/mod_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ Distributed under the Boost Software License, Version 1.0.
#include "mod_db.h"

#include "ext_nlohmann_json.hpp"
#include "lists.h"
#include "value.h"

#include <cstdio>
#include <cstdlib>
#include <exception>
#include <string>

void b2::property_db::emplace(list_cref k, value_ref v)
{
Expand Down Expand Up @@ -42,11 +46,12 @@ void b2::property_db::emplace(list_cref k, value_ref v)
db[nk] = v;
}

bool b2::property_db::write_file(value_ref filename, value_ref format)
bool b2::property_db::write_file(
const value_ref & filename, const value_ref & format)
{
if (!format->has_value())
{
format = b2::value::make("json");
return write_file_json(filename);
}
if (format == "json") return write_file_json(filename);
return false;
Expand Down Expand Up @@ -102,23 +107,22 @@ void build_json_from_db(const T & db, nlohmann::json & out)
}
} // namespace

bool b2::property_db::write_file_json(value_ref filename)
bool b2::property_db::write_file_json(const value_ref & filename)
{
nlohmann::json out;
build_json_from_db(db, out);
FILE * file = std::fopen(filename->str(), "w");
if (file)
if (!file) return false;
bool result = false;
try
{
try
{
auto data = out.dump(0);
return std::fwrite(data.c_str(), data.size(), 1, file) == 1;
}
catch (const std::exception &)
{}
std::fclose(file);
auto data = out.dump(0);
result = std::fwrite(data.c_str(), data.size(), 1, file) == 1;
}
return false;
catch (const std::exception &)
{}
std::fclose(file);
return result;
}

std::string b2::property_db::dump_json()
Expand Down
7 changes: 3 additions & 4 deletions src/engine/mod_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ Distributed under the Boost Software License, Version 1.0.
#ifndef B2_MOD_DB_H
#define B2_MOD_DB_H

#include "config.h"

#include "bind.h"
#include "lists.h"
#include "value.h"

#include <map>
#include <string>

/* tag::reference[]
Expand All @@ -36,7 +35,7 @@ end::reference[] */
struct property_db : public object
{
void emplace(list_cref k, value_ref v);
bool write_file(value_ref filename, value_ref format);
bool write_file(const value_ref & filename, const value_ref & format);
std::string dump(value_ref format);

private:
Expand All @@ -58,7 +57,7 @@ struct property_db : public object
};
using db_type = std::map<list_ref, value_ref, key_less>;
db_type db;
bool write_file_json(value_ref filename);
bool write_file_json(const value_ref & filename);
std::string dump_json();
};

Expand Down
Loading

0 comments on commit ef71f55

Please sign in to comment.