Running the code in the link below times out, is this a bug?
Looking at the assembly, it seems that the instructions corresponding to 
the if statement have been removed due to optimization.

https://go.dev/play/p/CZX4mbyrp37

The following is a description of how I thought it was a bug.

In the first loop, the value of i is 9223372036854775807 (the maximum value 
of type int), which is incremented and overflows. Then it becomes 
-9223372036854775808 (minimum value of type int) and I thought the 
condition in the if statement exits from the true neighbor loop.

I couldn't figure out where the bug was happening, so I decided to look at 
an assembly of the same code as above, built it locally, and checked it 
with objdump.
Perhaps, the CMP instruction that compares the conditions of the if 
statement has been removed due to optimization.

```
...
TEXT main.main(SB) Path/main.go
  main.go:3 0x100051550 92f00000 MOVD $9223372036854775807, R0
  main.go:4 0x100051554 14000002 JMP 2(PC)
  main.go:4 0x100051558 91000400 ADD $1, R0, R0
  main.go:4 0x10005155c 92f0001b MOVD $9223372036854775807, R27
  main.go:4 0x100051560 eb1b001f CMP R27, R0
  main.go:4 0x100051564 54ffffad BLE -3(PC)
  main.go:9 0x100051568 d65f03c0 RET
  main.go:9 0x10005156c 00000000 ? 
```

So I compiled it without optimization (go build -gcflags '-N -l') and the 
program terminated immediately as intended.

I may have misunderstood something, may I ask anyone to check once?

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/c4f21dc1-d450-42c8-996f-4f8f1461a9c2n%40googlegroups.com.

Reply via email to