Synopsis: Adding magic numbers to structures for a safer act(). December 96 From: erwin@pip.dknet.dk To: MERC/Envy Mailing List Subject: Adding magic numbers to CHAR_DATA/OBJ_DATA You might have already run into this problem with act: the 2 void* parameters make it impossible to do proper checking on the passed parameters. If you pass act() an object as the second void parameter (which would be used as $P in the string) but use $N mistakenly, act() will cast the object to a char_data - resulting in most likely a crash, when it tries to call can_see() with it as parameter. It is not too hard to avoid this when programming. However, if you are using MobProgs and act triggers, a mob prog which uses $o (equivalent of $P in act()) but triggers via an action that uses $N will crash the MUD. In his improved version of MobProgs, Markuu 'Newt' Nyylander suggests to extend act so that it takes typed parameters - however, it is somewhat cumbersome to rewrite the many act() calls so they adhere to a new standard. I've implemented another solution, which is somewhat easier and may have other applications. Simply add a common magic header to both the CHAR_DATA and OBJ_DATA structure: struct char data { char magic; /* Magic header */ ... }; This magic header must be filled by create_object, create_mobile and load_char_obj with a value that is different for char_data and obj_data. In act() as well as expand_arg() and cmd_eval() in mob_prog.c you will need to add checks that what supposedly points to a char_data has the magic number is its first byte, e.g.: case 'p': if (!obj1 || obj1->magic != MAGIC_OBJ) { /* Report a bad act string */ Note that while as char magic; is only one byte, becuase of alignment you'll waste 4 bytes on it. I've chosen not to make it an int because of strings: an act() could be passed a one-character string - reading 4 bytes from that location might cause a segmentation fault. That you risk getting a string passed as parameter also means that you should choose magic numbers that are unlikely to show up in a string. ============================================================================== Erwin Andreasen Viby J, Denmark Computer Science Student at Aarhus Business erwin@pip.dknet.dk College ==============================================================================