[comp.windows.x] Bug in AssignMin and AssignMax in XawMisc.h

kit@EXPO.LCS.MIT.EDU (Chris D. Peterson) (03/01/89)

This is obscure and annoying enough that I thought all you xperts out there
might be interested.

						Chris D. Peterson

Disclaimer:  This is not an "official" patch, I just thought it would
	     be of interest to the community.

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

			  X Window System Bug Report

VERSION:
    R3

CLIENT MACHINE and OPERATING SYSTEM:
    all

DISPLAY:
    all

WINDOW MANAGER:
    all

AREA:
    Xaw

SYNOPSIS:
    Macro definitions for AssignMin and AssignMax can cause problems.

DESCRIPTION:
    The following lines of code will cause unexpected behavior.

	if (fubar)
		AssignMin(foo, bar)
	else
		i++;

	When the macros get substituted in via cpp, the code looks like:

	if (fubar)
		if ((bar) < (foo)) 
			foo = (bar)
		else
			i++;

	This is NOT the expected bevaior, but the compiler is happy to
	interpret this since it is valid C code.
	
REPEAT BY:
	Try using code with the above syntax in it.

SAMPLE FIX:

	I added an extra set of curly braces, this does not allow the above
	statement to work, but the compiler at least complains.

*** /tmp/,RCSt1a02004	Tue Feb 28 13:26:45 1989
--- lib/Xaw/XawMisc.h	Tue Feb 28 13:17:02 1989
***************
*** 1,5 ****
  /*
! * $XConsortium: XawMisc.h,v 1.8 88/09/06 16:42:52 jim Exp $
  */
  
  /***********************************************************
--- 1,5 ----
  /*
! * $XConsortium: XawMisc.h,v 1.9 89/02/28 13:16:26 kit Exp $
  */
  
  /***********************************************************
***************
*** 35,41 ****
  
  #define Max(x, y)	(((x) > (y)) ? (x) : (y))
  #define Min(x, y)	(((x) < (y)) ? (x) : (y))
! #define AssignMax(x, y)	if ((y) > (x)) x = (y)
! #define AssignMin(x, y)	if ((y) < (x)) x = (y)
  
  #endif /*_XawMisc_h*/
--- 35,41 ----
  
  #define Max(x, y)	(((x) > (y)) ? (x) : (y))
  #define Min(x, y)	(((x) < (y)) ? (x) : (y))
! #define AssignMax(x, y)	{if ((y) > (x)) x = (y);}
! #define AssignMin(x, y)	{if ((y) < (x)) x = (y);}
  
  #endif /*_XawMisc_h*/