Weird sscanf behavior with float variables

Hi all,

Since sprintf wasn’t writting float variables for me, I found in several places that this feature has to be enabled through linker arguments. For example as it said in this link

Enabling by “-lc -u _printf_float” and “-lc -u _scanf_float”, I have sprintf working correctly with floats, while sscanf get stucked in some kind of infinite loop.

I tried to use newlib by removing “–specs=nano.specs” and sprintf works nice too. On the other hand, sscanf don’t fall in that infinite loop but I saw that string I passed as argument has been written in the proccess causing a corruption of the data.

If the string buffer is small enough it keeps writing garbage out, which is more catastrophic.

I’m using SDK_2.9.0_MIMX8QX5xxxFZ, Build Date: 2021-10-01. I think this is te latest.

Hi @Eduardo!

From your steps, seems like you are on the right track.

But it is hard to try to help you because your description does not have much information.

Also, using sscanf is pretty easy to miss some detail (e.g. a misplaced % or a variable of the wrong type) and end up screwing the memory.

Best regards,

Hello again,

I figured out what was happening now, when I have tried to write some code to explain this issue for you.

Problem was I was using these instructions from a FreeRTOS task context that apparently did not have a large enough stack reserved.

This is the code I wrote in an empty project, right at the beginning of main:

char str_buffer[32];
float value;

memset(str_buffer, 0, sizeof(str_buffer));
value = 1.2345;

sprintf(str_buffer, "%f", value);
sscanf(str_buffer, "%f", &value);

It worked fine today.

But confused I tried again in the code in where I was having the issue. By default the examples used to reserve by default “configMINIMAL_STACK_SIZE + 100” when creating the task.

So if you try this code inside the task, you can view how “str_buffer” data is overwritten with random characters after executing sscanf.

On the other hand, if you create the task with a little more memory, the code works correctly.

Hi @Eduardo !

Good to know that you solved it :smiley:

And thanks for sharing your solution with us!

Also, as your previous answer is the solution to your problem, please mark it as a solution :slight_smile:

Best regards,