C语言写Python模块扩展溢出

这几天写了一个用C语言实现的Python模块,用于生成随机数和随机字符串,然后,编译啥的都没问题,但是测试的时候从python shell中import 进去,计算到最后一步溢出来。。。
主要功能就算是从python中传入一个文件名称,然后将生成的随机字符串保存进去。

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include "Python.h"

static PyObject *xrand(PyObject *self, PyObject *args);

static PyObject *xrand(PyObject *self, PyObject *args)
{
    FILE *fp;
    int j = 9999, i;
    int number, small_i;
    char string, name;
    int choice, choice_string;
    if(!PyArg_ParseTuple(args, "s", &name))
    {
        return NULL;
    }

    fp = fopen(&name, "at");
    if(fp)
    {
        printf("successful\n");
    }
    else
    {
        printf("error\n");
    }
    srand(time(0));

    for(i = 0; i < j; i ++)
    {
        for(small_i = 0; small_i < 8; small_i ++)
        {
            choice = rand() % 2;
            if(choice == 0)
            {
                number = rand() % 10;
                fprintf(fp, "%d", number);
            }
            else
            {
                choice_string = rand() % 2;
                if(choice_string == 0)
                {
                    string = 'a' + rand() % 26;
                }
                else
                {
                    string = 'A' + rand() % 26;
                }
                fprintf(fp, "%c", (char)string);
                printf("write successfule\n");
            }
        }
        fprintf(fp, "%s",  "\n");
        printf("wirte successful\n");
    }
    fclose(fp);
    printf("close successful\n");
    return 0;
}

