-->
Page 2 of 2

Re: strncpy() Failing

PostPosted: Tue Oct 20, 2015 5:25 am
by snowdenator
eduperez wrote:I am afraid there is a bug (or two) in you code:

You have declared CURRENT_SSID_CHAR as a pointer, but you have not initialized it, or allocated any memory for the data you want to store there. Thus, your call to os_strncpy is writing data somewhere within your device's memory where you are not expected to write any data.

Now, remember that os_strncpy does not zero-terminate the destination string when it has to truncate the input. Thus, when the SSID name is longer than 15 characters, CURRENT_SSID_CHAR becomes an unterminated string, and the call to Serial.println will probably send some unwanted characters to the serial.

Hope this helps!


I'll have to try this after work, but calling malloc() with 16 bytes, 15 for the SSID and then one for the terminator, this should work?

EG:
Code: Select allchar* CURRENT_SSID_CHAR;
CURRENT_SSID_CHAR = (char*) malloc(16);


I'll have to look into C char arrays more, I have never properly looked into them...

Thanks!

Re: strncpy() Failing

PostPosted: Tue Oct 20, 2015 6:02 am
by eduperez
snowdenator wrote:I'll have to try this after work, but calling malloc() with 16 bytes, 15 for the SSID and then one for the terminator, this should work?

EG:
Code: Select allchar* CURRENT_SSID_CHAR;
CURRENT_SSID_CHAR = (char*) malloc(16);


I'll have to look into C char arrays more, I have never properly looked into them...

Thanks!


That seems much better! Or you could just use an array, and allocate it in the declaration:
Code: Select allchar CURRENT_SSID_CHAR[16];

Now "CURRENT_SSID_CHAR" is a pointer, and can be used exactly as you have been using it up to now; next you have to make sure it is properly terminated after the call to os_strncpy:
Code: Select allCURRENT_SSID_CHAR[15] = '\0';

Re: strncpy() Failing

PostPosted: Tue Oct 20, 2015 7:44 am
by snowdenator
eduperez wrote:That seems much better! Or you could just use an array, and allocate it in the declaration:
Code: Select allchar CURRENT_SSID_CHAR[16];

Now "CURRENT_SSID_CHAR" is a pointer, and can be used exactly as you have been using it up to now; next you have to make sure it is properly terminated after the call to os_strncpy:
Code: Select allCURRENT_SSID_CHAR[15] = '\0';


After testing this code over my dinner break, it has solved my problem.

Thank you very much for your help!

Re: strncpy() Failing

PostPosted: Tue Oct 20, 2015 8:05 am
by eduperez
snowdenator wrote:
eduperez wrote:That seems much better! Or you could just use an array, and allocate it in the declaration:
Code: Select allchar CURRENT_SSID_CHAR[16];

Now "CURRENT_SSID_CHAR" is a pointer, and can be used exactly as you have been using it up to now; next you have to make sure it is properly terminated after the call to os_strncpy:
Code: Select allCURRENT_SSID_CHAR[15] = '\0';


After testing this code over my dinner break, it has solved my problem.

Thank you very much for your help!


Glad to know it worked!