Jump to page: 1 2
Thread overview
[Issue 4835] New: DMD should warn about integer overflow in computed constant
Sep 07, 2010
Lars Holowko
Feb 06, 2011
Brad Roberts
Mar 26, 2013
Luís Marques
Mar 26, 2013
Walter Bright
Mar 27, 2013
Walter Bright
Mar 27, 2013
Luís Marques
Mar 27, 2013
Walter Bright
Mar 27, 2013
Luís Marques
Mar 27, 2013
Walter Bright
Mar 27, 2013
Luís Marques
Mar 28, 2013
Walter Bright
September 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4835

           Summary: DMD should warn about integer overflow in computed
                    constant
           Product: D
           Version: D2
          Platform: x86_64
        OS/Version: Linux
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: lars.holowko@gmail.com


--- Comment #0 from Lars Holowko <lars.holowko@gmail.com> 2010-09-07 10:09:23 PDT ---
I got bitten by this when I wanted to create a 6GB large file to test 64 bit support in std.stdio with dmd 2.048.


The output of:


import std.stdio;

void main(string args[])
{
    long l_dangerous = 1024 * 1024 * 1024 * 6;
    writefln("l_dangerous = 0x%x", l_dangerous);
    writefln("l_dangerous = %s", l_dangerous);

    long l_ok = 1024 * 1024 * 1024 * 6L;
    writefln("l_ok = 0x%x", l_ok);
    writefln("l_ok = %s", l_ok);
    return;
}

is

l_dangerous = 0xffffffff80000000
l_dangerous = -2147483648
l_ok = 0x180000000
l_ok = 6442450944


dmd 2.048 did not issue a warning about the integer overflow (neither with or
without -w)

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
September 07, 2010
http://d.puremagic.com/issues/show_bug.cgi?id=4835


bearophile_hugs@eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs@eml.cc


--- Comment #1 from bearophile_hugs@eml.cc 2010-09-07 12:39:13 PDT ---
I'm asking for overflow detection for years (both at compile-time and
run-time).

Again here the C language is better than the D language:

// C code
#include "stdio.h"
int main() {
    long long x = 1024 * 1024 * 1024 * 6;
    printf("%lld\n", x);
    return 0;
}


GCC 4.3.4 gives:
prog.c: In function ‘main’:
prog.c:3: warning: integer overflow in expression

D compiler is not _practical_ enough yet.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
February 06, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=4835


Brad Roberts <braddr@puremagic.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Platform|x86_64                      |x86


--- Comment #2 from Brad Roberts <braddr@puremagic.com> 2011-02-06 15:38:57 PST ---
Mass migration of bugs marked as x86-64 to just x86.  The platform run on isn't what's relevant, it's if the app is a 32 or 64 bit app.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 26, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4835



--- Comment #3 from bearophile_hugs@eml.cc 2013-03-26 11:27:23 PDT ---
An example in Go language:


package main
func main() {
    var x uint32 = 1 << 40
    var y = 1024 * 1024 * 1024 * 6
}


The Go compiler gives the errors:

test.go:3: constant 1099511627776 overflows uint32 test.go:4: constant 6442450944 overflows int

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 26, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4835



--- Comment #4 from bearophile_hugs@eml.cc 2013-03-26 11:29:45 PDT ---
Another example in Go:


package main
func main() {
    var x uint64 = 1 << 90
}


The Go compiler gives the error:

test.go:3: constant 1237940039285380274899124224 overflows uint64

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 26, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4835


Luís Marques <luismarques@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |luismarques@gmail.com


--- Comment #5 from Luís Marques <luismarques@gmail.com> 2013-03-26 11:50:04 PDT ---
(In reply to comment #4)

For completeness, it also does not have a problem (like in your example) where
the initializer of the 64-bit variable is initialized with (what is not
obvious) a folded and truncated 32-bit int:

    var x uint64 = 4 * 1024 * 1024 * 1024;
    // x is now 4294967296, not 0.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 26, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4835


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bugzilla@digitalmars.com


--- Comment #6 from Walter Bright <bugzilla@digitalmars.com> 2013-03-26 13:14:08 PDT ---
uint foo() {
    uint x = 1 << 40;
    return 1 << 90;
}

gives:

foo2.d(3): Error: shift by 40 is outside the range 0..31
foo2.d(4): Error: shift by 90 is outside the range 0..31

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 26, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4835



--- Comment #7 from bearophile_hugs@eml.cc 2013-03-26 15:13:56 PDT ---
(In reply to comment #6)
> uint foo() {
>     uint x = 1 << 40;
>     return 1 << 90;
> }
> 
> gives:
> 
> foo2.d(3): Error: shift by 40 is outside the range 0..31
> foo2.d(4): Error: shift by 90 is outside the range 0..31

I was aware of that. I have added the 1<<90 example to show the peculiar error message given by Go, that contains 1237940039285380274899124224, that is larger than a ulong.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 27, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4835



--- Comment #8 from Walter Bright <bugzilla@digitalmars.com> 2013-03-26 19:06:55 PDT ---
https://github.com/D-Programming-Language/dmd/pull/1803

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
March 27, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4835



--- Comment #9 from bearophile_hugs@eml.cc 2013-03-26 19:45:01 PDT ---
(In reply to comment #8)
> https://github.com/D-Programming-Language/dmd/pull/1803

An error, even :-) So the issue name should be updated.

Thank you Walter. Let's see how this patch goes.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2