Giter Site home page Giter Site logo

overflow.cr's Introduction

[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

overflow.cr's People

Contributors

petoem avatar

Stargazers

 avatar

Watchers

 avatar  avatar

overflow.cr's Issues

Shard detects Integer overflow in stdlib

An empty crystal code file, only requiring overflow shard, raises. e.g.

require "overflow"
-1 too big or small for Int32#to_u64 (IntegerOverflow)
Failed to raise an exception: END_OF_STACK
[0x731a09a30e6] *CallStack::print_backtrace:Int32 +118
[0x731a098aeb6] __crystal_raise +86
[0x731a099a801] *CallStack::read_dwarf_sections:(Array(Tuple(UInt64, UInt64, String)) | Nil) +673
[0x731a099a30d] *CallStack::decode_line_number<UInt64>:Tuple(String | Nil, Int32, Int32) +45
[0x731a0999ab8] *CallStack#decode_backtrace:Array(String) +296
[0x731a0999972] *CallStack#printable_backtrace:Array(String) +50
[0x731a09bd3cd] *Exception+ +77
[0x731a09bd095] *Exception+ +117
[0x731a09e336d] *Crystal::main<Int32, Pointer(Pointer(UInt8))>:Int32 +157
[0x731a0995ed6] main +6
[0x620e3495e9a7] __libc_start_main +231
[0x731a098a44a] _start +42
[0x0] ???

Probably something inside the stdlib overflows. Needs further investigation.

UPDATE: crystal spec gives the same output.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.