Skip to content

Commit

Permalink
Fix bounds of singed integers for overflow checking (#795)
Browse files Browse the repository at this point in the history
* add example of bug

* fix bug

* fix
  • Loading branch information
jcp19 authored Nov 16, 2024
1 parent e70a229 commit 97f0988
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/viper/gobra/util/TypeBounds.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ object TypeBounds {
}

sealed trait Signed extends BoundedIntegerKind {
override lazy val upper: BigInt = BigInt(pow(2, nbits-1).toLong - 1)
override lazy val lower: BigInt = BigInt(-pow(2, nbits-1).toLong)
override lazy val upper: BigInt = BigInt(2).pow(nbits-1) - 1
override lazy val lower: BigInt = -BigInt(2).pow(nbits-1)
}

sealed trait Unsigned extends BoundedIntegerKind {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/

package main

// ##(--overflow)

const MinInt64 = -9223372036854775808 // = -1 << 63
// @ requires x > MinInt64
// @ ensures res >= 0
func abs(x int64) (res int64) {
if x < 0 {
return -x
} else {
return x
}
}

0 comments on commit 97f0988

Please sign in to comment.