Bug #26461 Intrinsic data type bool (1 byte) redefined to BOOL (4 bytes)
Submitted: 17 Feb 2007 17:14 Modified: 1 Apr 2008 19:20
Reporter: Von Chance Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S2 (Serious)
Version:5.0.27/4.1/5.1/5.0BK OS:Microsoft Windows (Windows (Any))
Assigned to: Georgi Kodinov CPU Architecture:Any
Tags: BOOL, corruption, heap, windows
Triage: D1 (Critical) / R2 (Low) / E2 (Low)

[17 Feb 2007 17:14] Von Chance
Description:
In the file "config-win.h" there is a line that unconditionally defines the data type "bool" to "BOOL".
...
#define bool BOOL
...

In Visual Studio 2005, the intrinsic type bool is 1 byte and windef.h defines BOOL as an int or 4 bytes.  In a complex application, one would include "my_global.h" in only the modules that required it.  Those modules would define bool as 4 bytes.  In other modules that do not include "my_global.h", bool would be 1 byte in size.  This results in heap and/or stack corruption.

A simple fix would be to change the offending line to:
...
#if !defined(__cplusplus)
#define bool BOOL
#endif
...

I am not sure of the impact of this fix using the pre-built windows binaries if they already use bool as a 4 byte value.

How to repeat:
I encountered this issue in a complex app.  If this issue is not obvious, I will provide a scaled down test scenario.

Suggested fix:
In config-win.h change
...
#define bool BOOL
...

To:

...
#if !defined(__cplusplus)
#define bool BOOL
#endif
...
[18 Feb 2007 17:09] Miguel Solorzano
Thank you for the bug report.

c:\temp>cl testbool.cpp
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.42 for 80x86
Copyright (C) Microsoft Corporation.  All rights reserved.

testbool.cpp
Microsoft (R) Incremental Linker Version 8.00.50727.42
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:testbool.exe
testbool.obj

c:\temp>testbool
bool: 1
BOOL: 4

c:\temp>cat c:\build\mysql-5.0.34\include\config-win.h | findstr bool
#define bool BOOL
#define bool_defined

c:\temp>cat testbool.cpp
#include <windows.h>
#include <stdio.h>
void main()
{
  printf("bool: %d\n",sizeof(bool));
  printf("BOOL: %d\n",sizeof(BOOL));
}
[31 May 2007 9:10] Dirk Maij
Hi,

I may have a simpler test case since I came across the same problem. The propsed fix works for my by the way.

How does this works: The precomiler defined bool to BOOL causing incompatible types when compiling. The generated errors on VC7.1 (vs 2003.net) should be c2556 and c2371

The fix:
booltest.h
------------------------------>8
class booltest
{
   booltest() {};
   ~booltest() {};

   bool showProblem( bool );
};
------------------------------->8

booltest.cpp
------------------------------->8
#include "booltest.h"

#include <my_global.h>
#include <mysql.h>

bool booltest::showProblem()
{
  return true;
}
------------------------------->8

Kind regards,

Dirk Maij
[24 Oct 2007 20:00] Miguel Solorzano
Bug: http://bugs.mysql.com/bug.php?id=31822 has been marked as duplicate
of this one.
[27 Oct 2007 1:13] Miguel Solorzano
Notes from related bug report http://bugs.mysql.com/bug.php?id=31822:

 [27 Oct 1:57] Bill Mitchell

Yes, Miguel, I did recognize that bug #26461 is one of several that discusses issues
surrounding the definition of bool in config-win.h.  So I see why you might regard this
bug #31822 as a duplicate.  But I think I can draw a distinction.  Bug #26461 discusses
problems that arise when a source file includes one but not both of the header files
config-win.h and my_global.h.  In principal, one could resolve that bug report by ensuring
that the two header files give equivalent redefinitions, either 1 byte or 4 byte for
consistency.  Bug #31822 discusses a different issue, the complete inability to use
publicly available C++ libraries from a C++ plugin.  And the only resolution on the mysql
side would be the elimination of redefinitions of bool in both header files, leaving the
native C++ bool intact.  

Looking at the two reports from a practical level, a source resolution to #31822 must
necessarily fix #26461 as well.  But there exist source changes that resolve #26461, but
leave #31822 as an outstanding issue.

[27 Oct 2:01] Bill Mitchell

