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

How to reproduce "Stack smashing detected" in C++ application

我在嵌入式 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;
    }

提前致谢。

这是一个很简单的方法:

class T {
   char big[<Some number bigger than your stack size>];
}

int main() {
    T bang;
    return 0;
}

在堆栈上分配 T 将导致您的 stacksmash。您所做的可能与单个 class.

相似

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