No description
Find a file
Xi Wang 1bfec89b99 rc: avoid undefined C
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
2013-03-19 14:36:50 -04:00
acid merge 2012-03-05 16:36:31 -05:00
bin 9c: support clang on Mac 2012-10-21 12:01:13 -04:00
dict now with hget 2005-07-18 22:52:05 +00:00
dist INSTALL: don't try to use \b in acme window 2012-10-20 13:41:25 -04:00
face faces for lp. 2005-09-13 01:24:50 +00:00
font font: add peter face to font/times/*.font 2009-08-24 08:05:09 -07:00
include libdraw: change DefaultDPI to 133 2012-11-26 00:13:04 -05:00
lib fix clang warnings reported by Tuncer Ayaz 2012-10-21 11:25:08 -04:00
log ignore everything 2005-02-13 22:09:54 +00:00
lp lp: move devices to devices.sample 2009-04-30 07:28:37 -07:00
mac Plumb.app: prevent the Finder's activation after Devdraw's 2012-01-16 17:04:52 -05:00
mail merge 2007-11-27 15:39:06 -05:00
man xd: accept -S for 8-byte swap 2013-03-11 17:26:11 -04:00
ndb ndb/local: remove rsc-specific entries (oops) 2008-06-04 13:28:05 -04:00
news odds and ends 2004-04-21 23:43:46 +00:00
plumb plumb: character offsets start at 1, not 0 2011-08-31 09:22:22 -04:00
postscript troff: yet another case problem (OS X) 2008-02-01 07:54:19 -05:00
proto forgotten files 2005-01-13 04:56:07 +00:00
sky add here 2005-01-27 19:28:22 +00:00
src rc: avoid undefined C 2013-03-19 14:36:50 -04:00
tmac tmac.an: insert spaces after .?? request names 2008-11-11 11:01:03 -08:00
troff tr2post: rename charlib files to not be case-dependent (OS X) 2008-01-31 16:58:38 -05:00
unix unix: move Makefile so that it cannot be run accidentally 2009-08-24 07:53:01 -07:00
.cvsignore merge 2007-11-27 15:39:06 -05:00
.hgignore devdraw: restore compilation on OS X 10.6 2012-11-25 21:20:18 -05:00
.hgtags Added tag rtag for changeset 0b0321e26f56 2007-06-24 20:05:05 -04:00
CHANGES CHANGES: add pointer to web version 2008-05-10 13:38:21 -04:00
configure For annoying people like Uriel. 2005-11-25 13:18:11 +00:00
CONTRIBUTORS CONTRIBUTORS: three more 2013-01-30 17:45:21 -08:00
INSTALL fontserv: fix build on FreeBSD 9.1 2013-01-30 17:45:28 -08:00
install.txt install: typo in man page 2009-04-30 08:30:49 -07:00
LICENSE allow less restricted use of plan9port-specific code 2009-08-14 21:08:23 -07:00
Makefile For annoying people like Uriel. 2005-11-25 13:18:11 +00:00
rcmain Make fn cd safe for rc -e. 2005-08-10 16:59:19 +00:00
README add text version of install(1) 2005-11-28 16:36:57 +00:00
TODO Note 9P2000.u 2005-08-31 04:00:14 +00:00

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>