Infrastructure:
K60 , IAR emmebbed work bench,MQX4.0
Running securitytelnet program , on the shell i have a small code for the application
int_32 shell_startsource(int_32 argc, char_ptr argv[])
{
boolean print_usage, shorthelp = FALSE;
int_32 return_code = SHELL_EXIT_SUCCESS;
char *str;
const char *str1;
print_usage = Shell_check_help_request(argc, argv, &shorthelp );
if (!print_usage)
{
if (argc !=1 )
{
printf("Error, invalid number of parameters\n");
return_code = SHELL_EXIT_ERROR;
print_usage=TRUE;
}
else
{
str=":o1";
str1="/r";
str=strcat(str,str1);
openserial();
serialwrite(str);
closeserial();
}
}
if (print_usage)
{
print_usage_simple (shorthelp, argv[0], "sourcestatus");
}
return return_code;
}
void openserial(void)
{
uint_32 flags=0;
serial_fd = fopen(BSP_DEFAULT_IO_CHANNEL1, 0);
ioctl(serial_fd, IO_IOCTL_SERIAL_SET_FLAGS, &flags);
}
void serialwrite(char * data)
{
_io_serial_polled_write(serial_fd,data,5);
}
void closeserial(void)
{
fclose(serial_fd);
}
intially telnet server will created and which gives shell login and in that i have shell_startsource command .
The code is running fine till the strcat function , when perform the strcat function , the telent task is going to unhand led interrupt error . inside the strcat when it tries to execute *dp++=*sp++; line it is go it this error and the step is not breaking .
the code of strcat as follows:
char *
strcat (char *dest, const char *src)
{
char *dp;
char *sp = (char *)src;
if ((dest != NULL) && (src != NULL))
{
dp = &dest[strlen(dest)];
while (*sp != '\0')
{
*dp++ = *sp++;
}
*dp = '\0';
}
return dest;
}
So please suggest a way to debug this issue and what is causing the telnet task to go unhandled interrupt error. Used the install _int_unexpected_isr() before the strcat function but do not excat way use this function to debug.so please suggest
snapshot attached:yes
Please use C++ syntax in FULL EDITOR to simplify view of your code. Thank you Message was edited by: Martin Kojtal
Solved! Go to Solution.
Hello vallinath s,
let's find out why did your application invoked unexpected interrupt,
char *str;
str = "text";
const char *str2;
str2 = "text_only";
What is the difference in those 2? Only in constantness. I assume you wanted to satisfy compiler because strcat is declared as the function expecting a destination as char* and a source as const char*. They both point to a read only memory (a string literal). They are not immutable ! Thus strcat was overwriting memory in your application.
What you need it's writeable memory and sufficient space as well. 2 options available: use stack frame or heap (malloc).
char str[255] = "text";
This allocates 255 bytes (explicitly copied to a mutable location (stack) in its declaration and fills first 5 bytes with value text + the terminating character. Be warned if you do not define size of an array, it allocates exactly sufficient bytes for holding the declared string plus one more for the terminating character.
Regards,
c0170
Hello vallinath s,
let's find out why did your application invoked unexpected interrupt,
char *str;
str = "text";
const char *str2;
str2 = "text_only";
What is the difference in those 2? Only in constantness. I assume you wanted to satisfy compiler because strcat is declared as the function expecting a destination as char* and a source as const char*. They both point to a read only memory (a string literal). They are not immutable ! Thus strcat was overwriting memory in your application.
What you need it's writeable memory and sufficient space as well. 2 options available: use stack frame or heap (malloc).
char str[255] = "text";
This allocates 255 bytes (explicitly copied to a mutable location (stack) in its declaration and fills first 5 bytes with value text + the terminating character. Be warned if you do not define size of an array, it allocates exactly sufficient bytes for holding the declared string plus one more for the terminating character.
Regards,
c0170
Thanks Martin , used malloc to allocate memory for the string array and is working fine ,
thanks again
Try to allocate more memory for the str. Replace
char *str;
by
char str[MAX_LEN];
MAX_LEN being maximum number of characters you allow for the str.