mbed webserver crashing because of memcpy

340 views Asked by At

I used FRDM-K64F microcontroller to create a webserver using C language.My webserver is offering a web page with some text boxes and display msgs. My next challenge was to store ip addresses and bit srteams provided by the user.

I created two global arrays ipaddr[3][17] and bits[3][17] to store the bits and ipaddresses, i now want to increase the size of the array, but i observed if i increase the size of array even ipaddr[4][17], it crashes my webserver. following is the code which copies the user provided string to the arrays.

void testFunc(char * str)
{    
    char *ht="://";  
    char *ur="/cgi-bin/ext/e_alarm.cgi?zone=0";   
    char *ip;  //ip address to be stored from the user

    ip = new char[15];

    char *pch; //use for tokenizing the string
    char *htt; //combining the strings
    char *b; //bits to be stored from the user

    b = new char [6];

    if(strstr(str,"trigger") != NULL)
    { 
        //ip1="http://192.168.0.233/cgi-bin/ext/e_alarm.cgi?zone=0";
        RED_ON; GREEN_OFF; BLUE_OFF; 

        pch = strtok (str,"&=");

        ip = pch;

        pch = strtok (NULL, "&=");
        pch = strtok (NULL, "&=");

        b = pch;

        pch = strtok (NULL, "=");

        htt = pch;

        strcat(htt, ht); 

        strcat(htt, ip);  
        strcat(htt, ur);  

        printf("htt is %s \n",htt);

        memcpy(ipaddr[counter], ip, strlen(ip));
        memcpy(bits[counter], b, strlen(b));

        counter++;
        printf("\n");

        for (int k = 0; k < counter; k++) 
        {
            printf("IP ADDRESSES:  %s", ipaddr[k]);
            printf("...Bits %s \n", bits[k]);
        }
    }   
}

where str is the data provided by the user, i then used some tokenization techniques to extract ip and bits. When user press the button the program calls this function, strange thing is that my web server crashes just after getting the ip address, when i put the assign ip in the url of my browser, it crashes after couple of refreshes. If i removed the memcpy functions it works fine or if i reduce the size of gloabally declare arrays, that is ipaddr and bits to [3][17], it works fine as well.

On a side note, I am using 74KB of flash memory and 54 KB of RAM, where as total memory of MBED microcontroller is 1MB of flash and 512KB of RAM.

0

There are 0 answers