2024-02-15 04:34 AM - last edited on 2024-02-15 08:27 AM by SofLit
Hi, I am having problem with string copy strncpy(). I am getting error message "Cannot access memory at address 0x31203a6e"
static char *DataBuffer[30] = {0};
for(char *str =strtok((char*)recdata, "\n\n"); str; str = strtok(NULL, "\n\n"))
{
strncpy(DataBuffer, str, strlen(str)+2);
strcat(DataBuffer,"\n");
strncpy(DataBuffer, str, strlen(str)+2);
}
Is it memory addressing access faults. Is the chip failing memory handling. How do I debug it to find out.
2024-02-15 04:53 AM
The pointer starts or becomes invalid as not in usable memory
Suggest you instrument so you can watch where it fails.
strlen + 2 ?? Why?
Are these properly NUL terminated strings?
2024-02-15 06:33 AM
I have found the cause basically I have a else if condition,
else if(command)
{
break;
}
its in the while loop when the command is 1 it breaks up the loop from the while loop. and it will come out from the while loop. But when I debug command showing as -1 and goes inside the if condition and breaks from the while loop. Why is that?. if condition is to check if it is true not as -1.
2024-02-15 06:38 AM - edited 2024-02-15 06:39 AM
> static char *DataBuffer[30] = {0};
This is an array of pointers to char. Probably not what you want. Should be producing errors or warning upon trying to compile. You probably want and array of chars like this:
static char DataBuffer[30] = {0};
> strlen(str)+2
Calculate this before you use it and ensure you're not overstepping the bounds of DataBuffer.
2024-02-15 07:36 AM
Wouldn't any non-zero value for command be expected to enter that condition, if it gets there, and break?
2024-02-15 01:19 PM
I did debug and I found that 'command' value change from 0 to -1 even though I assigned at the begining as 0 for command. Somehow it changes its value for some reason. I even tried initializing static int command to volatile int command. It didnt make difference. I dont understand why its value changed to -1.
2024-02-15 02:14 PM
I've got zero visibility into anything you're doing.
The implied cause would be you're breaching some other memory buffer or the stack.
Behaviour change with optimization levels?
Got any static analysis tools, or a colleague who can write and review code?
2024-02-15 02:34 PM
Hi,
Thank you for the reply. I am using STM32CUBIDE to debug. Are there static analysis tools that I can use with the STM32CUBEIDE. Can you recomend. Why the behaviour change optimization level? is there way to check the optimization levels
2024-02-15 03:06 PM
>>Why the behaviour change optimization level? is there way to check the optimization levels
It was posed as a question, do you see differences? Typically compilers will provide an option/setting as to what level to use. Look in the options and related tabs. Increase warning levels, and perhaps review what's currently flagging.
Do a basic code walk. Have a colleague do a code walk. Run LINT or MISRA tools against your code, enable them on your source code control if you can to check commits for rules violations.
Put checks in your code for the state of command, and bisect the problem to identify where you first see it occurring.
Your string code looks problematic, strtok() also isn't thread safe, perhaps use strsep() instead.
2024-02-24 09:45 PM
Thank you for the reply. It was very useful
I tried to use strsep() but I am getting compiler warning stating "passing argument 1 of strsep from incompatible pointer type" here is the code,
for(char *str =strsep(&recdata, "\n\n"); str; str = strsep(NULL, "\n\n"))
{
}
What am I doing wrong here..