Ethereal-dev: [ethereal-dev] macro expansion protection

Note: This archive is from the project's previous web site, ethereal.com. This list is no longer active.

From: Jun-ichiro itojun Hagino <itojun@xxxxxxxxxx>
Date: Tue, 22 Aug 2000 16:51:43 +0900
	packet.h does not have protection against macro argument evaluation.
	for example, if we have some code like:
		guint32_t *p;
		printf("%x", pntohl(p + 1));
	it will get evaluated incorrectly.  i believe we need more parens
	in macros, for example like this.  opinions?

itojun


Index: packet.h
===================================================================
RCS file: /usr/local/cvsroot/ethereal/packet.h,v
retrieving revision 1.197
diff -c -r1.197 packet.h
*** packet.h	2000/08/21 15:45:21	1.197
--- packet.h	2000/08/22 07:40:53
***************
*** 41,87 ****
   */
  
  #define pntohs(p)   ((guint16)                       \
!                      ((guint16)*((guint8 *)p+0)<<8|  \
!                       (guint16)*((guint8 *)p+1)<<0))
  
! #define pntohl(p)   ((guint32)*((guint8 *)p+0)<<24|  \
!                      (guint32)*((guint8 *)p+1)<<16|  \
!                      (guint32)*((guint8 *)p+2)<<8|   \
!                      (guint32)*((guint8 *)p+3)<<0)
  
  #ifdef G_HAVE_GINT64
! #define pntohll(p)  ((guint64)*((guint8 *)p+0)<<56|  \
!                      (guint64)*((guint8 *)p+1)<<48|  \
!                      (guint64)*((guint8 *)p+2)<<40|  \
!                      (guint64)*((guint8 *)p+3)<<32|  \
!                      (guint64)*((guint8 *)p+4)<<24|  \
!                      (guint64)*((guint8 *)p+5)<<16|  \
!                      (guint64)*((guint8 *)p+6)<<8|   \
!                      (guint64)*((guint8 *)p+7)<<0)
  #endif
  
  #define pletohs(p)  ((guint16)                       \
!                      ((guint16)*((guint8 *)p+1)<<8|  \
!                       (guint16)*((guint8 *)p+0)<<0))
  
! #define pletohl(p)  ((guint32)*((guint8 *)p+3)<<24|  \
!                      (guint32)*((guint8 *)p+2)<<16|  \
!                      (guint32)*((guint8 *)p+1)<<8|   \
!                      (guint32)*((guint8 *)p+0)<<0)
  
  #ifdef G_HAVE_GINT64
! #define pletohll(p) ((guint64)*((guint8 *)p+7)<<56|  \
!                      (guint64)*((guint8 *)p+6)<<48|  \
!                      (guint64)*((guint8 *)p+5)<<40|  \
!                      (guint64)*((guint8 *)p+4)<<32|  \
!                      (guint64)*((guint8 *)p+3)<<24|  \
!                      (guint64)*((guint8 *)p+2)<<16|  \
!                      (guint64)*((guint8 *)p+1)<<8|   \
!                      (guint64)*((guint8 *)p+0)<<0)
  #endif
  
! #define hi_nibble(b) ((b & 0xf0) >> 4)
! #define lo_nibble(b) (b & 0x0f)
  
  /* Useful when you have an array whose size you can tell at compile-time */
  #define array_length(x)	(sizeof x / sizeof x[0])
--- 41,87 ----
   */
  
  #define pntohs(p)   ((guint16)                       \
!                      ((guint16)*((guint8 *)(p)+0)<<8|  \
!                       (guint16)*((guint8 *)(p)+1)<<0))
  
! #define pntohl(p)   ((guint32)*((guint8 *)(p)+0)<<24|  \
!                      (guint32)*((guint8 *)(p)+1)<<16|  \
!                      (guint32)*((guint8 *)(p)+2)<<8|   \
!                      (guint32)*((guint8 *)(p)+3)<<0)
  
  #ifdef G_HAVE_GINT64
! #define pntohll(p)  ((guint64)*((guint8 *)(p)+0)<<56|  \
!                      (guint64)*((guint8 *)(p)+1)<<48|  \
!                      (guint64)*((guint8 *)(p)+2)<<40|  \
!                      (guint64)*((guint8 *)(p)+3)<<32|  \
!                      (guint64)*((guint8 *)(p)+4)<<24|  \
!                      (guint64)*((guint8 *)(p)+5)<<16|  \
!                      (guint64)*((guint8 *)(p)+6)<<8|   \
!                      (guint64)*((guint8 *)(p)+7)<<0)
  #endif
  
  #define pletohs(p)  ((guint16)                       \
!                      ((guint16)*((guint8 *)(p)+1)<<8|  \
!                       (guint16)*((guint8 *)(p)+0)<<0))
  
! #define pletohl(p)  ((guint32)*((guint8 *)(p)+3)<<24|  \
!                      (guint32)*((guint8 *)(p)+2)<<16|  \
!                      (guint32)*((guint8 *)(p)+1)<<8|   \
!                      (guint32)*((guint8 *)(p)+0)<<0)
  
  #ifdef G_HAVE_GINT64
! #define pletohll(p) ((guint64)*((guint8 *)(p)+7)<<56|  \
!                      (guint64)*((guint8 *)(p)+6)<<48|  \
!                      (guint64)*((guint8 *)(p)+5)<<40|  \
!                      (guint64)*((guint8 *)(p)+4)<<32|  \
!                      (guint64)*((guint8 *)(p)+3)<<24|  \
!                      (guint64)*((guint8 *)(p)+2)<<16|  \
!                      (guint64)*((guint8 *)(p)+1)<<8|   \
!                      (guint64)*((guint8 *)(p)+0)<<0)
  #endif
  
! #define hi_nibble(b) (((b) & 0xf0) >> 4)
! #define lo_nibble(b) ((b) & 0x0f)
  
  /* Useful when you have an array whose size you can tell at compile-time */
  #define array_length(x)	(sizeof x / sizeof x[0])