public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/39962]  New: Subtraction bug with a constant without braces
@ 2009-04-29 14:54 jvme38 at gmail dot com
  2009-04-29 15:00 ` [Bug c/39962] " pinskia at gcc dot gnu dot org
  0 siblings, 1 reply; 2+ messages in thread
From: jvme38 at gmail dot com @ 2009-04-29 14:54 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 3012 bytes --]

Dear,

There may be a substraction or other arithmetic operation bug in the gcc
compiler when one of the operands is a constant which is defined with another
constant and a value.

I've discovered the bug in gcc version v4.1.2-44 which is the default gcc
compiler for the RedHat Enterprise Linux v5.2 distribution.
I'v also been enable to reproduce the bug with the latest gcc version v4.4.0 .

My development platform is an i386 Linux system in 32-bit mode with kernel
version 2.6.18 .

See the following test program for the explanation/invocation of the gcc bug:

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

/*
** Test program to reproduce the subtraction bug with a constant without braces
**
** Author: Johan Vermeire, Diebold corporation, Belgium
** 
** Date: April, the 29th of 2009
*/

#include <stdlib.h>

/* MOSEL HEADER OFFSETS */

#define Q_dest         0x00

#define BUGGY
#ifdef BUGGY  // Invokes the bug in gcc

#define Q_source       Q_dest     + 1
#define Q_len          Q_source   + 1
#define Q_T_tpdu       Q_len      + 2
#define Q_T_dest       Q_T_tpdu   + 1
#define Q_T_source     Q_T_dest   + 8
#define Q_T_mes        Q_T_source + 8

#else // Doesn't invoke the bug in gcc

#define Q_source       (Q_dest     + 1)
#define Q_len          (Q_source   + 1)
#define Q_T_tpdu       (Q_len      + 2)
#define Q_T_dest       (Q_T_tpdu   + 1)
#define Q_T_source     (Q_T_dest   + 8)
#define Q_T_mes        (Q_T_source + 8)

#endif

#define iMOSEL_HEADER_SIZE  Q_T_mes


int tppnauto_PPC_prepare_PPC2() {

int iOffset = 460; 

return iOffset;

}

int main() {

int iPrepMsgSize = tppnauto_PPC_prepare_PPC2();
int iMacTextLength;

iMacTextLength = iPrepMsgSize;
int size = iMOSEL_HEADER_SIZE;  // size = iMOSEL_HEADER_SIZE = 21

iMacTextLength = iPrepMsgSize - iMOSEL_HEADER_SIZE;
// iMacTextLength = 481, problem

iMacTextLength = iPrepMsgSize - size;
// iMacTextLength = 439, OK

exit(0);

}

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

Met Vriendelijke Groet, Bien à vous, Best Regards,

Johan Vermeire
Software engineer
Fixed line: +32 (0)2 464 32 32
Mobile : +32 (0)473 533 186
Picture (Metafile)
Url: http://www.diebold.com
Diebold BeLux
Professional Services
Brusselsesteenweg, 498 Chaussée de Bruxelles
B-1731 Zellik
Belgium


-- 
           Summary: Subtraction bug with a constant without braces
           Product: gcc
           Version: 4.4.0
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: jvme38 at gmail dot com
 GCC build triplet: Linux rhel4-1 2.6.18-128.1.6.el5 #1 SMP Tue Mar 24
                    12:10:27 EDT
  GCC host triplet: Linux rhel4-1 2.6.18-128.1.6.el5 #1 SMP Tue Mar 24
                    12:10:27 EDT
GCC target triplet: Linux rhel4-1 2.6.18-128.1.6.el5 #1 SMP Tue Mar 24
                    12:10:27 EDT


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39962


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [Bug c/39962] Subtraction bug with a constant without braces
  2009-04-29 14:54 [Bug c/39962] New: Subtraction bug with a constant without braces jvme38 at gmail dot com
@ 2009-04-29 15:00 ` pinskia at gcc dot gnu dot org
  0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2009-04-29 15:00 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2009-04-29 14:59 -------
This is not a bug, this is how the C preprocessor works.
#define Q_dest         0x00

#define Q_source       Q_dest     + 1
#define Q_len          Q_source   + 1
#define Q_T_tpdu       Q_len      + 2
#define Q_T_dest       Q_T_tpdu   + 1
#define Q_T_source     Q_T_dest   + 8
#define Q_T_mes        Q_T_source + 8


#define iMOSEL_HEADER_SIZE  Q_T_mes

iMacTextLength = iPrepMsgSize - iMOSEL_HEADER_SIZE;


That is turned into:
iMacTextLength = iPrepMsgSize - 0x00     + 1   + 1      + 2   + 1   + 8 + 8;

Macro replacement is a direct token replacement so if you forget the penthsies
it will not work the way you want it to work.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=39962


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-04-29 15:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-29 14:54 [Bug c/39962] New: Subtraction bug with a constant without braces jvme38 at gmail dot com
2009-04-29 15:00 ` [Bug c/39962] " pinskia at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).