if MKSH_AFREE_DEBUG is defined, guard against wrong frees

This commit is contained in:
tg
2008-07-13 16:43:55 +00:00
parent 04bc7ba51a
commit a9f219dd60
2 changed files with 16 additions and 3 deletions

15
alloc.c
View File

@ -29,7 +29,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/alloc.c,v 1.6 2007/06/05 18:59:54 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/alloc.c,v 1.7 2008/07/13 16:43:55 tg Exp $");
struct link {
struct link *prev;
@ -102,12 +102,25 @@ void
afree(void *ptr, Area *ap)
{
struct link *l;
#ifdef MKSH_AFREE_DEBUG
struct link *lp;
#endif
if (!ptr)
return;
l = P2L(ptr);
#ifdef MKSH_AFREE_DEBUG
for (lp = ap->freelist; lp != NULL; lp = lp->next)
if (l == lp)
break;
if (lp == NULL) {
internal_warningf("afree: pointer %p not allocated", ptr);
exit(255);
}
#endif
if (l->prev)
l->prev->next = l->next;
else