Skip to content

Commit

Permalink
[#52] vdfs: throw a custom exception if the VDFs signature is not known
Browse files Browse the repository at this point in the history
This fixes a crash with VDFs generate by Union and potentially other 3rd-party tools.
  • Loading branch information
lmichaelis committed Jan 28, 2023
1 parent 55325da commit 39319e9
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/phoenix/vdfs.hh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ namespace phoenix {
/// \note This will convert to a ``DOS`` timestamp in the ``GMT`` timezone.
std::uint32_t unix_time_to_dos(std::time_t tm) noexcept;

/// \brief An exception thrown if the signature of a VDF file is not recognized.
///
/// If the signature is not recognized, this means that the VDF file was created using a 3rd-party tool
/// like Union and is not necessarily compatible with the VDFS spec. Accepted signatures are
/// phoenix::VDF_SIGNATURE_G1 and phoenix::VDF_SIGNATURE_G2.
class vdfs_signature_error : public error {
public:
explicit vdfs_signature_error(const std::string& signature);
};

/// \brief Represents the header of a VDF.
class vdf_header {
public:
Expand Down
9 changes: 9 additions & 0 deletions source/vdfs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ namespace phoenix {
return dos;
}

vdfs_signature_error::vdfs_signature_error(const std::string& signature)
: error("VDF signature not recognized: \"" + signature + "\"") {}

bool vdf_entry_comparator::operator()(const vdf_entry& a, const vdf_entry& b) const {
return icompare(a.name, b.name);
}
Expand Down Expand Up @@ -236,6 +239,12 @@ namespace phoenix {
vdf_file vdf {};

vdf.header = vdf_header::read(buf);

// TODO: Reverse-engineer Union VDF format
if (vdf.header.signature != VDF_SIGNATURE_G1 && vdf.header.signature != VDF_SIGNATURE_G2) {
throw vdfs_signature_error {vdf.header.signature};
}

buf.position(vdf.header.catalog_offset);

const vdf_entry* entry = nullptr;
Expand Down

0 comments on commit 39319e9

Please sign in to comment.