This repository has been archived by the owner. It is now read-only.

overflow.cr

[Experiment] Wrapper for integer to prevent overflow.

[archived] overflow.cr

Build Status

[Experiment] Wrap integer into SafeBox(T) to prevent/raise on overflow. Inspired by this gist.

Note: Do not use in production! You have been warned.

Installation

Add this to your application's shard.yml:

dependencies:
  overflow:
    github: petoem/overflow.cr

This shard links against libclang_rt.builtins-*.a which is part of compiler-rt. It is needed to fix this error related to Int128 multiplication.

You will have to install clang because it is used to discover the location of the compiler runtime library. Above file is provided by package compiler-rt on ArchLinux and libclang-common on Debian (both dependencies of clang package). Hence, installing clang should be enough.

Usage

overflow.cr shard provides SafeBox(T), which helps you prevent integer overflow.
Operations like +, -, * on an integer stored inside SafeBox(T) are done using LLVM Intrinsics and SafeBox(T)#to_* methods are checked for overflow too.
All other method calls are forwarded to the integer value stored inside using forward_missing_to macro.

require "overflow"

# Short example
struct Color
  getter r : SafeBox(UInt8)
  getter g : SafeBox(UInt8)
  getter b : SafeBox(UInt8)

  def initialize(@r, @g ,@b); end
end

# Create cyan color
cyan = Color.new 0_u8.to_sb, 255_u8.to_sb, 255_u8.to_sb

# Lets look what it stores ...
pp cyan # => Color(
        #     @b=SafeBox(UInt8)(@type=UInt8, @value=255_u8),
        #     @g=SafeBox(UInt8)(@type=UInt8, @value=255_u8),
        #     @r=SafeBox(UInt8)(@type=UInt8, @value=0_u8))

# and now make it a bit more blue.
blue = cyan.b + 20 # SafeBox(UInt8) uadd Int32 (IntegerOverflow)

# Oops ... to much blue.

That's it for now.

Known Issues

  • In a computation e.g. a + b, the right operand type is always extended or trimmed to the left operand type.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

  • petoem Michael Petö - creator, maintainer
Repository

overflow.cr

Owner
Statistic
  • 1
  • 0
  • 0
  • 0
  • 0
  • over 5 years ago
  • May 5, 2018
License

MIT License

Links
Synced at

Thu, 02 May 2024 04:10:33 GMT

Languages