FuelLabs / sway

🌴 Empowering everyone to build reliable and efficient smart contracts.
https://docs.fuel.network/docs/sway/
Apache License 2.0
62.64k stars 5.36k forks source link

pow can return invalid results #6157

Open IGI-111 opened 4 months ago

IGI-111 commented 4 months ago

pow may return results where the value is superior to the type maximum due to all primitive types of size < 8 bytes being treated as u64

vaivaswatha commented 4 months ago
#[test]
    fn u32_pow() -> () {
      disable_panic_on_overflow();
      let x: u32 = 2;
      let y: u32 = 64;
      let z = x.pow(y); //this becomes 2**32, since u32 are essentially u64 under disguise, and explicit checks must be made to ensure values fall within range
      assert(z == 0);
      ()
    }
    #[test]
    fn u64_pow() -> () {
      disable_panic_on_overflow();
      let x: u64 = 2;
      let y: u32 = 64;
      let z = x.pow(y);
      assert(z == 0);
      ()
    }