Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sun-barr!lll-winken!sol.ctr.columbia.edu!spool.mu.edu!samsung!think.com!linus!agate!ucbvax!BRL.MIL!mike From: mike@BRL.MIL (Mike Muuss) Newsgroups: comp.sys.sgi Subject: Re: Pointer validation code Message-ID: <9102041809.aa13187@WOLF.BRL.MIL> Date: 4 Feb 91 23:09:21 GMT Sender: daemon@ucbvax.BERKELEY.EDU Organization: The Internet Lines: 70 Dan - Glad you found the rt_malloc() code interesting. Let me also share some of the "magic number checking" stuff, as well. Best, -Mike ------- /* * Macros to check and validate a structure pointer, given that * the first entry in the structure is a magic number. */ #define RT_CKMAG(_ptr, _magic, _str) \ if( !(_ptr) ) { \ rt_log("ERROR: null %s ptr, file %s, line %d\n", \ _str, __FILE__, __LINE__ ); \ rt_bomb("NULL pointer"); \ } else if( *((long *)(_ptr)) != (_magic) ) { \ rt_log("ERROR: bad %s ptr x%x, s/b x%x, was %s(x%x), file %s, line %d\n", \ _str, _ptr, _magic, \ rt_identify_magic( *((long *)(_ptr)) ), \ *((long *)(_ptr)), __FILE__, __LINE__ ); \ rt_bomb("Bad pointer"); \ } Here is an example use of this: /* * R T _ E X T E R N A L * * An "opaque" handle for holding onto objects, * typically in some kind of external form that is not directly * usable without passing through an "importation" function. */ struct rt_external { long ext_magic; int ext_nbytes; genptr_t ext_buf; }; #define RT_EXTERNAL_MAGIC 0x768dbbd0 #define RT_INIT_EXTERNAL(_p) {(_p)->ext_magic = RT_EXTERNAL_MAGIC; \ (_p)->ext_buf = GENPTR_NULL; (_p)->ext_nbytes = 0;} #define RT_CK_EXTERNAL(_p) RT_CKMAG(_p, RT_EXTERNAL_MAGIC, "rt_external") /* * * D B _ P U T _ E X T E R N A L * * Caller is responsible for freeing memory, using db_free_external(). */ int db_put_external( ep, dp, dbip ) struct rt_external *ep; struct directory *dp; struct db_i *dbip; { if(rt_g.debug&DEBUG_DB) rt_log("db_put_external(%s) ep=x%x, dbip=x%x, dp=x%x\n", dp->d_namep, ep, dbip, dp ); RT_CK_DBI(dbip); RT_CK_EXTERNAL(ep); if( db_put( dbip, dp, (union record *)(ep->ext_buf), 0, (ep->ext_nbytes+sizeof(union record)-1)/sizeof(union record) ) < 0 ) return(-1); return(0); }