ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

python中的错误:大小与prev_size损坏

2019-11-11 07:05:44  阅读:1489  来源: 互联网

标签:c-3 python


我用c编写了python扩展.
我使用c程序加密字符串.
我的C程序:

#include <Python.h>

static const char *codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static const unsigned char map[256] = {
        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 253, 255,
        255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255, 255, 253, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255,  62, 255, 255, 255,  63,
        52,  53,  54,  55,  56,  57,  58,  59,  60,  61, 255, 255,
        255, 254, 255, 255, 255,   0,   1,   2,   3,   4,   5,   6,
        7,   8,   9,  10,  11,  12,  13,  14,  15,  16,  17,  18,
        19,  20,  21,  22,  23,  24,  25, 255, 255, 255, 255, 255,
        255,  26,  27,  28,  29,  30,  31,  32,  33,  34,  35,  36,
        37,  38,  39,  40,  41,  42,  43,  44,  45,  46,  47,  48,
        49,  50,  51, 255, 255, 255, 255, 255, 255, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
        255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
        255, 255, 255, 255
};

int length(char *str){
    int i = 0;
    while (str[i] != '\0') i++;
    return i;
}

char *str_encrypt(char *in) {
    unsigned long len = (unsigned long)length(in);
    unsigned long index, l_even;
    char *p;
    char *out = (char *) PyMem_Malloc (len * 4 / 3 + 1);
    p = out;
    /* valid output size ? */
    l_even = 3 * (len / 3);
    for (index = 0; index < l_even; index += 3) {
        *p++ = codes[in[0] >> 2];
        *p++ = codes[((in[0] & 3) << 4) + (in[1] >> 4)];
        *p++ = codes[((in[1] & 0xf) << 2) + (in[2] >> 6)];
        *p++ = codes[in[2] & 0x3f];
        in += 3;
    }
    /* Pad it if necessary...  */
    if (index < len) {
        unsigned a = (unsigned)in[0];
        unsigned b = (unsigned)((index+1 < len) ? in[1] : 0);
        unsigned c = 0;
        *p++ = codes[a >> 2];
        *p++ = codes[((a & 3) << 4) + (b >> 4)];
        *p++ = (char)((index+1 < len) ? codes[((b & 0xf) << 2) + (c >> 6)] : '=');
        *p++ = '=';
    }
    /* append a NULL byte */
    *p = '\0';
    return out;
}

static PyObject *wrap_encrypt_str(PyObject *self, PyObject *args) {
    char *input_str;
    if (!PyArg_ParseTuple(args, "s", &input_str)) {
        return NULL;
    }
    char *result = str_encrypt(input_str);
    PyObject *pyObject = Py_BuildValue("s", result);
    PyMem_Free(result);
    return pyObject;
}

/* registration table  */
static PyMethodDef wrap_methods[] ={
        {"encrypt_str", wrap_encrypt_str, METH_VARARGS},       /* method name, C func ptr, always-tuple */
        {NULL, NULL}                   /* end of table marker */
};

/* module initializer */
PyMODINIT_FUNC initencryption(void)                       /* called on first import */
{                                      /* name matters if loaded dynamically */
    (void) Py_InitModule("encryption", wrap_methods);   /* mod name, table ptr */
}

然后我写一个名为setup.py的文件.
setup.py中的程序在这里:

from setuptools import setup, Extension
setup(
    name="extest",
    packages=["extest", "extest.math", "extest.test"],
    version="0.0.1",
    zip_safe=False,
    ext_modules=[
        Extension("extest.test.encryption", sources=['extest/test/encryption.c'], extra_compile_args=["-Wno-char-subscripts"])
    ],
    long_description='',
    classifiers=[
        "Environment :: Web Environment",
        "Intended Audience :: Developers",
        "Operating System :: OS Independent",
        'Programming Language :: Python :: 2',
        'Programming Language :: Python :: 2.6',
        'Programming Language :: Python :: 2.7'
    ]
)

我运行python setup.py install并成功安装扩展.
我将模块导入python程序.
像这样:

from extest.test.encryption import encrypt_str
a = encrypt_str("asdfg")
print a

运行成功.
但是,当我将a = crypto_str(“ asdfg”)替换为= crypto_str(“ / static / hello.js”)时,出现错误,程序崩溃了!
错误信息在这里:

*** Error in `python': corrupted size vs. prev_size: 0x0000000000aaca00 ***

是什么原因?如何解决?

解决方法:

您没有足够的内存来分配价值,
我修改了此行代码char * out =(char *)PyMem_Malloc(len * 4/3 1);到char * out =(char *)PyMem_Malloc(len * 2);然后重建该程序.我看到问题已经解决.

标签:c-3,python
来源: https://codeday.me/bug/20191111/2018386.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有