简体   繁体   English

从头开始编写 heapify 函数,获得“基于堆栈的缓冲区溢出”

[英]Writing heapify function from scratch, getting a “stack-based buffer overrun”

I am trying to implement the heap sort algorithm for the first time, but I am getting an error with the heapify function.我第一次尝试实现堆排序算法,但是我在使用 heapify 函数时遇到错误。

Unhandled exception at 0x0005369A in heapify.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.

The console does open, and the output is 999 10 5 11 1012398875 2 0 1 .控制台确实打开了,输出是999 10 5 11 1012398875 2 0 1

Could someone help me understand what is going wrong here?有人能帮我理解这里出了什么问题吗? Thank you.谢谢你。

#include <iostream>

// given the address of element 0 of an array, and a non-zero index k, heapify assumes that the L/R subtrees
// of node k are max heaps. But the subtrees combined with node k do not necesarily form 
// a max heap. heapify interchanges the value at node k with the value of one of its children,
// and then calls itself on the subtree in question 
int heapify(int* n, int k, int sizeOfHeap)
{
    // terminate the function if the input "node" is not actually a node
    if (k > sizeOfHeap)
    {
        return 0;
    }
        int root = *(n + k); // value of kth node
        int leftChild = *(n + 2 * k); // value of left chold
        int rightChild = *(n + 2 * k + 1); // value of right child
        if (root < leftChild)
        {
            // swap value of kth node with value of its left child
            int temp = root;
            *(n + k) = leftChild;
            *(n + 2 * k) = root;

            // call heapify on the left child
            heapify(n, 2 * k, sizeOfHeap);
        }
        else
        {
            // swap value of kth node with value of its right child
            int temp = root;
            *(n + k) = rightChild;
            *(n + 2 * k + 1) = root;

            // call heapify on right child
            heapify(n, 2 * k + 1, sizeOfHeap);
        }
    
}

int main()
{
    // arr is the array we will heapify. 999 is just a placeholder. 
    // The actual (almost) heap occupies indices 1 - 7
    int arr[8] = {999, 3, 10, 11, 5, 2, 0, 1};
    int sizeOfHeap = 8;
    
    heapify(arr, 1, sizeOfHeap);

    // print out arr
    int i;
    for (i = 0; i <= 7; i++)
    {
        std::cout << arr[i] << std::endl;
    }
}
 

Unhandled exception at 0x0005369A in heapify.exe: Stack cookie instrumentation code detected a stack-based buffer overrun.

The console does open, and the output is 999 10 5 11 1012398875 2 0 1.控制台确实打开了,输出是 999 10 5 11 1012398875 2 0 1。

Could someone help me understand what is going wrong here?有人能帮我理解这里出了什么问题吗? Thank you.谢谢你。

Stack of process (one of real-live uses of stack data structure, FILO queue ) is the place in memory for static allocation.进程堆栈堆栈数据结构的实际用途之一,FILO 队列)是内存中用于静态分配的地方。 Always small and mostly same size for all processes.对于所有进程,始终很小且大小基本相同。 On stack, still, compiler save local variables ie small statically allocated buffers (this happens then the stack pointer is, on Linux, moved to expand the stack size, and compiler evaluate offsets on stack).在堆栈上,编译器仍然保存局部变量,即小的静态分配的缓冲区(发生这种情况时,在 Linux 上,堆栈指针被移动以扩展堆栈大小,编译器评估堆栈上的偏移量)。 They (buffers) could not be handled correctly (unsafe lib functions, like strcpy() ) so they could be potentially overflowed (overrunned) leading to buffer overflow vulnerability.它们(缓冲区)无法正确处理(不安全的库函数,例如strcpy() ),因此它们可能会溢出(溢出),从而导致缓冲区溢出漏洞。

Stack cookie AKA stack canary is mitigation technique for writing sequential data on stack while attacker try to exploit vulnerability like stack buffer overflow , but not limited to (if You do stack pivot from heap back to heap but badly overwrite saved instruction pointer... nevermind ;) ). Stack cookie AKA stack canary 是一种缓解技术,用于在攻击者尝试利用堆栈缓冲区溢出等漏洞时在堆栈上写入顺序数据,但不限于(如果您将堆栈从堆转回堆但严重覆盖保存的指令指针......没关系;) )。 If the overflow is detected then they raise SegFault.如果检测到溢出,则它们会引发 SegFault。 Example link with example of exploitation. 示例链接与利用示例。

This answers Your direct question (understand what is going wrong).这回答了您的直接问题(了解出了什么问题)。

Now, You should debug it and then narrow down the issue.现在,您应该对其进行调试,然后缩小问题的范围。 Especially ask the next question, not edit again.特别是问下一个问题,不再编辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数组的重排元素:基于堆栈的缓冲区溢出错误 - Shuffling elements of Array : stack-based buffer overrun error 0x00363A09处未处理的异常,堆栈cookie工具代码检测到基于堆栈的缓冲区溢出 - Unhandled exception at 0x00363A09, Stack cookie instrumentation code detected a stack-based buffer overrun 如何检测哪个变量/代码正在创建基于堆栈的缓冲区溢出 - How to detect which variable/code is creating a stack-based buffer overrun 为什么我的互操作代码会抛出“堆栈 cookie 检测代码检测到基于堆栈的缓冲区溢出”异常? - Why does my Interop code throw a "Stack cookie instrumentation code detected a stack-based buffer overrun" exception? 基于堆栈的虚拟机功能调用/返回实现问题 - Stack-based virtual machine function call/return implementation issues 基于堆栈的回文检查器 - Stack-based palindrome checker 什么是基于堆栈的引用? - What is stack-based reference? 带有LoadLibrary的STATUS_STACK_BUFFER_OVERRUN - STATUS_STACK_BUFFER_OVERRUN with LoadLibrary 如何在Windows上报告堆栈缓冲区溢出? - How to report a stack buffer overrun on Windows? 基于堆栈的迷宫算法背后的逻辑 - Logic behind a stack-based maze algorithm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM