1773 – excessively long integer literal

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 1773 - excessively long integer literal
Summary: excessively long integer literal
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Linux
: P2 normal
Assignee: Walter Bright
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
Reported: 2008年01月07日 23:14 UTC by Ap Lee
Modified: 2014年02月24日 15:33 UTC (History)
0 users

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Ap Lee 2008年01月07日 23:14:50 UTC
An excessively long integer literal is accepted by the compiler, as show in this simple example, where a LargeInt struct instance is declared with a capacity of 256 bits:
LargeInt!(256) v = 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF;
Under the hood, this assignment uses the opAssign overload I created for ulong. Obviously, ulong is only 64 bits in size, and only the lowest 64 bits of the "v" variable get set to 1, while the upper bits remain at 0. Also obvious is that a 256-bit integer literal is not supposed to be supported. The prioblem is that this code is accepted by the compiler, leading to beleive that all 256 bits are set to 1 when in reality they aren't.
I experimented a little bit and found that 0xf_ffff_ffff_ffff_ffff also compiled, but 0x1_0000_0000_0000_0000 did not compile.
This is with gdc 0.24, using dmd 1.020
Comment 1 Walter Bright 2008年01月16日 04:57:32 UTC
I get:
 test.d(1): integer overflow
for:
 ulong v = 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF;
I could not reproduce the problem (I don't have your definition of LargeInt()) with dmd 1.025.
Comment 2 Ap Lee 2008年01月17日 21:06:03 UTC
Sorry, LargeInt is not necessary. I have found that the problem is only with dmd (or gdc) on linux:
$ ./dmd
Digital Mars D Compiler v1.025
...
$ cat t.d
ulong v = 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF;
ulong v = 0x1_0000_0000_0000_0000;
void main() {}
$ ./dmd t.d
t.d(2): integer overflow
Line 1 was not rejected by the compiler. On Windows, both lines are rejected.
Comment 3 Ap Lee 2008年01月17日 22:14:33 UTC
I have been able to characterize the problem further by running a piece of the lexer as a standalone C program:
#include <stdio.h>
#include <stdint.h>
void main() {
 int cnt = 17;
 uint64_t n;
 int r = 16, d;
 printf("Walter's parsing 0xF_FFFF_FFFF_FFFF_FFFF\n");
 n = 0;
 cnt = 17;
 while(cnt--) {
 d = 15; // 0xF_FFFF_FFFF_FFFF_FFFF;
 if (d >= r) break;
 printf("Condition value %llu < %llu\n",n * r + d,n);
 if (n * r + d < n) {
 printf("Overflow\n");
 break;
 }
 n = n * r + d;
 printf("%0d: n = %llu\n",cnt,n);
 }
 printf("Walter's parsing 0x1_0000_0000_0000_0000\n");
 n = 0;
 cnt = 17;
 while(cnt--) {
 if (cnt == 16) d = 1; // 0x1_0000_0000_0000_0000;
 else d = 0;
 if (d >= r) break;
 printf("Condition value %llu < %llu\n",n * r + d,n);
 if (n * r + d < n) {
 printf("Overflow\n");
 break;
 }
 n = n * r + d;
 printf("%0d: n = %llu\n",cnt,n);
 }
}
In running this program which mimics the parsing of the two integer literals shown above, the problem becomes obvious. It is in the conditional. I do not know the solution yet.
Comment 4 Walter Bright 2008年02月16日 06:06:53 UTC
Fixed dmd 1.026 and 2.010
Comment 5 Ap Lee 2008年02月23日 07:35:04 UTC
$ cat t.d
ulong v1 = 0xFFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF;
ulong v2 = 0x1_0000_0000_0000_0000;
ulong v3 = 0x1_FFFF_FFFF_FFFF_FFFF;
ulong v4 = 0x7_FFFF_FFFF_FFFF_FFFF;
ulong v5 = 0x1_0000_FFFF_FFFF_FFFF;
void main() {}
$ ./dmd t.d
t.d(1): integer overflow
t.d(2): integer overflow
t.d(5): integer overflow
$ ./dmd -v
Digital Mars D Compiler v1.026
v3 and v4 are also overflows but D does not see that.
The problem is that any input stream that fills in the r*n+d will all 1s is going to be a problem. I was not able to come up with a solution different than the one explained in the strtol.c file which can be found everywhere on the internet, which uses a cutoff value between legal numbers and illegal numbers.
Comment 6 Walter Bright 2008年09月03日 01:34:22 UTC
Fixed dmd 1.035 and 2.019


AltStyle によって変換されたページ (->オリジナル) /