Thread overview | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 16, 2013 [Issue 10093] New: wrong unsigned arithmetic | ||||
---|---|---|---|---|
| ||||
http://d.puremagic.com/issues/show_bug.cgi?id=10093 Summary: wrong unsigned arithmetic Product: D Version: D2 Platform: All OS/Version: All Status: NEW Severity: normal Priority: P2 Component: DMD AssignedTo: nobody@puremagic.com ReportedBy: luka8088@owave.net --- Comment #0 from luka8088 <luka8088@owave.net> 2013-05-16 06:29:40 PDT --- The following code fails: static assert(-("foo".length) < 0); -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
May 16, 2013 [Issue 10093] wrong unsigned arithmetic | ||||
---|---|---|---|---|
| ||||
Posted in reply to luka8088 | http://d.puremagic.com/issues/show_bug.cgi?id=10093 Steven Schveighoffer <schveiguy@yahoo.com> changed: What |Removed |Added ---------------------------------------------------------------------------- Status|NEW |RESOLVED CC| |schveiguy@yahoo.com Resolution| |INVALID --- Comment #1 from Steven Schveighoffer <schveiguy@yahoo.com> 2013-05-16 06:37:28 PDT --- This is expected, array length is an unsigned property. unsigned math results in unsigned result. You are basically saying: static assert(-3u < 0) 0 is promoted to unsigned, and 0xff_ff_ff_fd is compared to it. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
May 16, 2013 [Issue 10093] wrong unsigned arithmetic | ||||
---|---|---|---|---|
| ||||
Posted in reply to luka8088 | http://d.puremagic.com/issues/show_bug.cgi?id=10093 9999 <sibaqexozequgaba@tempomail.fr> changed: What |Removed |Added ---------------------------------------------------------------------------- CC| |sibaqexozequgaba@tempomail. | |fr --- Comment #2 from 9999 <sibaqexozequgaba@tempomail.fr> 2013-05-16 10:36:20 PDT --- Should a negative unsigned number even compile? As far as I know, Visual C++ issues a warning in that case. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
May 16, 2013 [Issue 10093] wrong unsigned arithmetic | ||||
---|---|---|---|---|
| ||||
Posted in reply to luka8088 | http://d.puremagic.com/issues/show_bug.cgi?id=10093 --- Comment #3 from Steven Schveighoffer <schveiguy@yahoo.com> 2013-05-16 11:11:38 PDT --- (In reply to comment #2) > Should a negative unsigned number even compile? Debatable, but since it compiles in C, and is frequently used (-1u is 0xffffffff), I think it will continue to compile. > As far as I know, Visual C++ issues a warning in that case. But still compiles, right? C is full of questionable, yet valid, behavior. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
May 16, 2013 [Issue 10093] wrong unsigned arithmetic | ||||
---|---|---|---|---|
| ||||
Posted in reply to luka8088 | http://d.puremagic.com/issues/show_bug.cgi?id=10093 --- Comment #4 from 9999 <sibaqexozequgaba@tempomail.fr> 2013-05-16 11:25:00 PDT --- Why would you ever do -1u? We don't talk about implicitly converting -1 to unsigned, right? That's a different case. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
May 16, 2013 [Issue 10093] wrong unsigned arithmetic | ||||
---|---|---|---|---|
| ||||
Posted in reply to luka8088 | http://d.puremagic.com/issues/show_bug.cgi?id=10093 --- Comment #5 from Steven Schveighoffer <schveiguy@yahoo.com> 2013-05-16 11:44:46 PDT --- (In reply to comment #4) > Why would you ever do -1u? Shortcut. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
May 16, 2013 [Issue 10093] wrong unsigned arithmetic | ||||
---|---|---|---|---|
| ||||
Posted in reply to luka8088 | http://d.puremagic.com/issues/show_bug.cgi?id=10093 --- Comment #6 from 9999 <sibaqexozequgaba@tempomail.fr> 2013-05-16 11:58:25 PDT --- > We don't talk about implicitly converting -1 to unsigned, right? That's a different case. ^ Am I missing something? ^ We're not talking about: func: void SetText(char* text, uint len); call: SetText(text, -1); ^ here -1 is a special constant meaning e.g. calculate the len of a null terminated string. We're talking about: uint len = strlen(text); // ... Func(-len); // Why would you ever need this? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
May 16, 2013 [Issue 10093] wrong unsigned arithmetic | ||||
---|---|---|---|---|
| ||||
Posted in reply to luka8088 | http://d.puremagic.com/issues/show_bug.cgi?id=10093 --- Comment #7 from Steven Schveighoffer <schveiguy@yahoo.com> 2013-05-16 12:44:36 PDT --- (In reply to comment #6) > We're talking about: > uint len = strlen(text); > // ... > Func(-len); // Why would you ever need this? What about Func(1 - len) The compiler can't cover every case. If you want to propose something to make this illegal, go ahead, but I doubt you will get traction. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
May 16, 2013 [Issue 10093] wrong unsigned arithmetic | ||||
---|---|---|---|---|
| ||||
Posted in reply to luka8088 | http://d.puremagic.com/issues/show_bug.cgi?id=10093 --- Comment #8 from luka8088 <luka8088@owave.net> 2013-05-16 12:49:27 PDT --- (In reply to comment #6) > > We don't talk about implicitly converting -1 to unsigned, right? That's a different case. > ^ Am I missing something? ^ > > We're not talking about: > func: > void SetText(char* text, uint len); > call: > SetText(text, -1); > ^ here -1 is a special constant meaning e.g. calculate the len of a null > terminated string. > > We're talking about: > uint len = strlen(text); > // ... > Func(-len); // Why would you ever need this? The original issue was: auto offset = text1.length - text2.length; func(offset); and offset turned out to be around 4294967291 I was thinking, setting a uint to a negative value is kind of an overflow, should it maybe be treated the same way like array bounds and be checked by druntime (with optional disabling in production release)? -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
May 16, 2013 [Issue 10093] wrong unsigned arithmetic | ||||
---|---|---|---|---|
| ||||
Posted in reply to luka8088 | http://d.puremagic.com/issues/show_bug.cgi?id=10093 --- Comment #9 from 9999 <sibaqexozequgaba@tempomail.fr> 2013-05-16 12:57:40 PDT --- (In reply to comment #8) > The original issue was: > > auto offset = text1.length - text2.length; > func(offset); > > and offset turned out to be around 4294967291 > > I was thinking, setting a uint to a negative value is kind of an overflow, should it maybe be treated the same way like array bounds and be checked by druntime (with optional disabling in production release)? Well, that's probably something the compiler can't warn about. Not statically, that's for sure. You can use a custom type which checks for bound overflows, and fallback to regular int for release builds. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: ------- |
Copyright © 1999-2021 by the D Language Foundation