Skip to content

Commit

Permalink
Fix crash due to i2d out-of-bounds access
Browse files Browse the repository at this point in the history
By controlling the order of static object construction
through the "Construct on First Use" idiom.
  • Loading branch information
dspinellis committed Jul 19, 2024
1 parent 7eced44 commit 8aa286e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
13 changes: 11 additions & 2 deletions src/filedetails.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,16 @@
#include "md5.h"
#include "os.h"

FI_id_to_details Filedetails::i2d; // From id to file details
/*
* Return the map from file id to file details, controlling the order of its
* construction through the "Construct on First Use" idiom.
*/
FI_id_to_details &
Filedetails::get_i2d()
{
static FI_id_to_details i2d;
return i2d;
}

/*
* Return a map of files that are exact duplicates, controlling the order
Expand Down Expand Up @@ -185,7 +194,7 @@ Filedetails::set_hand_edited()
void
Filedetails::clear_all_visited()
{
for (FI_id_to_details::iterator i = i2d.begin(); i != i2d.end(); i++)
for (FI_id_to_details::iterator i = get_i2d().begin(); i != get_i2d().end(); i++)
i->clear_visited();
}

Expand Down
10 changes: 6 additions & 4 deletions src/filedetails.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ class Filedetails {
string contents; // Original contents, if hand-edited
bool visited; // For calculating transitive closures

static FI_id_to_details i2d; // From id to file details
// Get map from id to file details
static FI_id_to_details& get_i2d();

// Return map of files that are exact duplicates
static FI_hash_to_ids& get_identical_files();
public:
Expand All @@ -121,16 +123,16 @@ class Filedetails {

// Return the instance associated with the specified id
static Filedetails &get_instance(Fileid fi) {
return i2d[fi.get_id()];
return get_i2d()[fi.get_id()];
}

static FI_id_to_details::size_type get_i2d_map_size() {
return i2d.size();
return get_i2d().size();
}

// Add a new instance with the specified ctor values
static void add_instance(string n, bool r, const FileHash &hash) {
i2d.emplace_back(n, r, hash);
get_i2d().emplace_back(n, r, hash);
}

// Unify identifiers of files that are exact copies
Expand Down

0 comments on commit 8aa286e

Please sign in to comment.