Merged pull request #8 from schacon/5985563.

Fix memory leaks
This commit is contained in:
Vicent Martí 2011-04-25 14:08:34 -07:00
commit 3bc7d70898
2 changed files with 9 additions and 4 deletions

View file

@ -227,7 +227,7 @@ bufputc(struct buf *buf, char c) {
/* bufrelease • decrease the reference count and free the buffer if needed */
void
bufrelease(struct buf *buf) {
if (!buf || !buf->unit || !buf->asize) return;
if (!buf) return;
buf->ref -= 1;
if (buf->ref == 0) {
#ifdef BUFFER_STATS
@ -241,7 +241,7 @@ bufrelease(struct buf *buf) {
/* bufreset • frees internal data of the buffer */
void
bufreset(struct buf *buf) {
if (!buf || !buf->unit || !buf->asize) return;
if (!buf) return;
#ifdef BUFFER_STATS
buffer_stat_alloc_bytes -= buf->asize;
#endif

View file

@ -1948,7 +1948,7 @@ static void expand_tabs(struct buf *ob, const char *line, size_t size)
void
ups_markdown(struct buf *ob, struct buf *ib, const struct mkd_renderer *rndrer, unsigned int extensions) {
struct link_ref *lr;
struct buf *text = bufnew(TEXT_UNIT);
struct buf *text;
size_t i, beg, end;
struct render rndr;
@ -1956,6 +1956,10 @@ ups_markdown(struct buf *ob, struct buf *ib, const struct mkd_renderer *rndrer,
if (!rndrer)
return;
text = bufnew(TEXT_UNIT);
if (!text)
return;
rndr.make = *rndrer;
arr_init(&rndr.refs, sizeof (struct link_ref));
parr_init(&rndr.work);
@ -2028,7 +2032,7 @@ ups_markdown(struct buf *ob, struct buf *ib, const struct mkd_renderer *rndrer,
/* adding a final newline if not already present */
if (!text->size)
return;
goto cleanup;
if (text->data[text->size - 1] != '\n' && text->data[text->size - 1] != '\r')
bufputc(text, '\n');
@ -2043,6 +2047,7 @@ ups_markdown(struct buf *ob, struct buf *ib, const struct mkd_renderer *rndrer,
rndr.make.doc_footer(ob, rndr.make.opaque);
/* clean-up */
cleanup:
bufrelease(text);
lr = rndr.refs.base;
for (i = 0; i < (size_t)rndr.refs.size; i += 1) {