Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overflow prepare stdlib #7256

Merged
Merged
2 changes: 1 addition & 1 deletion spec/std/hash_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ describe "Hash" do
end

it "doesn't generate a negative index for the bucket index (#2321)" do
items = (0..100000).map { rand(100000).to_i16 }
items = (0..100000).map { rand(100000).to_i16! }
items.uniq.size
end

Expand Down
4 changes: 2 additions & 2 deletions spec/std/number_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ describe "Number" do
slice.size.should eq(3)
slice[0].should eq(1)
slice[1].should eq(2)
slice[2].should eq(300.to_u8)
slice[2].should eq(300.to_u8!)
end

it "creates a static array" do
Expand All @@ -211,7 +211,7 @@ describe "Number" do
ary.size.should eq(3)
ary[0].should eq(1)
ary[1].should eq(2)
ary[2].should eq(300.to_u8)
ary[2].should eq(300.to_u8!)
end

it "test zero?" do
Expand Down
12 changes: 6 additions & 6 deletions src/base64.cr
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ module Base64
break if cstr > endcstr
a, b, c, d = next_decoded_value, next_decoded_value, next_decoded_value, next_decoded_value

yield (a << 2 | b >> 4).to_u8
yield (b << 4 | c >> 2).to_u8
yield (c << 6 | d).to_u8
yield (a << 2 | b >> 4).to_u8!
yield (b << 4 | c >> 2).to_u8!
yield (c << 6 | d).to_u8!
end

while (cstr < endcstr + 4) && (cstr.value == NL || cstr.value == NR)
Expand All @@ -262,11 +262,11 @@ module Base64
mod = (endcstr - cstr) % 4
if mod == 2
a, b = next_decoded_value, next_decoded_value
yield (a << 2 | b >> 4).to_u8
yield (a << 2 | b >> 4).to_u8!
elsif mod == 3
a, b, c = next_decoded_value, next_decoded_value, next_decoded_value
yield (a << 2 | b >> 4).to_u8
yield (b << 4 | c >> 2).to_u8
yield (a << 2 | b >> 4).to_u8!
yield (b << 4 | c >> 2).to_u8!
elsif mod != 0
raise Error.new("Wrong size")
end
Expand Down
6 changes: 3 additions & 3 deletions src/char/reader.cr
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ struct Char
end

if first < 0xe0
return yield (first << 6) + (second - 0x3080), 2, nil
return yield (first << 6) &+ (second &- 0x3080), 2, nil
end

third = byte_at?(pos + 2)
Expand All @@ -217,7 +217,7 @@ struct Char
invalid_byte_sequence 3
end

return yield (first << 12) + (second << 6) + (third - 0xE2080), 3, nil
return yield (first << 12) &+ (second << 6) &+ (third &- 0xE2080), 3, nil
end

if first == 0xf0 && second < 0x90
Expand All @@ -236,7 +236,7 @@ struct Char
end

if first < 0xf5
return yield (first << 18) + (second << 12) + (third << 6) + (fourth - 0x3C82080), 4, nil
return yield (first << 18) &+ (second << 12) &+ (third << 6) &+ (fourth &- 0x3C82080), 4, nil
end

invalid_byte_sequence 4
Expand Down
8 changes: 4 additions & 4 deletions src/crypto/bcrypt.cr
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ class Crypto::Bcrypt
j = -1

cipher.size.times do |i|
ret[j += 1] = (cdata[i] >> 24).to_u8
ret[j += 1] = (cdata[i] >> 16).to_u8
ret[j += 1] = (cdata[i] >> 8).to_u8
ret[j += 1] = cdata[i].to_u8
ret[j += 1] = (cdata[i] >> 24).to_u8!
ret[j += 1] = (cdata[i] >> 16).to_u8!
ret[j += 1] = (cdata[i] >> 8).to_u8!
ret[j += 1] = cdata[i].to_u8!
end

ret
Expand Down
6 changes: 3 additions & 3 deletions src/crypto/bcrypt/base64.cr
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,18 @@ module Crypto::Bcrypt::Base64
break if c1 == -1 || c2 == -1
off += 2

