add the setmalloctags I used to analyse the libsec-plug-some-leaks and find who should be responsible for cleanup of the leaked memory. also removed one now superflous ...=nil; in x509.c Axel. Notes: Mon Sep 19 07:15:50 EDT 2005 rsc I edited this patch before applying it, to remove some of the calls to setmalloctag. betomp illustrates the general philosophy. It looks like this: mpint* betomp(int n) { b = malloc(sizeof *b); b->p = malloc(n); return b; } In this case, b should get tagged with setmalloctag, because the responsibility for freeing (via mpfree) b is being passed to the caller. b->p, on the other hand, has a fine malloc tag as it is. The caller is not responsible for freeing b->p. b->p is hidden from the caller and expected to be freed by the innards of mpfree. Reference: /n/sources/patch/applied/setmalloctag-libsec-libmp Date: Sat Sep 17 22:47:44 CES 2005 Reviewed-by: rsc --- /sys/src/libsec/port/x509.c Sat Sep 17 22:21:39 2005 +++ /sys/src/libsec/port/x509.c Mon Sep 19 13:15:09 2005 @@ -172,6 +172,7 @@ exits("out of memory"); } memset(p, 0, n); + setmalloctag(p, getcallerpc(&n)); return p; } @@ -259,9 +260,11 @@ if(err == ASN_OK) { err = length_decode(pp, pend, &length); if(err == ASN_OK) { - if(tag.class == Universal) + if(tag.class == Universal) { err = value_decode(pp, pend, length, tag.num, isconstr, &val); - else + if(val.tag == VSeq || val.tag == VSet) + setmalloctag(val.u.seqval, getcallerpc(&pp)); + }else err = value_decode(pp, pend, length, OCTET_STRING, 0, &val); if(err == ASN_OK) { pelem->tag = tag; @@ -498,6 +501,7 @@ case SEQUENCE: err = seq_decode(&p, pend, length, isconstr, &vl); + setmalloctag(vl, getcallerpc(&pp)); if(err == ASN_OK) { pval->tag = VSeq ; pval->u.seqval = vl; @@ -506,6 +510,7 @@ case SETOF: err = seq_decode(&p, pend, length, isconstr, &vl); + setmalloctag(vl, getcallerpc(&pp)); if(err == ASN_OK) { pval->tag = VSet; pval->u.setval = vl; @@ -742,6 +747,7 @@ } *pp = p; *pelist = ans; + setmalloctag(ans, getcallerpc(&pp)); return err; } @@ -1402,6 +1408,7 @@ Elist* el; el = (Elist*)emalloc(sizeof(Elist)); + setmalloctag(el, getcallerpc(&e)); el->hd = e; el->tl = tail; return el; @@ -2042,7 +2049,6 @@ /* see 9.2.1 of rfc2437 */ pkcs1 = betomp(signature->data, signature->len, nil); mpexp(pkcs1, pk->ek, pk->n, pkcs1); - pkcs1buf = nil; buflen = mptobe(pkcs1, nil, 0, &pkcs1buf); buf = pkcs1buf; if(buflen != nlen || buf[0] != 1) { --- /sys/src/libmp/port/mptobe.c Sat Sep 17 22:21:49 2005 +++ /sys/src/libmp/port/mptobe.c Sat Sep 17 22:21:47 2005 @@ -15,6 +15,7 @@ if(p == nil){ n = (b->top+1)*Dbytes; p = malloc(n); + setmalloctag(p, getcallerpc(&b)); } if(p == nil) return -1; --- /sys/src/libmp/port/mpaux.c Sat Sep 17 22:22:02 2005 +++ /sys/src/libmp/port/mpaux.c Mon Sep 19 13:14:14 2005 @@ -58,6 +58,7 @@ sysfatal("mpsetminbits: n < 0"); b = mallocz(sizeof(mpint), 1); + setmalloctag(b, getcallerpc(&n)); if(b == nil) sysfatal("mpnew: %r"); n = DIGITS(n); --- /sys/src/libmp/port/betomp.c Sat Sep 17 22:22:25 2005 +++ /sys/src/libmp/port/betomp.c Mon Sep 19 13:14:42 2005 @@ -9,8 +9,10 @@ int m, s; mpdigit x; - if(b == nil) + if(b == nil){ b = mpnew(0); + setmalloctag(b, getcallerpc(&p)); + } // dump leading zeros while(*p == 0 && n > 1){