c++ - 如何在 C++ 应用程序中重现 "Stack smashing detected"

标签 c++ stack stack-smash

我在嵌入式 Linux 应用程序中经常遇到此错误。我试图找出问题所在,并将其缩小到以下代码段。

我想解决这个问题,否则我会很感激一些可能导致它的原因。

非常感谢任何有关如何重现此堆栈粉碎问题的建议:

    uint8_t laststate = HIGH;
    uint8_t counter = 0;
    uint8_t j = 0;
    uint8_t i = 0;
    int data[5] = {0,0,0,0,0};

    int try_again = 1;
    float h = 0.0;
    float c = 0.0;

    int try_count = 0;
    const int max_tries = 30;

    if (this->DHT22_SETUP_ != 1)
    {
        fprintf(stderr,"You havent set up Gpio !\n");
    }
    else
    {

            data[0] = 0;
            data[1] = 0;
            data[2] = 0;
            data[3] = 0;
            data[4] = 0;

            //f = 0.0;
            h = 0.0;
            c = 0.0;
            j = 0;
            i = 0;
            counter = 0;
            laststate = HIGH;

            /* pull pin down for 18 milliseconds */
            pinMode( this->DHT22Pin, OUTPUT );
            digitalWrite( this->DHT22Pin, LOW );
            delay( 18 );

            /* prepare to read the pin */
            pinMode( this->DHT22Pin, INPUT );

            /* detect change and read data */
            for ( i = 0; i < MAX_TIMINGS; i++ )
            {
                counter = 0;
                while ( digitalRead( this->DHT22Pin ) == laststate )
                {
                    counter++;
                    delayMicroseconds( 1 );
                    if ( counter == 255 )
                    {
                        break;
                    }
                }
                laststate = digitalRead( this->DHT22Pin );

                if ( counter == 255 )
                    break;

                /* ignore first 3 transitions */
                if ( (i >= 4) && (i % 2 == 0) )
                {
                    /* shove each bit into the storage bytes */
                    data[j / 8] <<= 1;
                    if ( counter > 16 )
                        data[j / 8] |= 1;
                    j++;
                }
            }


            /*
             * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
             * print it out if data is good
             */
            if ((j >= 40) &&
                 (data[4] == ( (data[0] + data[1] + data[2] + data[3]) & 0xFF) ) )
            {
                h = (float)((data[0] << 8) + data[1]) / 10;
                if ( h > 100 )
                {
                    h = data[0];    // for DHT11
                }
                c = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10;
                if ( c > 125 )
                {
                    c = data[2];    // for DHT11
                }
                if ( data[2] & 0x80 )
                {
                    c = -c;
                }
                //f = c * 1.8f + 32;
    #ifdef DEBUG
                printf( "Humidity = %.1f %% Temperature = %.1f *C (%.1f *F)\n", h, c, f );
    #endif
                try_again = 0;

                if (h == 0)
                {
                    try_again = 1;
                }
            }
            else
            {
                /* Data not good */
                try_again = 1;
                return 0.0;
                //printf ("Data not good, skipping\n");
            }

        /* Return humidity */
        return h;
    }

提前致谢。

最佳答案

如果 MAX_TIMINGS 大于 83,并且如果 counteri 超过 83 阈值之前没有达到 255,则 detect change and read data 循环重复了那么多次,因此 ignore first 3 transitions if-expression block 被执行了 >40 次(可能有一些 off-by-我的快速分析中有一个错误),因此 j 最终是 >40,这意味着 j/8 将是 >4,这意味着它超出了 的范围data 数组,因此在这种情况下访问 data[j/8] 具有未定义的行为。

关于c++ - 如何在 C++ 应用程序中重现 "Stack smashing detected",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48525463/

相关文章:

c++ - 不匹配调用指向成员函数的 std::function 对象

c++ - 使用 cin 跳过预期的字符,如 scanf()

java - 为什么使用toString方法后仍然得到[]?

c++ - 如何调试 'Stack smashing detected' ?

c++ - 汇编代码是否试图到达相同的寄存器,在不同的线程中损坏任何东西?

c++ - 为什么 `cin >> noskipws` 不等待输入?

c - 使用链表的堆栈实现有什么问题?

python - 如何在 Python 中迭代元组的堆栈

c - *** 检测到堆栈粉碎 *** 中止(核心转储)

c - 发送 ICMP 数据包时检测到堆栈崩溃