Commit graph

60 commits

Author SHA1 Message Date
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
Russ Cox
a3b799d9f0 rc: silence lion roar
R=rsc
http://codereview.appspot.com/4835048
2011-08-02 16:21:33 -04:00
Russ Cox
6886b3cd89 rc: fix $ifs bug introduced with utf-8 code
R=rsc
http://codereview.appspot.com/4187050
2011-02-16 12:48:06 -05:00
Russ Cox
0786c9647c rc: handle 4-byte utf-8
R=rsc
http://codereview.appspot.com/3833043
2011-01-02 13:44:15 -05:00
Russ Cox
72f66c2d3c rc: handle utf-8 in $ifs
R=rsc
http://codereview.appspot.com/3798046
2011-01-02 13:43:20 -05:00
Michael Teichgräber
38b62735e4 rc: implement and document <>{cmd} notation
http://codereview.appspot.com/105061
2009-09-13 18:26:51 -04:00
Michael Teichgräber
e066b12927 rc: make read not ignore interrupts/errors (again)
http://codereview.appspot.com/110042
2009-08-23 13:25:44 -04:00
Michael Teichgräber
74be46038d rc: fix segfault when SIGINT is received
Save the value of `runq' at the start of the function, so that the `pc'
update
at the end does work on that original value, and not on a probably
modified
value of `runq'.

fixes #14 http://code.swtch.com/plan9port/issue/14/

http://codereview.appspot.com/104066
2009-08-08 16:27:01 -04:00
Russ Cox
362264eb51 rc: fix local variables in functions
reported by micah stetson:

fn foo { echo $bar }
bar=baz foo
2008-08-14 10:29:29 -04:00
Russ Cox
94e1f2a438 rc: add subscript sequences (Erik Quanstrom) 2008-07-20 04:15:41 -04:00
rsc
fbc629a995 keep path and PATH in sync 2007-03-28 16:04:37 +00:00
rsc
2894c70c08 do not redefine rewind 2007-03-26 17:04:41 +00:00
rsc
40402738da more memory errors (valgrind) 2007-03-26 15:02:15 +00:00
rsc
4b241872ef fix wait 2007-03-26 14:26:32 +00:00
rsc
bd1b0cc17e fix phantom rc crashes 2007-03-26 14:24:44 +00:00
rsc
c8f538425f sync with plan 9 2007-03-26 12:02:41 +00:00
rsc
edc77f0b2b cope with programs that leave fd in non-blocking mode (Tim Wiess) 2007-03-25 17:16:40 +00:00
rsc
aec641d24c experiment - allow = in words late in the command line 2006-06-27 17:41:48 +00:00
rsc
1f2ab849c0 add exitcode 2006-04-08 23:49:08 +00:00
rsc
cbeb0b26e4 Use gcc -ansi -pedantic in 9c. Fix many non-C89-isms. 2006-04-01 19:24:03 +00:00
rsc
17157e4aa8 update lucida 2006-03-20 02:25:59 +00:00
rsc
7236e45bbc shut up about signals in scripts 2006-02-14 19:43:54 +00:00
rsc
f7174317de Add rfork builtin. 2005-08-11 16:44:43 +00:00
rsc
4ae2f414e2 make sure errors cause non-zero exit status 2005-08-11 16:44:18 +00:00
rsc
0e4068e8c4 fixes from bengt for sun 2005-07-26 10:19:23 +00:00
rsc
c6f92061c0 ignore window size change 2005-07-14 12:48:02 +00:00
rsc
7ce3f20d73 stupid sun 2005-07-13 03:54:01 +00:00
rsc
e3ffbf3b84 set $PLAN9 if necessary 2005-05-19 17:13:24 +00:00
rsc
4ee543e58c try harder to put background jobs in background; do not print in response to SIGPIPE 2005-03-18 19:03:25 +00:00
rsc
168518a993 correct command-printing bug 2005-03-18 18:54:54 +00:00
rsc
134c20c605 handle /dev/stdin always 2005-02-13 21:38:32 +00:00
rsc
26a5fd5725 set pid=-1 explicitly 2005-02-11 16:58:06 +00:00
rsc
de39860a2a more searchpath-related changes 2005-01-23 23:19:47 +00:00
rsc
1b0c8a154a use correct yacc 2005-01-19 19:48:58 +00:00
rsc
c8b6342d3c Many small edits. 2005-01-13 04:49:19 +00:00
rsc
a9eaaa03e0 maintain $path and $PATH simultaneously 2005-01-12 16:59:50 +00:00
rsc
005a85f3a2 success on the sun 2005-01-07 08:02:54 +00:00
rsc
f7b74c1725 FreeBSD tweaks 2004-12-28 17:34:05 +00:00
rsc
f002cc17a6 print out signalled exits 2004-10-17 05:29:53 +00:00
rsc
39cff6e750 add ulimit and umask as builtins 2004-10-17 05:19:53 +00:00
wkj
a87f4771c7 Supress line noise. 2004-05-16 07:58:58 +00:00
rsc
f1bfc54e12 More little bug fixes 2004-05-14 17:45:39 +00:00
rsc
c4097c2951 Fix small bugs. 2004-05-11 17:51:27 +00:00
rsc
b1455f33a8 Little fixes. 2004-04-30 02:16:28 +00:00
rsc
a2c2caaafe fix 2004-04-24 17:34:52 +00:00
rsc
8a3b2ceb0f Add scat. Temporary fix to rc r.e. note groups. 2004-04-24 17:05:43 +00:00
rsc
1b135a7805 clean up when finished.
don't set PLAN9
don't set PLAN9
2004-04-19 23:04:36 +00:00
rsc
69ab5d3d49 handle interrupts and backgrounded processes a little better. 2004-03-26 17:30:36 +00:00
rsc
be22ae2d07 SunOS can rot in hell. 2004-03-26 01:59:35 +00:00
rsc
8ad517944e Today's changes.
More changes.
2004-03-25 23:03:57 +00:00