To determine the size of the issue on the mysql side, and to verify that the problem can
indeed be resolved, I have developed a set of source changes that attempt to make
consistent use for each set of related variables/methods of either bool or my_bool.  Where
consistency is too hard to achieve, most of the compilation warnings are remedied through
appropriate use of the test() method.  Beyond the constraints I sketched in the initial
bug report, the most significant source constraint appears where some functions expect the
address of an item, e.g., a char* that points to a my_bool.  Some of these functions are
in C modules, and not C++ modules, where the native C++ bool should not be passed.  

I have uploaded a .zip file, bug-fix-31822.zip, with the set of changes I have made so
far.  These yield an error-free compilation with a manageable set of warning messages.  A
few of these warnings relate to real source errors that should be looked at.  The largest
clump of remaining warnings are in the yacc parser, the has numerous conversions from
int/my_bool to bool.  Not yet being really familiar with yacc, I thought I would leave
these for someone else to consider, although they could all be made to go away very
quickly with a few more test() calls.  

For each of the modified files, I have included a .rev file that describes the changes in
each file.  For header and source files with the same name, I shared all the revision
comments in one file with the same base name.  

Of course, not yet being as expert as I might like on mysql, the implications for
not-Windows platforms, and the implications for the data across the client interfaces,
there is a chance that these source changes affect a data type that should be kept fixed
across multiple versions of the product.  I hope that is not the case.  Also, I have not
yet built a Linux version where I could run the provided test suite.  So all I know is
that it compiles relatively cleanly and seems to work on very preliminary simple tests.
[21 Dec 2007 8:53] Dirk Maij
This is the second longest discussion I have seen about 1 bit... The longest actually dealt about 3 bits and was in a mobile environment with no memory space due to cost reduction (32K total including OS and multimedia apps)

Good luck with taking the right decision and solving this bug!

Cheers,
- Dirk
[14 Mar 2008 16:55] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/44013

ChangeSet@1.2598, 2008-03-14 18:54:48+02:00, gkodinov@macbook.gmz +12 -0
  Bug #26461: Intrinsic data type bool (1 byte) redefined to BOOL (4 bytes)
  
  The bool data type was redefined to BOOL (4 bytes on windows).
  Removed the #define and fixed some of the warnings that were uncovered
  by this.
  Note that the fix also disables 2 warnings :
  4800 : 'type' : forcing value to bool 'true' or 'false' (performance warning)
  4805: 'operation' : unsafe mix of type 'type' and type 'type' in operation
  
  These warnings will be handled in a separate bug, as they are performance related or bogus.
[19 Mar 2008 19:17] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/44255

ChangeSet@1.2598, 2008-03-19 21:13:59+02:00, gkodinov@macbook.gmz +16 -0
  Bug #26461: Intrinsic data type bool (1 byte) redefined to BOOL (4 bytes)
  
  The bool data type was redefined to BOOL (4 bytes on windows).
  Removed the #define and fixed some of the warnings that were uncovered
  by this.
  Note that the fix also disables 2 warnings :
  4800 : 'type' : forcing value to bool 'true' or 'false' (performance warning)
  4805: 'operation' : unsafe mix of type 'type' and type 'type' in operation
  
  These warnings will be handled in a separate bug, as they are performance related or bogus.
[23 Mar 2008 12:30] Bugs System
A patch for this bug has been committed. After review, it may
be pushed to the relevant source trees for release in the next
version. You can access the patch from:

  http://lists.mysql.com/commits/44351

ChangeSet@1.2598, 2008-03-21 17:23:17+02:00, gkodinov@macbook.gmz +17 -0
  Bug #26461: Intrinsic data type bool (1 byte) redefined to BOOL (4 bytes)
  
  The bool data type was redefined to BOOL (4 bytes on windows).
  Removed the #define and fixed some of the warnings that were uncovered
  by this.
  Note that the fix also disables 2 warnings :
  4800 : 'type' : forcing value to bool 'true' or 'false' (performance warning)
  4805: 'operation' : unsafe mix of type 'type' and type 'type' in operation
  
  These warnings will be handled in a separate bug, as they are performance related or bogus.
  
  Fixed to int the return type of functions that return more than 
  2 distinct values.
[28 Mar 2008 9:21] Bugs System
Pushed into 5.1.24-rc
[28 Mar 2008 9:23] Bugs System
Pushed into 5.0.60
[31 Mar 2008 13:58] Bugs System
Pushed into 6.0.5-alpha
[1 Apr 2008 19:20] Paul Dubois
Noted in 5.0.60, 5.1.24, 6.0.5 changelogs.

config-win.h unconditionally defined bool as BOOL, causing problems 
on systems where bool is 1 byte and BOOL is 4 bytes.