From 6ce8fe3c0fa7725e77d6ea61557bcec65d6cdb34 Mon Sep 17 00:00:00 2001 From: Beaky2000 Date: Sun, 17 Apr 2016 22:09:49 +0200 Subject: [PATCH] Fixed a bug which caused printing of floats larger than 100 in the CLI to crash --- src/main/common/typeconversion.c | 5 +++++ src/main/common/typeconversion.h | 10 ++++++++++ src/main/io/serial_cli.c | 2 +- 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/main/common/typeconversion.c b/src/main/common/typeconversion.c index 4e3f8b4c2e..3a3730fd56 100644 --- a/src/main/common/typeconversion.c +++ b/src/main/common/typeconversion.c @@ -144,6 +144,11 @@ char *itoa(int i, char *a, int base) #endif +/* Note: the floatString must be at least 13 bytes long to cover all cases. + * This includes up to 10 digits (32 bit ints can hold numbers up to 10 + * digits long) + the decimal point + negative sign or space + null + * terminator. + */ char *ftoa(float x, char *floatString) { int32_t value; diff --git a/src/main/common/typeconversion.h b/src/main/common/typeconversion.h index 62b437875b..f2811d65ba 100644 --- a/src/main/common/typeconversion.h +++ b/src/main/common/typeconversion.h @@ -21,7 +21,17 @@ void li2a(long num, char *bf); void ui2a(unsigned int num, unsigned int base, int uc, char *bf); void i2a(int num, char *bf); char a2i(char ch, const char **src, int base, int *nump); + +/* Simple conversion of a float to a string. Will display completely + * inaccurate results for floats larger than about 2.15E6 (2^31 / 1000) + * (same thing for negative values < -2.15E6). + * Will always display 3 decimals, so anything smaller than 1E-3 will + * not be displayed. + * The floatString will be filled in with the result and will be + * returned. It must be at least 13 bytes in length to cover all cases! + */ char *ftoa(float x, char *floatString); + float fastA2F(const char *p); #ifndef HAVE_ITOA_FUNCTION diff --git a/src/main/io/serial_cli.c b/src/main/io/serial_cli.c index 7106a99fb7..8f009328f4 100644 --- a/src/main/io/serial_cli.c +++ b/src/main/io/serial_cli.c @@ -2446,7 +2446,7 @@ static void cliWrite(uint8_t ch) static void cliPrintVar(const clivalue_t *var, uint32_t full) { int32_t value = 0; - char buf[8]; + char buf[13]; void *ptr = var->ptr; if ((var->type & VALUE_SECTION_MASK) == PROFILE_VALUE) {