diff options
| author | 2023-06-18 18:23:13 +0200 | |
|---|---|---|
| committer | 2023-06-18 18:23:13 +0200 | |
| commit | 3990f0f2731c2289eaefefc9aff5bdcaee6c3289 (patch) | |
| tree | 7d97c52dc8018cd07daea062f22f31fa5898a1f1 | |
| parent | 97621f5870d60722ae2bc13682c58d194bc20794 (diff) | |
fix memory bugs
| -rw-r--r-- | common.h | 2 | ||||
| -rw-r--r-- | ir.c | 10 | ||||
| -rw-r--r-- | mem.c | 12 |
3 files changed, 14 insertions, 10 deletions
@@ -54,7 +54,7 @@ hashb(uint h, const void *d, uint n) } static inline uint ptrhash(const void *p) { - return (uint)(size_t)p * 2654435761; + return (uint)(size_t)p * 2654435761u; } static inline uint popcnt(uvlong x) { @@ -183,10 +183,12 @@ union ref mkdatref(const char *name, uint siz, uint align, const void *bytes, uint n, bool deref) { struct irdat dat = { .align = align, .siz = siz, .name = name }; - if (siz <= 8) memcpy(dat.sdat, bytes, n < siz ? n : siz); - else { - while (((uchar *)bytes)[n-1] == 0) --n; /* nip trailing zeroes */ - if (n) vpushn(&dat.dat, bytes, n); + if (bytes) { + if (siz <= 8) memcpy(dat.sdat, bytes, n < siz ? n : siz); + else { + while (((uchar *)bytes)[n-1] == 0) --n; /* nip trailing zeroes */ + if (n) vpushn(&dat.dat, bytes, n); + } } if (!name) { extern const char *intern(const char *); @@ -44,13 +44,15 @@ vinit_(void **p, int *pcap, void *inlbuf, int cap, uint siz) void vpush_(void **p, int *pcap, uint *pn, uint siz) { - if (*pn == *pcap) { /* empty or inline buffer */ + if (*pcap >= 0 && *pn >= *pcap) { /* empty or inline buffer */ int cap = *pcap ? *pcap * 2 : 8; *p = xrealloc(NULL, cap * siz); *pcap = -cap; - } else if (*pn == -*pcap) { /* dyn buf */ + } else if (*pcap < 0 && *pn >= -*pcap) { /* dyn buf */ *p = xrealloc(*p, -(*pcap *= 2) * siz); } else return; + while (*pn >= -*pcap) + *p = xrealloc(*p, -(*pcap *= 2) * siz); assert(-(volatile int)*pcap > *pn && "overflow"); } @@ -59,11 +61,11 @@ vpushn_(void **p, int *pcap, uint *pn, uint siz, const void *dat, uint ndat) { void *beg; - for (uint i = 0; i < ndat; ++i) + *pn += ndat; + while (*pn >= (*pcap < 0 ? -*pcap : *pcap)) vpush_(p, pcap, pn, siz); - beg = (char *)*p + *pn * siz; + beg = (char *)*p + (*pn - ndat) * siz; memcpy(beg, dat, ndat * siz); - *pn += ndat; return beg; } |