[haiku-bugs] [Haiku] #6373: Bluetooth address macros are broken

  • From: "zooey" <trac@xxxxxxxxxxxx>
  • Date: Fri, 23 Jul 2010 19:49:25 -0000

#6373: Bluetooth address macros are broken
--------------------------------------------+-------------------------------
   Reporter:  zooey                         |        Owner:  oruizdorantes
       Type:  bug                           |       Status:  new          
   Priority:  normal                        |    Milestone:  R1           
  Component:  Network & Internet/Bluetooth  |      Version:  R1/alpha2    
   Keywords:                                |   Blocked By:               
Has a Patch:  0                             |     Platform:  All          
   Blocking:                                |  
--------------------------------------------+-------------------------------
 The bluetooth.h header defines these bdaddr_t macros:

 {{{
 #define BDADDR_NULL             (&(bdaddr_t) {{0, 0, 0, 0, 0, 0}})
 #define BDADDR_LOCAL            (&(bdaddr_t) {{0, 0, 0, 0xff, 0xff,
 0xff}})
 #define BDADDR_BROADCAST        (&(bdaddr_t) {{0xff, 0xff, 0xff, 0xff,
 0xff, 0xff}})
 }}}

 However, the concept of getting an address of a compound literal is pretty
 much broken, as compound literals are neither part of the C++ standard
 (though gcc supports them) and their lifetime is not well-controlled
 inside of a macro.

 As an example:

 {{{

 struct foo {
     bdaddr_t peer;
 };

 void bar()
 {
     foo myfoo;
     if (...)
         myfoo.peer = BDADDR_LOCAL;
     else
         myfoo.peer = BDADDR_NULL;

     // try doing something with myfoo, but the actuall bdaddr_t object
 will
     // be dead here already, so the data at the address myfoo.peer may
 have
     // been overwritten
 }

 }}}

 Instead, I suggest doing something along the lines of:

 {{{
 extern const bdaddr_t __BDADDR_NULL;
 extern const bdaddr_t __BDADDR_LOCAL;
 extern const bdaddr_t __BDADDR_BROADCAST;

 #define BDADDR_NULL             (&__BDADDR_NULL)
 #define BDADDR_LOCAL            (&__BDADDR_LOCAL)
 #define BDADDR_BROADCAST        (&__BDADDR_BROADCAST)
 }}}

 and then define these constants somewhere in an implementation file that
 gets linked by all users of those macros.

-- 
Ticket URL: <http://dev.haiku-os.org/ticket/6373>
Haiku <http://dev.haiku-os.org>
Haiku - the operating system.

Other related posts: