Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!mnetor!uunet!seismo!mimsy!oddjob!gargoyle!jpusa1!news From: news@jpusa1.UUCP (usenet) Newsgroups: comp.unix.xenix Subject: SCO Xenix 'C' pointer bug Message-ID: <319@jpusa1.UUCP> Date: Fri, 28-Aug-87 19:44:10 EDT Article-I.D.: jpusa1.319 Posted: Fri Aug 28 19:44:10 1987 Date-Received: Sun, 30-Aug-87 06:05:39 EDT References: <227@krebs.acc.virginia.edu> Reply-To: stu@jpusa1.UUCP (Stu Heiss) Followup-To: comp.unix.xenix Organization: JPUSA - Chicago, IL Lines: 46 Summary: Expires: In article <227@krebs.acc.virginia.edu> wrp@krebs.acc.virginia.edu (Wm Pearson) writes: - - Attached is a test program which displays a bug I just found -in the SCO 'C' development system 2.1.4 release G. Compile the -program and run it. You will find the value of 'dpos' goes up to 3999 -just fine, but then becomes negative. I would appreciate a work-around -that does not require a division by sizeof(struct tstruct). Note -that although the pointers are being treated as ints (intead of unsigned) -in the expression (int)(dptr-diag), they are being treated properly in -the 'dptr<&diag[5000]. - -============= test.c -Bill Pearson -wrp@virginia.BITNET -...!seismo!virginia!wrp - - -#include - -struct tstruct { - int one; - int two; - int three; - int four; - } *diag, *dptr; - -main() -{ - char *calloc(); - unsigned int dpos; - - diag = (struct tstruct *)calloc(5000,sizeof(struct tstruct)); - for (dptr=&diag[99]; dptr<&diag[5000]; dptr +=100) { - dpos = (unsigned int)(dptr-diag); - printf("%4d %4d\n",dpos,(int)(dptr-diag)); - } -} -============= The problem seems to be incorect pointer arithmetic and I don't see any work around. When the expression '(char *)dptr-(char *)diag' becomes greater than 0x7fff, there is sign extension(!!!). This is before the scaling of sizeof(tstruct). Use a macro like #define pos(p,base,type) ((unsigned)((char *)p-(char *)base)/sizeof(type)) and 'dpos = pos(dptr,diag,struct tstruct);' Stu Heiss {gargoyle,ihnp4}!jpusa1!stu