www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 5409] New: Disallow (!x & y)

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5409

           Summary: Disallow (!x & y)
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc


--- Comment #0 from bearophile_hugs eml.cc 2011-01-04 04:52:27 PST ---
Studying bug reports in Linux kernel I've seen many cases like:
!x & y

That were meant to be:
!(x & y)

So I suggest to turn an expression like the first one (!x & y) into a D2 syntax
error.

So the D2 compiler asks the programmer for explicit parentheses like (the two
following cases are both accepted, the error message may show both examples):
!(x & y)
Or even:
(!x) & y

The following case is not covered, this enhancement request is about the
bitwise "&" case only:
!x && y

--------------------------

The Coccinelle tool is able to catch bugs like that with this little semantic
patch:

// Copyright: (C) 2009 Gilles Muller, Julia Lawall, INRIA, DIKU.  GPLv2.
   expression E1,E2;   
(
  !E1 & !E2
|
- !E1 & E2
+ !(E1 & E2)
)

For some of the (!x & y) Linux bugs caught by Coccinelle see:
http://coccinelle.lip6.fr/impact_linux.php
searching for "Correct occurrences of !x&y".

An example:
http://git.kernel.org/?p=linux/kernel/git/torvalds/linux-2.6.git;a=commit;h=b2d7c7f7a69fd953626c3e507bac70e18b21f70e

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 04 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5409



--- Comment #1 from bearophile_hugs eml.cc 2011-05-06 10:14:02 PDT ---
Don seems to agree in catching this bug statically:

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

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

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 06 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5409



--- Comment #2 from bearophile_hugs eml.cc 2011-07-09 11:08:24 PDT ---
*** Issue 5814 has been marked as a duplicate of this issue. ***

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 09 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5409



--- Comment #3 from bearophile_hugs eml.cc 2011-10-19 15:35:52 PDT ---
One more case found here, in the source code of the Chrome browser:
http://www.viva64.com/en/b/0113/

Fragment N3:

#define SEC_ASN1_CHOICE        0x100000

typedef struct sec_ASN1Template_struct {
  unsigned long kind; 
  ...
} SEC_ASN1Template;

PRBool SEC_ASN1IsTemplateSimple(
  const SEC_ASN1Template *theTemplate)
{
  ...
  if (!theTemplate->kind & SEC_ASN1_CHOICE) {
  ...
}



A related case (Fragment N4), that I think too is worth catching (probably it's
a less common bug):


bool GetPlatformFileInfo(...) {
  ...
  info->is_directory =
    file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0;
  ...
}


The probably right code for Fragment N4:

info->is_directory =
  (file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 19 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5409


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |timon.gehr gmx.ch


--- Comment #4 from timon.gehr gmx.ch 2011-10-23 01:19:22 PDT ---
(In reply to comment #3)
 A related case (Fragment N4), that I think too is worth catching (probably it's
 a less common bug):
 
 
 bool GetPlatformFileInfo(...) {
   ...
   info->is_directory =
     file_info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0;
   ...
 }
 
That is already caught with the current compiler. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Oct 23 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5409



--- Comment #5 from bearophile_hugs eml.cc 2011-11-13 09:57:46 PST ---
I think one of those bugs are present in DMD sources too:
http://article.gmane.org/gmane.comp.lang.d.dmd.devel/2648

dmd\src\backend\cgreg.c(51):

 void cgreg_init()
 {
     if (!config.flags4 & CFG4optimized)
         return;
.\backend\cgreg.c(53) : warning C4806: '&' : unsafe operation: no value of type 'bool' promoted to type 'int' can equal the given constant -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Nov 13 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5409



--- Comment #6 from bearophile_hugs eml.cc 2011-11-25 15:55:36 PST ---
One more example found in the Doom3 sources:
http://www.viva64.com/en/b/0120/


#define BIT( num ) ( 1 << ( num ) )
const int BUTTON_ATTACK = BIT(0);
void idTarget_WaitForButton::Think( void ) {
  ...
  if ( player &&
      ( !player->oldButtons & BUTTON_ATTACK ) &&
      ( player->usercmd.buttons & BUTTON_ATTACK ) ) {
  ...
}


I hope people like Kenji Hara will create a patch to fix this situation in D
language.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Nov 25 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5409



--- Comment #7 from bearophile_hugs eml.cc 2012-04-23 18:29:55 PDT ---
One more example found in the Blender code:
http://www.gamasutra.com/blogs/AndreyKarpov/20120423/169021/Analyzing_the_Blender_project_with_PVSStudio.php

#define  DEFAULT_STREAM  \
  m[dC] = RAC(ccel,dC); \
  \
  if((!nbored & CFBnd)) { \
  \
  ....

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Apr 23 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=5409



--- Comment #8 from bearophile_hugs eml.cc 2013-06-26 10:57:01 PDT ---
The Visual Studio 2012 warning:

http://msdn.microsoft.com/en-us/library/z01etkwy.aspx

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