简体   繁体   中英

How to reproduce “Stack smashing detected” in C++ application

I get this error constantly in an embedded Linux application. I am trying to locate the problem and I narrowed it down to the following piece of code.

I want to solve this problem, if not I'd appreciate a couple of pointers what might have caused it.

Any suggestions how to reproduce this stack smashing problem is greately appreciated:

    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;
    }

Thanks in advance.

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

Here's a nice easy way:

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

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

The allocation of T on the stack will result in your stacksmash. What you have done is likely similar just not with a single class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM