No description
There are two bugs in pdec() on INT_MIN:
* wrong output.
`n = 1-n' should be `n = -1-n' when n is INT_MIN.
* infinite loop.
gcc optimizes `if(n>=0)' into `if(true)' because `-INT_MIN' (signed integer overflow) is undefined behavior in C, and gcc assumes the negation of a negative number must be positive. The resulting binary keeps printing '-' forever given INT_MIN.
Try the simplified pdec.c below.
$ gcc pdec.c
$ ./a.out -2147483648
--214748364*
$ gcc pdec.c -O2
$ ./a.out -2147483648
<infinite loop>
$ gcc pdec.c -O2 -D__PATCH__
$ ./a.out -2147483648
-2147483648
=== pdec.c ===
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#define io void
void pchr(io *f, int c)
{
putchar(c);
}
void pdec(io *f, int n)
{
if(n<0){
#ifndef __PATCH__
n=-n;
if(n>=0){
pchr(f, '-');
pdec(f, n);
return;
}
/* n is two's complement minimum integer */
n = 1-n;
#else
if(n!=INT_MIN){
pchr(f, '-');
pdec(f, -n);
return;
}
/* n is two's complement minimum integer */
n = -(INT_MIN+1);
#endif
pchr(f, '-');
pdec(f, n/10);
pchr(f, n%10+'1');
return;
}
if(n>9)
pdec(f, n/10);
pchr(f, n%10+'0');
}
int main(int argc, char **argv)
{
int n = atoi(argv[1]);
pdec(NULL, n);
putchar('\n');
}
R=rsc
CC=plan9port.codebot
https://codereview.appspot.com/7241055
|
||
|---|---|---|
| acid | ||
| bin | ||
| dict | ||
| dist | ||
| face | ||
| font | ||
| include | ||
| lib | ||
| log | ||
| lp | ||
| mac | ||
| man | ||
| ndb | ||
| news | ||
| plumb | ||
| postscript | ||
| proto | ||
| sky | ||
| src | ||
| tmac | ||
| troff | ||
| unix | ||
| .cvsignore | ||
| .hgignore | ||
| .hgtags | ||
| CHANGES | ||
| configure | ||
| CONTRIBUTORS | ||
| INSTALL | ||
| install.txt | ||
| LICENSE | ||
| Makefile | ||
| rcmain | ||
| README | ||
| TODO | ||
This is a port of many Plan 9 libraries and programs to Unix.
* Installation
To install, run ./INSTALL. It builds mk and then uses mk to
run the rest of the installation.
For more details, see install(1), at install.txt in this directory
and at http://swtch.com/plan9port/man/man1/install.html.
* Documentation
See http://swtch.com/plan9port/man/ for more documentation.
(Documentation is also in this tree, but you need to run
a successful install first. After that, "9 man 1 intro".)
Intro(1) contains a list of man pages that describe new features
or differences from Plan 9.
* Helping out
If you'd like to help out, great! The TODO file contains a small list.
If you port this code to other architectures, please share your changes
so others can benefit.
Please use diff -u or CVS (see below) to prepare patches.
* CVS
You can use CVS to keep your local copy up-to-date as we make
changes and fix bugs. See the cvs(1) man page here ("9 man cvs")
for details on using cvs.
* Contact
Russ Cox <rsc@swtch.com>