[Issue 4077] New: Bugs caused by bitwise operator precedence

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Apr 10 16:06:35 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4077

           Summary: Bugs caused by bitwise operator precedence
           Product: D
           Version: future
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-04-10 16:06:32 PDT ---
This isn't a bug report, and it's not exactly an enhancement request yet. It's
a report that a problem exists, but I don't know a solution yet. I think it's
useful to have this in Bugzilla, to keep in mind that this problem exists in D.

This report is born from a bug done by Adam D. Ruppe, but similar bugs have
happened in my code too in the past:

http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=108772
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=108781
http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D&article_id=108783

The precedence of bitwise operators is low, this makes them error-prone, it's a
part of C/C++/D that causes frequent bugs in programs (the solution is to 
extra parentheses when you use bitwise operators). At the moment I don't see a
simple way to remove this source of bugs from the D2 language.

This class of bugs is so common that GCC developers have felt the need to
improve the situation. When you switch on the warnings GCC warns you about few
possible similar errors, suggesting to add parentheses to remove some
ambiguity. A small example in C:

#include "stdio.h"
#include "stdlib.h"
int main() {
    int a = atoi("10");
    int b = atoi("20");
    int c = atoi("30");
    printf("%u\n", a|b <= c);
    return 0;
}

If you compile it with GCC 4.4.1:

gcc -Wall test.c -o test
test.c: In function 'main':
test.c:9: warning: suggest parentheses around comparison in operand of '|'

You always use -Wall (and other warnings) when you write C code, so here gcc is
able to catch such bugs.

This class of warnings can be added to the D compiler too.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list