Path: utzoo!mnetor!uunet!husc6!uwvax!oddjob!gargoyle!ihnp4!inuxc!iuvax!pur-ee!uiucdcs!bradley!bucc2!brian From: brian@bucc2.UUCP Newsgroups: comp.lang.c Subject: nasty evil pointers Message-ID: <13100003@bucc2> Date: 7 Mar 88 11:10:00 GMT Lines: 95 Nf-ID: #N:bucc2:13100003:000:2598 Nf-From: bucc2.UUCP!brian Mar 7 05:10:00 1988 We lose a lot of a time to pointers running off all over the place. We are using Microsoft C 5.0 under MESS-DOS, and that means no control of memory. We've tried using things like _nullcheck() and _heapwalk(), but the don't help much. It would be nice if we could check every pointer as it was used... something like void pointer_validate(p, n, f) void *p; int n; char *f; That would be called with a pointer, __LINE__, and __FILE__. If pointer pointed, say, into the operating system or the text space, the function would print a message and exit(). Otherwise it would return. Writing such a function is not too difficult. However, getting it called is. It would be nice if we had a utility program like ctrace we could run our code through that would put in calls to this function. Unfortunatley, this would have to have considerable knowledge of the C language to work... In simple cases like void foo(ip) int *ip; { printf("%d", *ip); } would become: void foo(ip) int *ip; { pointer_validate((void *) ip, __LINE__, __FILE__) printf("%d", *ip); } however, something like: char *bar(bp, ip, sp) int *bp, *ip; char *sp; { register int i; char *s; if (sp) s = sp; for (i = *bp; i < 50; i += *ip) s[i] = 'X'; return(s); } is another story entirley. The program would have to put in braces in the if statement, and do something to the for like: char *bar(bp, ip, sp) int *bp, *ip; char *sp; { register int i; char *s; pointer_validate((void *) sp, __LINE__, __FILE__) if (sp) { pointer_validate((void *) sp, __LINE__, __FILE__) s = sp; } else if (!(s = malloc(50))) return(NULL); pointer_validate((void *) bp, __LINE__, __FILE__) for (i=*bp; i<50; pointer_validate((void *) ip, __LINE__, __FILE__), i += *ip) s[i] = 'X'; return(s); } I know these aren't good examples, it 5:00 AM... anyway, you get the idea of the difficulties involved here. Without source to the compiler it could be a major project. Does anybody know a way this could be done, perhaps with something like lex? We don't have lex on the PC, but if we could come up with source from lex on the unix system and move it to the PC, wouldn't it work? (I don't know anything about lex) ............................................................................... When the going gets weird, the weird turn pro. Brian Michael Wendt UUCP: {cepu,ihnp4,uiucdcs,noao}!bradley!brian Bradley University ARPA: cepu!bradley!brian@seas.ucla.edu (309) 691-5175 ICBM: 40 40' N 89 34' W