Skip to content

Commit

Permalink
hashlib: replace check with proper assert
Browse files Browse the repository at this point in the history
  • Loading branch information
widlarizer authored and povik committed Sep 2, 2024
1 parent fde3839 commit 80ac684
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 17 deletions.
12 changes: 6 additions & 6 deletions kernel/rtlil.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ RTLIL::Const::Const(const RTLIL::Const &other) {
else if (is_bits())
new ((void*)&bits_) bitvectype(other.get_bits());
else
check(false);
log_assert(false && "malformed Const union");
}

RTLIL::Const::Const(RTLIL::Const &&other) {
Expand All @@ -261,15 +261,15 @@ RTLIL::Const::Const(RTLIL::Const &&other) {
else if (is_bits())
new ((void*)&bits_) bitvectype(std::move(other.get_bits()));
else
check(false);
log_assert(false && "malformed Const union");
}

RTLIL::Const &RTLIL::Const::operator =(const RTLIL::Const &other) {
flags = other.flags;
if (other.is_str()) {
if (!is_str()) {
// sketchy zone
check(is_bits());
log_assert(is_bits() && "malformed Const union");
bits_.~bitvectype();
(void)new ((void*)&str_) std::string();
}
Expand All @@ -278,14 +278,14 @@ RTLIL::Const &RTLIL::Const::operator =(const RTLIL::Const &other) {
} else if (other.is_bits()) {
if (!is_bits()) {
// sketchy zone
check(is_str());
log_assert(is_str() && "malformed Const union");
str_.~string();
(void)new ((void*)&bits_) bitvectype();
}
tag = other.tag;
get_bits() = other.get_bits();
} else {
check(false);
log_assert(false && "malformed Const union");
}
return *this;
}
Expand All @@ -296,7 +296,7 @@ RTLIL::Const::~Const() {
else if (is_str())
str_.~string();
else
check(false);
log_assert(false && "malformed Const union");
}

bool RTLIL::Const::operator<(const RTLIL::Const &other) const
Expand Down
23 changes: 12 additions & 11 deletions kernel/rtlil.h
Original file line number Diff line number Diff line change
Expand Up @@ -683,13 +683,14 @@ struct RTLIL::Const
bitvectype* get_if_bits() const { return is_bits() ? &bits_ : NULL; }
std::string* get_if_str() const { return is_str() ? &str_ : NULL; }

void check(bool condition) const {
// TODO debug assert or throw
// log_assert(condition && "malformed Const union");
bitvectype& get_bits() const {
log_assert(is_bits() && "malformed Const union");
return *get_if_bits();
}
std::string& get_str() const {
log_assert(is_str() && "malformed Const union");
return *get_if_str();
}

bitvectype& get_bits() const { check(is_bits()); return *get_if_bits(); }
std::string& get_str() const { check(is_str()); return *get_if_str(); }
public:
Const() : flags(RTLIL::CONST_FLAG_NONE), tag(backing_tag::bits), bits_(std::vector<RTLIL::State>()) {}
Const(const std::string &str);
Expand All @@ -716,19 +717,19 @@ struct RTLIL::Const

std::string decode_string() const;
inline size_t size() const {
if (tag == backing_tag::string)
if (is_str())
return 8 * str_.size();
else {
check(tag == backing_tag::bits);
log_assert(is_bits() && "malformed Const union");
return bits_.size();
}
}

inline bool empty() const {
if (tag == backing_tag::string)
if (is_str())
return str_.empty();
else {
check(tag == backing_tag::bits);
log_assert(is_bits() && "malformed Const union");
return bits_.empty();
}
}
Expand All @@ -737,7 +738,7 @@ struct RTLIL::Const
if (tag == backing_tag::bits)
return;

check(tag == backing_tag::string);
log_assert(is_str() && "malformed Const union");

bitvectype new_bits;

Expand Down

0 comments on commit 80ac684

Please sign in to comment.