c - Python extension for Upskirt: garbage at end of string -


i've been trying make python extension upskirt. though not too hard first c project since there examples (example program in upskirt code , ruby extension).

the extension works, converts markdown throw @ it, output has garbage @ end of string. , don't know causes it.

here's output:

python test.py  <module 'pantyshot' '/home/frank/code/pantyshot/virtenv/lib/python2.7/site-packages/pantyshot.so'> <built-in function render>  '<p>this <strong>is</strong> <em>a</em> <code>test</code>. <a href="http://example.com">test</a>.</p>\n\x7f' <p>this <strong>is</strong> <em>a</em> <code>test</code>. <a href="http://example.com">test</a>.</p>  --------------------------------------------------------------------------------  '<p>this <strong>is</strong> <em>a</em> <code>test</code>. <a href="http://example.com">test</a>.</p>\n\x7f' <p>this <strong>is</strong> <em>a</em> <code>test</code>. <a href="http://example.com">test</a>.</p>  -------------------------------------------------------------------------------- 

my code can found in my github repo. called pantyshot, because thought of when heard upskirt. strange name, know.

i hope can me.

you doing strdup in pantyshot_render:

output_text = strdup(ob->data); /* ob "struct buf *" */ 

but don't think ob->data nul-terminated c string. you'll find inside upskirt/buffer.c:

/* bufnullterm • nul-termination of string array (making c-string) */ void bufnullterm(struct buf *buf) {     if (!buf || !buf->unit) return;     if (buf->size < buf->asize && buf->data[buf->size] == 0) return;     if (bufgrow(buf, buf->size + 1))         buf->data[buf->size] = 0; } 

so, you're running off end of buffer , getting lucky hitting '\0' before doing damage. think you're supposed call bufnullterm(ob) before copying ob->data c string; or @ ob->size, use malloc , strncpy copy it, , take care of nul-terminator hand (but make sure allocation ob->size + 1 bytes copied string).

and if want rid of newline (i.e. trailing \n), you'll have whitespace stripping hand somewhere.


Comments