Support for float (%f) in chprintf

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: Support for float (%f) in chprintf

Post by utzig »

I did a fixed format implementation of printing (calling chprintf) on my main.c file. I made it just to test variadic functions and va_list implementations. In my tests passing any number of floats seems to work. Of course I have to know before hand what I passed 'cause there's no parsing of format.

I don't know yet why it works in this context but maybe this can give some ideas to someone.

And the end I call chprintf with 3 floats and it gives an error.

Code: Select all

void print_values(char *fmt, ...)
{
  va_list ap;
  float f;
  long l;

  va_start(ap, fmt);

  f = (float) va_arg(ap, double);
  chprintf((BaseSequentialStream *) &SD2, "%f\r\n", f);
  l = va_arg(ap, long);
  chprintf((BaseSequentialStream *) &SD2, "%d\r\n", l);
  f = (float) va_arg(ap, double);
  chprintf((BaseSequentialStream *) &SD2, "%f\r\n", f);
  l = va_arg(ap, long);
  chprintf((BaseSequentialStream *) &SD2, "%d\r\n", l);
  f = (float) va_arg(ap, double);
  chprintf((BaseSequentialStream *) &SD2, "%f\r\n", f);
  l = va_arg(ap, long);
  chprintf((BaseSequentialStream *) &SD2, "%d\r\n", l);
  f = (float) va_arg(ap, double);
  chprintf((BaseSequentialStream *) &SD2, "%f\r\n", f);
  f = (float) va_arg(ap, double);
  chprintf((BaseSequentialStream *) &SD2, "%f\r\n", f);
  f = (float) va_arg(ap, double);
  chprintf((BaseSequentialStream *) &SD2, "%f\r\n", f);
  f = (float) va_arg(ap, double);

  va_end(ap);
}

int main(void)
{
  ...
  sdStart(&SD2, &ser_config);
  print_values("", 1.0, 1, 2.0, 2, 3.0, 3, 4.0, 5.0, 6.0);
  chprintf((BaseSequentialStream *) &SD2, "%f %f %f\r\n", 1.0, 2.0, 3.0);
  ...
}


Output:

Code: Select all

1.00000
1
2.00000
2
3.00000
3
4.00000
5.00000
6.00000
1.00000 0.00000 0.00000


Fabio Utzig
MartinP
Posts: 35
Joined: Mon Apr 02, 2012 11:54 am

Re: Support for float (%f) in chprintf

Post by MartinP »

I took the file copied the content to my visual studio and debugged it:

There is a buffer overrun for

Code: Select all

tmpbuf[MAX_FILLER + 1]

the function long_to_string_with_divisor() is assuming pointe p is at the beginning of this buffer as it is doing the following:

Code: Select all

q = p + MAX_FILLER;


this is good only if it is called only once and then the pointer p needs to be reset to the beginning.

but the function ftoa() is calling it twice:

Code: Select all

l = num;
  p = long_to_string_with_divisor(p, l, 10, 0);
  *p++ = '.';
  l = (num - l) * precision;
  return long_to_string_with_divisor(p, l, 10, precision / 10);

therefore depending on the number the bufferoverflow ins more or less severe and destroying the stack

a simple solution (but not optimal in terms of memory consumption) would be:

Code: Select all

#if CHPRINTF_USE_FLOAT
   char tmpbuf[2*MAX_FILLER + 1];
#else
   char tmpbuf[MAX_FILLER + 1];
#endif


it is of course better to fix the pointer arithmetics

unfortunately I have no time today to try it out on chibios

Martin
utzig
Posts: 359
Joined: Sat Jan 07, 2012 6:22 pm
Has thanked: 1 time
Been thanked: 20 times

Re: Support for float (%f) in chprintf

Post by utzig »

Yep, that's it. Very good catch Martin!
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Support for float (%f) in chprintf

Post by Giovanni »

I committed the above fix, it is good enough until somebody comes up with a better implementation.

Giovanni
nikhil
Posts: 40
Joined: Thu Jan 03, 2013 8:46 pm

Re: Support for float (%f) in chprintf

Post by nikhil »

Hello,

Is this patch implemented in current release of chibios? if not can anyone explain me how to use this patch?
User avatar
Giovanni
Site Admin
Posts: 14891
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: Support for float (%f) in chprintf

Post by Giovanni »

Hi,

It is already included in ChibiOS 2.5.1 but not in 2.4.x.

Giovanni
Post Reply