Upskirt 的 Python 扩展:字符串末尾出现垃圾
我一直在尝试为 Upskirt 制作一个 Python 扩展。我认为对于第一个 C 项目来说这不会太难,因为有示例(Upskirt 代码和 Ruby 扩展中的示例程序)。
该扩展可以工作,它会转换我扔给它的 Markdown,但有时输出的字符串末尾有一些垃圾。而且我不知道是什么原因造成的。
以下是一些输出:
python test.py
<module 'pantyshot' from '/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>
--------------------------------------------------------------------------------
我的代码可以在 我的 Github 存储库 中找到。我称之为 pantyshot,因为当我听到超短裙时我就想到了这个。奇怪的名字,我知道。
我希望有人能帮助我。
I've been trying to make a Python extension for Upskirt. I though it would not be too hard for a first C project since there are examples (example program in the Upskirt code and the Ruby extension).
The extension works, it converts the Markdown I throw at it, but sometimes the output has some garbage at the end of the string. And I don't know what causes it.
Here's some output:
python test.py
<module 'pantyshot' from '/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 be found in my Github repo. I called it pantyshot, because I thought of that when I heard upskirt. Strange name, I know.
I hope someone can help me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在
pantyshot_render 中执行
:strdup
但我不认为
ob->data
是一个以 nul 结尾的 C 字符串。你会在upskirt/buffer.c< 中找到它/code>
:
所以,您可能已经跑出了缓冲区的末尾,并在造成任何损害之前幸运地击中了
'\0'
。我认为你应该在将ob->data
复制为 C 字符串之前调用bufnullterm(ob)
;或者您可以查看ob->size
,使用
malloc
和strncpy
复制它,并手动处理 nul 终止符(但确保分配ob- >size + 1
字节用于复制的字符串)。如果您想删除换行符(即尾随的
\n
),那么您可能需要在某处手动删除空格。You are doing a
strdup
inpantyshot_render
:But I don't think
ob->data
is a nul-terminated C string. You'll find this insideupskirt/buffer.c
:So, you're probably running off the end of the buffer and getting lucky by hitting a
'\0'
before doing any damage. I think you're supposed to callbufnullterm(ob)
before copyingob->data
as a C string; or you could look atob->size
, usemalloc
andstrncpy
to copy it, and take care of the nul-terminator by hand (but make sure you allocationob->size + 1
bytes for your copied string).And if you want to get rid of the newline (i.e. the trailing
\n
), then you'll probably have to do some whitespace stripping by hand somewhere.