static PyMethodDef randMethods[] =
{
    {"xrand", (PyCFunction)xrand, METH_VARARGS, "xrand.c Python module"},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC initxrand(void)
{
    Py_InitModule("xrand", randMethods);
}

下面的是报错

write successfule
write successfule
wirte successful
close successful
*** stack smashing detected ***: python terminated
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x731af)[0x7fdf6ad9e1af]
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x37)[0x7fdf6ae23cf7]
/lib/x86_64-linux-gnu/libc.so.6(__fortify_fail+0x0)[0x7fdf6ae23cc0]
/home/jay/Documents/hash/mod/build/lib.linux-x86_64-2.7/xrand.so(+0xc2c)[0x7fdf6a324c2c]
python(PyEval_EvalFrameEx+0x355)[0x4c9e05]
python(PyEval_EvalCodeEx+0x411)[0x4c87a1]
python[0x5030ef]
python(PyRun_InteractiveOneFlags+0x198)[0x449b84]
python(PyRun_InteractiveLoopFlags+0xba)[0x449937]
python[0x42d96b]
python(Py_Main+0x562)[0x4982f2]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5)[0x7fdf6ad4cb45]
python[0x497ca0]
======= Memory map: ========
00400000-0071f000 r-xp 00000000 fe:00 139649                             /usr/bin/python2.7
0091e000-0091f000 r--p 0031e000 fe:00 139649                             /usr/bin/python2.7
0091f000-0099c000 rw-p 0031f000 fe:00 139649                             /usr/bin/python2.7
0099c000-009ae000 rw-p 00000000 00:00 0 
00b22000-00c1a000 rw-p 00000000 00:00 0                                  [heap]
7fdf6a10e000-7fdf6a124000 r-xp 00000000 fe:00 258574                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fdf6a124000-7fdf6a323000 ---p 00016000 fe:00 258574                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fdf6a323000-7fdf6a324000 rw-p 00015000 fe:00 258574                     /lib/x86_64-linux-gnu/libgcc_s.so.1
7fdf6a324000-7fdf6a325000 r-xp 00000000 fe:04 34735625                   /home/jay/Documents/hash/mod/build/lib.linux-x86_64-2.7/xrand.so
7fdf6a325000-7fdf6a524000 ---p 00001000 fe:04 34735625                   /home/jay/Documents/hash/mod/build/lib.linux-x86_64-2.7/xrand.so
7fdf6a524000-7fdf6a525000 r--p 00000000 fe:04 34735625                   /home/jay/Documents/hash/mod/build/lib.linux-x86_64-2.7/xrand.so
7fdf6a525000-7fdf6a526000 rw-p 00001000 fe:04 34735625                   /home/jay/Documents/hash/mod/build/lib.linux-x86_64-2.7/xrand.so
7fdf6a526000-7fdf6a54c000 r-xp 00000000 fe:00 259445                     /lib/x86_64-linux-gnu/libtinfo.so.5.9
7fdf6a54c000-7fdf6a74b000 ---p 00026000 fe:00 259445                     /lib/x86_64-linux-gnu/libtinfo.so.5.9
7fdf6a74b000-7fdf6a74f000 r--p 00025000 fe:00 259445                     /lib/x86_64-linux-gnu/libtinfo.so.5.9
7fdf6a74f000-7fdf6a750000 rw-p 00029000 fe:00 259445                     /lib/x86_64-linux-gnu/libtinfo.so.5.9
7fdf6a750000-7fdf6a790000 r-xp 00000000 fe:00 260090                     /lib/x86_64-linux-gnu/libreadline.so.6.3
7fdf6a790000-7fdf6a990000 ---p 00040000 fe:00 260090                     /lib/x86_64-linux-gnu/libreadline.so.6.3
7fdf6a990000-7fdf6a992000 r--p 00040000 fe:00 260090                     /lib/x86_64-linux-gnu/libreadline.so.6.3
7fdf6a992000-7fdf6a999000 rw-p 00042000 fe:00 260090                     /lib/x86_64-linux-gnu/libreadline.so.6.3
7fdf6a999000-7fdf6a99a000 rw-p 00000000 00:00 0 
7fdf6a99a000-7fdf6a99f000 r-xp 00000000 fe:00 139870                     /usr/lib/python2.7/lib-dynload/readline.x86_64-linux-gnu.so
7fdf6a99f000-7fdf6ab9f000 ---p 00005000 fe:00 139870                     /usr/lib/python2.7/lib-dynload/readline.x86_64-linux-gnu.so
7fdf6ab9f000-7fdf6aba0000 r--p 00005000 fe:00 139870                     /usr/lib/python2.7/lib-dynload/readline.x86_64-linux-gnu.so
7fdf6aba0000-7fdf6aba2000 rw-p 00006000 fe:00 139870                     /usr/lib/python2.7/lib-dynload/readline.x86_64-linux-gnu.so
7fdf6aba2000-7fdf6ad2b000 r--p 00000000 fe:00 145216                     /usr/lib/locale/locale-archive
7fdf6ad2b000-7fdf6aecd000 r-xp 00000000 fe:00 299793                     /lib/x86_64-linux-gnu/libc-2.19.so
7fdf6aecd000-7fdf6b0cc000 ---p 001a2000 fe:00 299793                     /lib/x86_64-linux-gnu/libc-2.19.so
7fdf6b0cc000-7fdf6b0d0000 r--p 001a1000 fe:00 299793                     /lib/x86_64-linux-gnu/libc-2.19.so
7fdf6b0d0000-7fdf6b0d2000 rw-p 001a5000 fe:00 299793                     /lib/x86_64-linux-gnu/libc-2.19.so
7fdf6b0d2000-7fdf6b0d6000 rw-p 00000000 00:00 0 
7fdf6b0d6000-7fdf6b1d6000 r-xp 00000000 fe:00 299797                     /lib/x86_64-linux-gnu/libm-2.19.so
7fdf6b1d6000-7fdf6b3d5000 ---p 00100000 fe:00 299797                     /lib/x86_64-linux-gnu/libm-2.19.so
7fdf6b3d5000-7fdf6b3d6000 r--p 000ff000 fe:00 299797                     /lib/x86_64-linux-gnu/libm-2.19.so
7fdf6b3d6000-7fdf6b3d7000 rw-p 00100000 fe:00 299797                     /lib/x86_64-linux-gnu/libm-2.19.so
7fdf6b3d7000-7fdf6b3f1000 r-xp 00000000 fe:00 258799                     /lib/x86_64-linux-gnu/libz.so.1.2.8
7fdf6b3f1000-7fdf6b5f0000 ---p 0001a000 fe:00 258799                     /lib/x86_64-linux-gnu/libz.so.1.2.8
7fdf6b5f0000-7fdf6b5f1000 r--p 00019000 fe:00 258799                     /lib/x86_64-linux-gnu/libz.so.1.2.8
7fdf6b5f1000-7fdf6b5f2000 rw-p 0001a000 fe:00 258799                     /lib/x86_64-linux-gnu/libz.so.1.2.8
7fdf6b5f2000-7fdf6b5f4000 r-xp 00000000 fe:00 299811                     /lib/x86_64-linux-gnu/libutil-2.19.so
7fdf6b5f4000-7fdf6b7f3000 ---p 00002000 fe:00 299811                     /lib/x86_64-linux-gnu/libutil-2.19.so
7fdf6b7f3000-7fdf6b7f4000 r--p 00001000 fe:00 299811                     /lib/x86_64-linux-gnu/libutil-2.19.so
7fdf6b7f4000-7fdf6b7f5000 rw-p 00002000 fe:00 299811                     /lib/x86_64-linux-gnu/libutil-2.19.so
7fdf6b7f5000-7fdf6b7f8000 r-xp 00000000 fe:00 299796                     /lib/x86_64-linux-gnu/libdl-2.19.so
7fdf6b7f8000-7fdf6b9f7000 ---p 00003000 fe:00 299796                     /lib/x86_64-linux-gnu/libdl-2.19.so
7fdf6b9f7000-7fdf6b9f8000 r--p 00002000 fe:00 299796                     /lib/x86_64-linux-gnu/libdl-2.19.so
7fdf6b9f8000-7fdf6b9f9000 rw-p 00003000 fe:00 299796                     /lib/x86_64-linux-gnu/libdl-2.19.so
7fdf6b9f9000-7fdf6ba11000 r-xp 00000000 fe:00 274527                     /lib/x86_64-linux-gnu/libpthread-2.19.so
7fdf6ba11000-7fdf6bc10000 ---p 00018000 fe:00 274527                     /lib/x86_64-linux-gnu/libpthread-2.19.so
7fdf6bc10000-7fdf6bc11000 r--p 00017000 fe:00 274527                     /lib/x86_64-linux-gnu/libpthread-2.19.so
7fdf6bc11000-7fdf6bc12000 rw-p 00018000 fe:00 274527                     /lib/x86_64-linux-gnu/libpthread-2.19.so
7fdf6bc12000-7fdf6bc16000 rw-p 00000000 00:00 0 
7fdf6bc16000-7fdf6bc36000 r-xp 00000000 fe:00 274528                     /lib/x86_64-linux-gnu/ld-2.19.so
7fdf6bca2000-7fdf6be18000 rw-p 00000000 00:00 0 
7fdf6be2b000-7fdf6be2d000 rw-p 00000000 00:00 0 
7fdf6be2d000-7fdf6be34000 r--s 00000000 fe:00 177736                     /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
7fdf6be34000-7fdf6be36000 rw-p 00000000 00:00 0 
7fdf6be36000-7fdf6be37000 r--p 00020000 fe:00 274528                     /lib/x86_64-linux-gnu/ld-2.19.so
7fdf6be37000-7fdf6be38000 rw-p 00021000 fe:00 274528                     /lib/x86_64-linux-gnu/ld-2.19.so
7fdf6be38000-7fdf6be39000 rw-p 00000000 00:00 0 
7ffd62c25000-7ffd62c46000 rw-p 00000000 00:00 0                          [stack]
7ffd62d00000-7ffd62d02000 r-xp 00000000 00:00 0                          [vdso]
7ffd62d02000-7ffd62d04000 r--p 00000000 00:00 0                          [vvar]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted

可以看到,C已经将fp关闭释放来,但是还是报错。。最后生成文件内容所正确的,但是名字所乱码,是不是从python中传入参数的时候有问题,我在想,但是又没有任何思路。。。各位大神帮帮忙啦~~

2个回答

好吧,自己的问题自己解决,最后查明,原因是

char name;

那里的问题,要写成这样,希望给后面的同志一些启示吧

char *name;

像是有空指针等调用,你c中打印一下,看接收到的文件名等是否正确,然后就是c是否处理正确字符串

isinstance
isinstance 是的,后来我又看了一遍官方的文档,是name那里要定义为指针*name
4 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