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

succ raise OverflowDefect, succ does not handle unsigned integers correctly #23894

Closed
haoyu234 opened this issue Jul 26, 2024 · 1 comment · Fixed by #23895
Closed

succ raise OverflowDefect, succ does not handle unsigned integers correctly #23894

haoyu234 opened this issue Jul 26, 2024 · 1 comment · Fixed by #23895
Assignees

Comments

@haoyu234
Copy link
Contributor

Description

succ does not handle unsigned integers correctly.

let v = high(uint) div 2
echo v # 9223372036854775807
echo v + 1 # 9223372036854775808
echo succ v # Error: unhandled exception: over- or underflow [OverflowDefect]
N_LIB_PRIVATE NIM_CONST NU v__t1_u5 = ((NU)IL64(9223372036854775807));
nimln_(16);	colontmpD__2 = dollar___systemZdollars_u14(((NU64) ((NU)((NU64)(v__t1_u5) + (NU64)(((NU)1))))));
nimln_(17);	if (nimAddInt(v__t1_u5, ((NI)1), &TM__RKFvtxDChgOSG9c3eaEfBVA_2)) { raiseOverflow(); goto LA1_;

https://play.nim-lang.org/#pasty=XDlRYyZz

Nim Version

Nim Compiler Version 2.1.9 [Linux: amd64]
Compiled at 2024-07-25
Copyright (c) 2006-2024 by Andreas Rumpf

git hash: c1f91c2
active boot switches: -d:release

Current Output

9223372036854775807
9223372036854775808
/root/repos/iobuf/t1.nim(17) t1
/root/.choosenim/toolchains/nim-#devel/lib/system/fatal.nim(53) sysFatal
Error: unhandled exception: over- or underflow [OverflowDefect]

Expected Output

9223372036854775807
9223372036854775808
9223372036854775808

Possible Solution

No response

Additional Information

No response

@ringabout ringabout self-assigned this Jul 26, 2024
@haoyu234
Copy link
Contributor Author

The following changes seem to fix the issue.

  #define nimAddInt64(a, b, res) __builtin_add_overflow(a, b, res)
  #define nimSubInt64(a, b, res) __builtin_sub_overflow(a, b, res)
  #define nimMulInt64(a, b, res) __builtin_mul_overflow(a, b, res)

  #define nimAddInt(a, b, res) __builtin_add_overflow(a, b, res)
  #define nimSubInt(a, b, res) __builtin_sub_overflow(a, b, res)
  #define nimMulInt(a, b, res) __builtin_mul_overflow(a, b, res)

the test:

discard """
  targets: "c cpp"
"""

# Bug #23894

proc checkSucc(tp: typedesc) =
  let v = high(tp)
  let v2 = tp(uint64(v) div uint64(2))
  let v3 = succ v2
  discard v3

proc checkSuccOverflow(tp: typedesc) =
  let v = high(tp)
  try:
    let v2 = succ v
    discard v2
  except OverflowDefect:
    return

  doAssert false, "unreachable"

block: # overflow

  checkSuccOverflow(char)
  checkSuccOverflow(cint)
  checkSuccOverflow(cuint)
  checkSuccOverflow(csize_t)

  checkSuccOverflow(uint)
  checkSuccOverflow(uint8)
  checkSuccOverflow(uint16)
  checkSuccOverflow(uint32)
  checkSuccOverflow(uint64)

  checkSuccOverflow(int)
  checkSuccOverflow(int8)
  checkSuccOverflow(int16)
  checkSuccOverflow(int32)
  checkSuccOverflow(int64)

  checkSuccOverflow(cint)
  checkSuccOverflow(cuint)
  checkSuccOverflow(csize_t)

block: # should't overflow

  checkSucc(char)
  checkSucc(cint)
  checkSucc(cuint)
  checkSucc(csize_t)

  checkSucc(uint)
  checkSucc(uint8)
  checkSucc(uint16)
  checkSucc(uint32)
  checkSucc(uint64)

  checkSucc(int)
  checkSucc(int8)
  checkSucc(int16)
  checkSucc(int32)
  checkSucc(int64)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants