[comp.lang.c] Constant overflow

franka@mmintl.UUCP (Frank Adams) (03/04/88)

[Not food]

Try this one on your favorite C compiler.  So far, every one I've tried
(including lint) has had no complaint:

	long i = 9876543210L;

(The problem, of course, is that that number doesn't fit in 32 bits.  If
your compiler has a word size of more than 32 bits, try something big
enough to overflow whatever it uses.)
-- 

Frank Adams                           ihnp4!philabs!pwa-b!mmintl!franka
Ashton-Tate          52 Oakland Ave North         E. Hartford, CT 06108

decot@hpisod2.HP.COM (Dave Decot) (03/07/88)

> Try this one on your favorite C compiler.  So far, every one I've tried
> (including lint) has had no complaint:
> 
> 	long i = 9876543210L;
> 
> (The problem, of course, is that that number doesn't fit in 32 bits...)

My compiler and lint don't complain either.

I presume this phenomenon is widespread because the algorithm used for
lex'ing such constants is almost always something like "keep eating
digits and multiplying by 10 (or 8 or 16 or 2) until no more digits".
Optionally, this may include "...or until too many digits have been
processed", but that doesn't help this particular case, since there
aren't too many digits, just too much value.

It's hard to do this right unless you do some clever comparison
just before multiplying by 10 (or 8 or 16 or 2).

Dave Decot
hpda!decot

meissner@xyzzy.UUCP (Michael Meissner) (03/08/88)

In article <2747@mmintl.UUCP> franka@mmintl.UUCP (Frank Adams) writes:

| Try this one on your favorite C compiler.  So far, every one I've tried
| (including lint) has had no complaint:
| 
| 	long i = 9876543210L;
| 
| (The problem, of course, is that that number doesn't fit in 32 bits.  If
| your compiler has a word size of more than 32 bits, try something big
| enough to overflow whatever it uses.)

Ok, on the Data General C compiler, you get:

Error 104 severity 2 beginning on line 1 (Line 1 of file foo.c) 
long i = 9876543210L;
         ^
The int constant "9876543210L" could not fit into 32 bits without
a loss of precision.  The compiler truncated it on the left.
-- 
Michael Meissner, Data General.		Uucp: ...!mcnc!rti!xyzzy!meissner
					Arpa/Csnet:  meissner@dg-rtp.DG.COM

cg@myrias.UUCP (Chris Gray) (03/09/88)

Similar, on the Myrias Parallel C compiler for the following input

long i = 9876543210L;

you get:

"test.c", line 1: (20) overflow in integral constant

The (20) is the column number where the error was noticed. We stuck with
the standard error message format so the magic tools like Emacs would still
work.

-- 
Chris Gray		Myrias Research, Edmonton	+1 403 428 1616
	{uunet!mnetor,ubc-vision,watmath,vax135}!alberta!myrias!cg

alan@bk.dk (Alan_Rooks_sps) (03/11/88)

In article <2550059@hpisod2.HP.COM> decot@hpisod2.HP.COM (Dave Decot) writes:

>It's hard to do this right unless you do some clever comparison
>just before multiplying by 10 (or 8 or 16 or 2).

A proven (and very simple) way to do this right was given by J. R. Parker in
"A General Character to Integer Conversion Method", Software - Practice and
Experience, Vol 15(8) (August '85), pp. 761-766.

Alan Rooks - Bruel & Kjaer Copenhagen - ...!mcvax!bk!alan or ...!diku!bk!alan

llave@phoenix.Princeton.EDU (Rafael Llave) (03/13/88)

In article <2550059@hpisod2.HP.COM>, decot@hpisod2.HP.COM (Dave Decot) writes:
 > Try this one on your favorite C compiler.  So far, every one I've tried
 > (including lint) has had no complaint:
 > 
 > 	long i = 9876543210L;
 > 
 > (The problem, of course, is that that number doesn't fit in 32 bits...)

Integer arithmetic is not supposed to overflow and all
integers are  supposed to be taken ss defined modulo a power of 2 which
is implementation dependent. I have seen 
some compilers that at compile time gave you warnings - God
bless them -- but to ensure portability of programs that
have to handle  occasionally large numbers , I have always
included some defensive lines. 

steve@endor.harvard.edu (Kaufer - Lopez - Pratap) (03/13/88)

Here is the output from the Saber-C programming environment:
(Running on SUN & VAX,  Ultrix/Unix):

                        Saber-C - Version 1.2c
        Copyright (C) 1986, 1987, 1988 by Saber Software, Inc.

Attaching: /lib/libc.a
1 -> load("foo.c");
Loading: foo.c
----------------
"foo.c", line 2: Integer too large (Warning #220)
    1: /* Sample overflow */
    2: long i = 9876543210L;
                ^
    3: 
Integer constant (9876543210) exceeds maximum unsigned long (4294967295).
Options: continue/suppress/terse/quit/abort/edit/reload [c] ?

	Stephen Kaufer
	Saber Software, Inc.
	saber@harvard.harvard.edu
	30 JFK Street, Cambridge, MA.  02138
	(617) 876-7636

john@uw-nsr.UUCP (John Sambrook) (03/14/88)

In article <2747@mmintl.UUCP> franka@mmintl.UUCP (Frank Adams) writes:
>
>Try this one on your favorite C compiler.  So far, every one I've tried
>(including lint) has had no complaint:
>
>	long i = 9876543210L;
>
>(The problem, of course, is that that number doesn't fit in 32 bits.  If
>your compiler has a word size of more than 32 bits, try something big
>enough to overflow whatever it uses.)

Ok, here is the output I get on our DG MV/10000 running DG/UX 3.11 and
DG/UX C 3.90.14:

Script started on Sun Mar 13 11:18:20 1988
% cat main.c

main()
{
        long i = 9876543210L;
}

% cc main.c -o main
Error 104 severity 2 beginning on line 4 (Line 4 of file main.c)
        long i = 9876543210L;
                 ^
The int constant "9876543210L" could not fit into 32 bits without
a loss of precision.  The compiler truncated it on the left.
 
% ^D
Script done at Sun Mar 13 11:19:06 1988
    
-- 
John Sambrook                        Internet: john@nsr.bioeng.washington.edu
University of Washington RC-05           UUCP: uw-nsr!john
Seattle, Washington  98195               Dial: (206) 548-4386