Bug #28184 min() and max() macro defines in my_global.h conflict with c++ headers
Submitted: 1 May 2007 18:26 Modified: 27 Nov 2007 19:11
Reporter: Arkadiusz Miskiewicz (Basic Quality Contributor) Email Updates:
Status: Closed Impact on me:
Category:MySQL Server: Compiling Severity:S3 (Non-critical)
Version:5.1.17, 5.0.37,6.0 OS:Any
Assigned to: CPU Architecture:Any
Tags: Contribution, qc

[1 May 2007 18:26] Arkadiusz Miskiewicz
my_global.h contains:
#if !defined(max)
#define max(a, b)       ((a) > (b) ? (a) : (b))
#define min(a, b)       ((a) < (b) ? (a) : (b))

which are highly problematic when building with upcoming gcc 4.2.0 because:

x86_64-pld-linux-g++ -DUNDEF_THREADS_HACK -DDEFAULT_MYSQL_HOME="\"/usr\"" -DDATADIR="\"/var/lib/mysql\"" -I. -I.. -I../include -I../include -I../regex     -O2 -fno-strict-aliasing -fwrapv -march=x86-64 -gdwarf-2 -g2  -felide-constructors -fno-rtti -fno-exceptions -fomit-frame-pointer   -fno-implicit-templates -fno-exceptions -fno-rtti  -MT mysqladmin.o -MD -MP -MF .deps/mysqladmin.Tpo -c -o mysqladmin.o mysqladmin.cc
sql_string.cc: In member function 'int String::reserve(uint32, uint32)':
sql_string.cc:665: error: 'max' was not declared in this scope
sql_string.cc: In function 'int stringcmp(const String*, const String*)':
sql_string.cc:751: error: 'min' was not declared in this scope
sql_string.cc: In function 'String* copy_if_not_alloced(String*, String*, uint32)':
sql_string.cc:768: error: 'min' was not declared in this scope

and that's because /usr/include/c++/4.2.0/limits contains:
  template<typename _Tp>
    struct numeric_limits : public __numeric_limits_base
      /** The minimum finite value, or for floating types with
          denormalization, the minimum positive normalized value.  */
      static _Tp min() throw() { return static_cast<_Tp>(0); }
      /** The maximum finite value.  */
      static _Tp max() throw() { return static_cast<_Tp>(0); }

How to repeat:
build mysql 5.1.17 with gcc 4.2.0rc1

Suggested fix:
Change names of macros from min to mysql_min and max to mysql_max.

Workaround patch:
but clean solution is to rename these everywhere in source code.
[1 May 2007 19:00] Valeriy Kravchuk
Thank you for a problem report. Verified just as described with code review.
[10 Jul 2007 7:32] Arkadiusz Miskiewicz
Replaces min() max() with mysql_min() and mysql_max()

Attachment: mysql-min_max.patch (application/octet-stream, text), 142.99 KiB.

[10 Jul 2007 7:33] Arkadiusz Miskiewicz
Compile tested. Replaces all (hope that I didn't miss anything in code that's not compiled in by default) occurences of min() and max() with non-conflicting mysql_min() and mysql_max().

Please review and merge since the bug is annoying for anyone using recent compiler (note, gcc 4.2.0 was already released some time ago).
[10 Jul 2007 21:42] Sergei Golubchik
see also http://lists.mysql.com/internals/34680 and references therein
[11 Jul 2007 13:09] Arkadiusz Miskiewicz
Uppercasing to indicate that these are macros is a good idea. So after applying my patch:

find . -type f -exec sed -i -e 's|mysql_min|MYSQL_MIN|g' -e 's|mysql_max|MYSQL_MAX|g' '{}' ';'
[23 Jul 2007 13:04] Paul Dubois
This occurs now on Gentoo Linux, for which a 4.2.0 ebuild has been released recently.

% gcc --version
gcc (GCC) 4.2.0 (Gentoo 4.2.0 p1.4)

Affects the 5.1, 5.1-falcon, and 5.2 bk trees.
[12 Nov 2007 17:28] Hartmut Holzgraefe
Also affects Debian "testing" and "unstable" as both have gcc 4.2.x as default compiler now (probably also some Ubuntu versions?)
[12 Nov 2007 17:44] Arkadiusz Miskiewicz
We use smaller version mentioned earlier in commeents:
[23 Nov 2007 13:30] Hartmut Holzgraefe
Fixed by

[26 Nov 2007 18:28] Paul Dubois
Noted in 5.1.23, 6.0.4 changelogs.

MySQL now can be compiled with gcc 4.2.x. There was a problem 
involving a conflict with the min() and max() macros in my_global.h,
which now have been renamed to mysql_min() and mysql_max().
[26 Nov 2007 19:21] Sergei Golubchik
No, they weren't renamed, see the patch at the link
[27 Nov 2007 19:11] Paul Dubois
Removed the "which have been renamed" part from the changelog entry.
[1 May 2013 17:06] Jon Baker
Is this still fixed?  I'm running into this issue using mysql-devel-5.5-1.3