str[i += 1] = ((c1 << 2) | (c2 & 0x30) >> 4).to_u8
str[i += 1] = ((c1 << 2) | (c2 & 0x30) >> 4).to_u8!
break if (olen += 1) >= maxolen || off >= slen

c3 = char64(string[off])
break if c3 == -1
off += 1

str[i += 1] = (((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2)).to_u8
str[i += 1] = (((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2)).to_u8!
break if (olen += 1) >= maxolen || off >= slen

c4 = char64(string[off])
str[i += 1] = (((c3 & 0x03) << 6) | c4).to_u8
str[i += 1] = (((c3 & 0x03) << 6) | c4).to_u8!
off += 1
olen += 1
end
Expand Down
2 changes: 1 addition & 1 deletion src/crypto/blowfish.cr
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class Crypto::Blowfish
c = (x >> 8) & 0xff_u32
b = (x >> 16) & 0xff_u32
a = (x >> 24) & 0xff_u32
((@s.to_unsafe[a] + @s1[b]) ^ @s2[c]) + @s3[d]
((@s.to_unsafe[a] &+ @s1[b]) ^ @s2[c]) &+ @s3[d]
end

private def next_word(data, pos)
Expand Down
14 changes: 7 additions & 7 deletions src/crystal/hasher.cr
Original file line number Diff line number Diff line change
Expand Up @@ -93,25 +93,25 @@ struct Crystal::Hasher
end

private def permute(v : UInt64)
@a = rotl32(@a ^ v) * C1
@b = (rotl32(@b) ^ v) * C2
@a = rotl32(@a ^ v) &* C1
@b = (rotl32(@b) ^ v) &* C2
self
end

def result
a, b = @a, @b
a ^= (a >> 23) ^ (a >> 40)
b ^= (b >> 23) ^ (b >> 40)
a *= C1
b *= C2
a &*= C1
b &*= C2
a ^= a >> 32
b ^= b >> 32
a + b
a &+ b
end

def nil
@a += @b
@b += 1
@a &+= @b
@b &+= 1
self
end

Expand Down
2 changes: 1 addition & 1 deletion src/debug/dwarf/info.cr
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module Debug
code = DWARF.read_unsigned_leb128(@io)
attributes.clear

if abbrev = abbreviations[code - 1]? # abbreviations.find { |a| a.code == abbrev }
if abbrev = abbreviations[code &- 1]? # abbreviations.find { |a| a.code == abbrev }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be heavily TODO'd, or compiled into an issue. If we just do a blanket "everything which fails due to overflow gets the old semantics" we miss a fantastic opportunity to get rid of bugs!

abbrev.attributes.each do |attr|
value = read_attribute_value(attr.form)
attributes << {attr.at, attr.form, value}
Expand Down
5 changes: 2 additions & 3 deletions src/debug/dwarf/line_numbers.cr
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,7 @@ module Debug
adjusted_opcode = opcode - sequence.opcode_base
operation_advance = adjusted_opcode / sequence.line_range
increment_address_and_op_index(operation_advance)

registers.line += sequence.line_base + (adjusted_opcode % sequence.line_range)
registers.line &+= sequence.line_base + (adjusted_opcode % sequence.line_range)
register_to_matrix(sequence, registers)
registers.reset
elsif opcode == 0
Expand Down Expand Up @@ -331,7 +330,7 @@ module Debug
operation_advance = DWARF.read_unsigned_leb128(@io)
increment_address_and_op_index(operation_advance)
when LNS::AdvanceLine
registers.line += DWARF.read_signed_leb128(@io)
registers.line &+= DWARF.read_signed_leb128(@io)
when LNS::SetFile
registers.file = DWARF.read_unsigned_leb128(@io)
when LNS::SetColumn
Expand Down
24 changes: 12 additions & 12 deletions src/digest/md5.cr
Original file line number Diff line number Diff line change
Expand Up @@ -103,27 +103,27 @@ class Digest::MD5 < Digest::Base
end

def ff(a, b, c, d, x, s, ac)
a += f(b, c, d) + x + ac.to_u32
a &+= f(b, c, d) &+ x &+ ac.to_u32
a = rotate_left a, s
a += b
a &+= b
end

def gg(a, b, c, d, x, s, ac)
a += g(b, c, d) + x + ac.to_u32
a &+= g(b, c, d) &+ x &+ ac.to_u32
a = rotate_left a, s
a += b
a &+= b
end

def hh(a, b, c, d, x, s, ac)
a += h(b, c, d) + x + ac.to_u32
a &+= h(b, c, d) &+ x &+ ac.to_u32
a = rotate_left a, s
a += b
a &+= b
end

def ii(a, b, c, d, x, s, ac)
a += i(b, c, d) + x + ac.to_u32
a &+= i(b, c, d) &+ x &+ ac.to_u32
a = rotate_left a, s
a += b
a &+= b
end

def transform(in)
Expand Down Expand Up @@ -201,10 +201,10 @@ class Digest::MD5 < Digest::Base
c = ii(c, d, a, b, in[2], S43, 718787259) # 63
b = ii(b, c, d, a, in[9], S44, 3951481745) # 64

@buf[0] += a
@buf[1] += b
@buf[2] += c
@buf[3] += d
@buf[0] &+= a
@buf[1] &+= b
@buf[2] &+= c
@buf[3] &+= d
end

def final
Expand Down
40 changes: 20 additions & 20 deletions src/digest/sha1.cr
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ class Digest::SHA1 < Digest::Base
e = @intermediate_hash[4]

{% for t in (0...20) %}
temp = circular_shift(5, a) +
((b & c) | ((~b) & d)) + e + w[{{t}}] + k[0]
temp = circular_shift(5, a) &+
((b & c) | ((~b) & d)) &+ e &+ w[{{t}}] &+ k[0]
e = d
d = c
c = circular_shift(30, b)
Expand All @@ -77,7 +77,7 @@ class Digest::SHA1 < Digest::Base
{% end %}

{% for t in (20...40) %}
temp = circular_shift(5, a) + (b ^ c ^ d) + e + w[{{t}}] + k[1]
temp = circular_shift(5, a) &+ (b ^ c ^ d) &+ e &+ w[{{t}}] &+ k[1]
e = d
d = c
c = circular_shift(30, b)
Expand All @@ -86,8 +86,8 @@ class Digest::SHA1 < Digest::Base
{% end %}

{% for t in (40...60) %}
temp = circular_shift(5, a) +
((b & c) | (b & d) | (c & d)) + e + w[{{t}}] + k[2]
temp = circular_shift(5, a) &+
((b & c) | (b & d) | (c & d)) &+ e &+ w[{{t}}] &+ k[2]
e = d
d = c
c = circular_shift(30, b)
Expand All @@ -96,19 +96,19 @@ class Digest::SHA1 < Digest::Base
{% end %}

{% for t in (60...80) %}
temp = circular_shift(5, a) + (b ^ c ^ d) + e + w[{{t}}] + k[3]
temp = circular_shift(5, a) &+ (b ^ c ^ d) &+ e &+ w[{{t}}] &+ k[3]
e = d
d = c
c = circular_shift(30, b)
b = a
a = temp
{% end %}

@intermediate_hash[0] += a
@intermediate_hash[1] += b
@intermediate_hash[2] += c
@intermediate_hash[3] += d
@intermediate_hash[4] += e
@intermediate_hash[0] &+= a
@intermediate_hash[1] &+= b
@intermediate_hash[2] &+= c
@intermediate_hash[3] &+= d
@intermediate_hash[4] &+= e

@message_block_index = 0
end
Expand All @@ -127,7 +127,7 @@ class Digest::SHA1 < Digest::Base
@length_low = 0_u32
@length_high = 0_u32
{% for i in 0...20 %}
message_digest[{{i}}] = (@intermediate_hash[{{i >> 2}}] >> 8 * (3 - ({{i & 0x03}}))).to_u8
message_digest[{{i}}] = (@intermediate_hash[{{i >> 2}}] >> 8 * (3 - ({{i & 0x03}}))).to_u8!
{% end %}

message_digest
Expand Down Expand Up @@ -157,14 +157,14 @@ class Digest::SHA1 < Digest::Base
end
end

@message_block[56] = (@length_high >> 24).to_u8
@message_block[57] = (@length_high >> 16).to_u8
@message_block[58] = (@length_high >> 8).to_u8
@message_block[59] = (@length_high).to_u8
@message_block[60] = (@length_low >> 24).to_u8
@message_block[61] = (@length_low >> 16).to_u8
@message_block[62] = (@length_low >> 8).to_u8
@message_block[63] = (@length_low).to_u8
@message_block[56] = (@length_high >> 24).to_u8!
@message_block[57] = (@length_high >> 16).to_u8!
@message_block[58] = (@length_high >> 8).to_u8!
@message_block[59] = (@length_high).to_u8!
@message_block[60] = (@length_low >> 24).to_u8!
@message_block[61] = (@length_low >> 16).to_u8!
@message_block[62] = (@length_low >> 8).to_u8!
@message_block[63] = (@length_low).to_u8!

process_message_block
end
Expand Down
2 changes: 1 addition & 1 deletion src/float/printer/grisu3.cr
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ module Float::Printer::Grisu3
# Since too_low = too_high - unsafe_interval this is equivalent to
# [too_high - unsafe_interval + 4 ulp; too_high - 2 ulp]
# Conceptually we have: rest ~= too_high - buffer
return (2 * unit <= rest) && (rest <= unsafe_interval - 4 * unit)
return (2 &* unit <= rest) && (rest <= unsafe_interval &- 4 &* unit)
end

# Generates the digits of input number *w*.
Expand Down
4 changes: 2 additions & 2 deletions src/float/printer/ieee.cr
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ module Float::Printer::IEEE
exp = 1 - EXPONENT_BIAS_64
else
frac = (d64 & SIGNIFICAND_MASK_64) + HIDDEN_BIT_64
exp = (((d64 & EXPONENT_MASK_64) >> PHYSICAL_SIGNIFICAND_SIZE_64) - EXPONENT_BIAS_64).to_i
exp = (((d64 & EXPONENT_MASK_64) >> PHYSICAL_SIGNIFICAND_SIZE_64) &- EXPONENT_BIAS_64).to_i!
end

{frac, exp}
Expand All @@ -166,7 +166,7 @@ module Float::Printer::IEEE
exp = 1 - EXPONENT_BIAS_32
else
frac = (d32 & SIGNIFICAND_MASK_32) + HIDDEN_BIT_32
exp = (((d32 & EXPONENT_MASK_32) >> PHYSICAL_SIGNIFICAND_SIZE_32) - EXPONENT_BIAS_32).to_i
exp = (((d32 & EXPONENT_MASK_32) >> PHYSICAL_SIGNIFICAND_SIZE_32) &- EXPONENT_BIAS_32).to_i!
end

{frac.to_u64, exp}
Expand Down
2 changes: 1 addition & 1 deletion src/hash.cr
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ class Hash(K, V)
copy = hasher
copy = key.hash(copy)
copy = value.hash(copy)
result += copy.result
result &+= copy.result
end

result.hash(hasher)
Expand Down
2 changes: 1 addition & 1 deletion src/iconv.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct Iconv

@iconv = LibC.iconv_open(to, from)

if @iconv.address == LibC::SizeT.new(-1)
if @iconv.address == ERROR
if Errno.value == Errno::EINVAL
if original_from == "UTF-8"
raise ArgumentError.new("Invalid encoding: #{original_to}")
Expand Down
2 changes: 1 addition & 1 deletion src/json/from_json.cr
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ end

{% for type in %w(Int8 Int16 Int32 Int64 UInt8 UInt16 UInt32 UInt64) %}
def {{type.id}}.new(pull : JSON::PullParser)
{{type.id}}.new(pull.read_int)
{{type.id}}.new!(pull.read_int)
asterite marked this conversation as resolved.
Show resolved Hide resolved
end
{% end %}

Expand Down
Loading