Hi,
I have one question about Cortexm3 from smartfusion device from actel. Could you pls help me? I add follwing codes into retarget.c and then add retarget.c into source code group and find : printf function works.however scanf function doesn't. anybody knows why?
int fputc(int ch, FILE *f) { return ((int)ITM_SendChar(ch)); } int fgetc(FILE *f) { int i=0; i=ITM_ReceiveChar(); if((-1)!=i) return ((int)ITM_SendChar((uint32_t)i)); else return (0); }
printf("hello world\n\r"); // when executed, string displayed in debug(printf)window scanf("%s",str); // when executed, hard fault occurs
So how do you ensure that the data returned by scanf doesn't exceed 9 characters (or whatever your array size happens to be) ?
Surely, this is a fundamental flaw with using scanf ?!
Try searching the forum for "scanf"...
hi, it is just a test since it is myself who is clicking the keyboard.
I don't know when the PC will point to hardfault handler once defined scanf function is executed.
I had searched "scanf" in this forum and find no similar thread.
Looking forward to help.
thanks.
Fisher
You missed the point!
The point is that scanf is, generally, a Bad Thing to be using in such a situation - there are plenty of threads that illustriate this point.
Did you ever implement success scanf function in Cortexm3 in smartFusion? I guess you didn't. otherwise you will find that those scanf examples found in another thereads don't work in Cortexm3 within smartFusion!
Well known dangerous functions that stands for a large number of buffer overflows in this world includes: scanf() gets() sprintf() strcpy()
Note that Keil doesn't have the standard gets() but have duplicated the fix from fgets(), i.e. added a size parameter telling the max number of characters that may be handled.
Why not?
Is it really anything specifically to do with Cortex-M3 and/or SmartFusion?
Could you please post your scanf() here? I know how to retarget to real UART connected to PC via Hypertermial using retarget.c. However I don't know how to retarget to debug(printf) windows via ITM.
"%9s"
I asked how he did ensure it - not how he could (or should) have ensured it...
And we did get an answer too, containing:
volatile unsigned char str='\0';
and
scanf("%s\n",&str);
The huge problem with scanf() is that you may specify a width attribute. But no one is making sure you do. And since people tends to test with good data, huge amounts of programs have been released with sprintf() and no width attribute set. But then again, the world is full of programs that calls scanf() without checking the return value, happilly doing their magic even without all parameters.