Skip to content

C Compiler generates wrong code for while loop #1439

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

Closed
gopherbot opened this issue Jan 22, 2011 · 6 comments
Closed

C Compiler generates wrong code for while loop #1439

gopherbot opened this issue Jan 22, 2011 · 6 comments

Comments

@gopherbot
Copy link
Contributor

by [email protected]:

Before filing a bug, please check whether it has been fixed since
the latest release: run "hg pull -u" and retry what you did to
reproduce the problem.  Thanks.

What steps will reproduce the problem?
1. 6c -S this.c
--SNIP--
typedef unsigned long long uint64;
typedef char int8;
static void
puthex(uint64 n, uint64 l, int8* t) {
        static int8 hexdigits[] = "0123456789ABCDEF";
        while(l--) {
                *--t = hexdigits[n & 0xF];
                n >>= 4;
        }
}
--SNAP--
2. inspect output code
3. notice that an infinite loop (no conditional jump!) is generated

What is the expected output?
A conditional jump.

What do you see instead?
An infinite loop.

Which compiler are you using (5g, 6g, 8g, gccgo)?
6c

Which operating system are you using?
Linux toshiba 2.6.34-12-default #1 SMP 2010-06-29 02:39:08 +0200 i686 i686 i386 GNU/Linux


Which revision are you using?  (hg identify)
09ab865d875c+

Please provide any additional information below.
This code can be used to actually run it.
--SNIP--
void main·main(int argc, char** argv) {
        int8 buf[3] = {0, 0, 0};
        runtime·prints("Hello, World\n");
        puthex(0x42, 2, buf+2);
        runtime·prints(buf);
}

void main·init() {
}
--SNAP--
No surprise, it crashes.
A similar program works fine with GCC (ugh feels bad to say that).
@gopherbot
Copy link
Contributor Author

Comment 1 by [email protected]:

I just noticed that a simple
        while(x) {x--;}
is sufficient to trigger the bug.

@gopherbot
Copy link
Contributor Author

Comment 2 by [email protected]:

I just noticed that a simple
        while(x) {x--;}
with uint64 x is sufficient to trigger the bug.

@robpike
Copy link
Contributor

robpike commented Jan 24, 2011

Comment 3:

Owner changed to [email protected].

Status changed to Accepted.

@gopherbot
Copy link
Contributor Author

Comment 4 by ejsherry:

I think this has been fixed.
http://code.google.com/p/go/source/detail?r=bb2d8b3cdc

@gopherbot
Copy link
Contributor Author

Comment 5 by [email protected]:

Indeed, this seems to be fixed.
I apologize for the inconvenience

@rsc
Copy link
Contributor

rsc commented Jan 27, 2011

Comment 6:

Status changed to Retracted.

@golang golang locked and limited conversation to collaborators Jun 24, 2016
@rsc rsc unassigned ken Jun 22, 2022
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants