使用malloc GNU缓冲区溢出 [英] GNU buffer overflow using malloc

查看:366
本文介绍了使用malloc GNU缓冲区溢出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个循环中运行以下功能:

I am running in a loop the following function:

int* rpermute(int n)
{
    int* a = malloc(n * sizeof(int));
    int  k;

    for (k = 0; k < n; k++)
    {
       a[k] = k;
    }

    for (k = n - 1; k > 0; k--)
    {
       int j    = rand() % (k + 1);
       int temp = a[j];
       a[j]     = a[k];
       a[k]     = temp;
   }

   return a;
}

如果我设置一个新的int变量在我的code每一个变量发生变化,我想这是一个缓冲区溢出问题。

If I set a new int variable in my code every variable is changing, I assume it is a buffer overflow problem.

运行Valgrind的,我得到如下:

Running the valgrind i get the following:

==4459== 73,036 bytes in 19 blocks are definitely lost in loss record 1 of 1

==4459==    at 0x4C2B6CD: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)

==4459==    by 0x402CFB: rpermute (in /home/giwrgos/Desktop/crowdv22/crowd_evacuation)

==4459==    by 0x403378: main (in /home/giwrgos/Desktop/crowdv22/crowd_evacuation)

我用linux通过VirtualBox的,但我已设置足够的存储和RAM,我该怎么办?

I use linux through the virtualbox but I have set enough storage and ram, what should I do?

推荐答案

编辑:请参见下面的海报的评论,这个问题的不毕竟这code

See poster's comment below, the problem is not in this code after all.

您应该简单地免费()分配并返回的内存rpermute()。这必须在code来完成从那里你叫 rpermute(),一旦你用这个数组完成的。

You should simply free() the memory allocated in and returned by rpermute(). This must be done in the code from where you call rpermute(), once you're done with this array.

据我了解,你可以回复此数组不同 INT 值( N 的参数 rpermute())。也许你只是分配一个新的输出数组你保持:

I understand that you regenerate this array for differing int values (the n parameter of rpermute()). Perhaps you simply assign a new output to the array you keep:

int* array;
...
array = rpermute(100);

// Some time later.
array = rpermute(200);  // Previous array memory is leaking.

您应该做的,而不是:

free(array);
array = rpermute(200);

请注意,这不是一个缓冲过流,但内存泄漏代替。我查了你code缓冲区溢出:的 A 点指数成份股之外,但是这似乎是在两个循环确定

Note that this is not a 'buffer over flow', but a memory leak instead. I checked you code for buffer overflow: index of a points outside it, but this seemed to be ok in both loops.

这篇关于使用malloc GNU缓冲区溢出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