morganstanley / hobbes

A language and an embedded JIT compiler
http://hobbes.readthedocs.io/
Apache License 2.0
1.16k stars 105 forks source link

remove size_t -> int conversion because size_t can be 2^64-1 #423

Closed mo-xiaoming closed 2 years ago

mo-xiaoming commented 2 years ago

In

Variant::Members consMember(const std::string& lbl, const MonoTypePtr& hty, const Variant::Members& tty) {
  int slot = findHiddenMember(tty.size() - 1, lbl, tty);

When tty is empty (CFRegion_C2H and CFRegion_H2C in Storage.C), conversion tty.size()-1 -> int becomes 2^64-1 -> int, narrowing and implicit conversion

int findHiddenMember(int i, const std::string& lbl, const Variant::Members& ms)

Although this code works on all modern platform (2 complement, guaranteed by c++17), it did take extra mental effort for me to be sure it won't cause real(?) problem