[SOLVED] Problem with os_strcat and char-array

Stational
Posts: 42
Joined: Sat Jul 02, 2016 10:54 pm

[SOLVED] Problem with os_strcat and char-array

Postby Stational » Mon Jul 04, 2016 7:35 pm

Hello,

this is my attempt to create a HTTP Get request header:

Code: Select all

   char requestText[120];
   strcat(requestText, "GET /myPHP.php?action=insert&value=");
   strcat(requestText, adcValue);
   strcat(requestText, "/ HTTP/1.1\r\nUser-Agent: curl/7.37.0\r\nHost: ");
   strcat(requestText, NET_DOMAIN);
   strcat(requestText, "\r\nAccept: */*\r\n\r\n");


this is what i get if i use

Code: Select all

os_printf(requestText);



Ô
@T«GET /myPHP.php?action=insert&value=155/ HTTP/1.1
User-Agent: curl/7.37.0
Host: http://www.myHost.org
Accept: */*


I don't know where these strange chars in front of the 'GET' come from? How do i get rid of them?

tobewinner
Posts: 45
Joined: Tue Jan 19, 2016 2:39 pm

Re: Problem with os_strcat and char-array

Postby tobewinner » Tue Jul 05, 2016 10:44 am

Stational wrote:Hello,

this is my attempt to create a HTTP Get request header:

Code: Select all

   char requestText[120];
   strcat(requestText, "GET /myPHP.php?action=insert&value=");
   strcat(requestText, adcValue);
   strcat(requestText, "/ HTTP/1.1\r\nUser-Agent: curl/7.37.0\r\nHost: ");
   strcat(requestText, NET_DOMAIN);
   strcat(requestText, "\r\nAccept: */*\r\n\r\n");


this is what i get if i use

Code: Select all

os_printf(requestText);



Ô
@T«GET /myPHP.php?action=insert&value=155/ HTTP/1.1
User-Agent: curl/7.37.0
Host: http://www.myHost.org
Accept: */*


I don't know where these strange chars in front of the 'GET' come from? How do i get rid of them?

You should add

Code: Select all

memset(requestText, 0, 120);
before strcat
or you can change

Code: Select all

char requestText[120];
to

Code: Select all

char requestText[120] = {0};

Stational
Posts: 42
Joined: Sat Jul 02, 2016 10:54 pm

Re: Problem with os_strcat and char-array

Postby Stational » Tue Jul 05, 2016 3:09 pm

Thanks a lot for your answer,

I found out, that this works also very well:

Code: Select all

os_sprintf(requestText, "GET /mySQLFunction.php?action=insert&value=%d / HTTP/1.1\r\nUser-Agent: curl/7.37.0\r\nHost: %s\r\nAccept: */*\r\n\r\n",adcRead,NET_DOMAIN);

pratik

Re: Problem with os_strcat and char-array

Postby pratik » Tue Jul 05, 2016 4:52 pm

Hello Stational,

Please explicitly initialize all variables and dynamically allocated memory for reliable code behavior. You still have to follow all the basic rules related to variables in C to avoid compiler specific problems.

Who is online

Users browsing this forum: No registered users and 288 guests