Skip to content

Commit

Permalink
Increase buffer size in order to avoid buffer overflow when using lar…
Browse files Browse the repository at this point in the history
…ge floating point numbers
  • Loading branch information
aentinger committed Dec 9, 2020
1 parent f3cfa2f commit 952d776
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
19 changes: 15 additions & 4 deletions api/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@
#include "itoa.h"
#include "deprecated-avr-comp/avr/dtostrf.h"

#include <float.h>

namespace arduino {

/*********************************************/
/* Constructors */
/* Static Member Initialisation */
/*********************************************/

namespace arduino {
size_t const String::FLT_MAX_DECIMAL_PLACES;
size_t const String::DBL_MAX_DECIMAL_PLACES;

/*********************************************/
/* Constructors */
/*********************************************/

String::String(const char *cstr)
{
Expand Down Expand Up @@ -111,15 +120,17 @@ String::String(unsigned long value, unsigned char base)

String::String(float value, unsigned char decimalPlaces)
{
static size_t const FLOAT_BUF_SIZE = FLT_MAX_10_EXP + FLT_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */;
init();
char buf[33];
char buf[FLOAT_BUF_SIZE];
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
}

String::String(double value, unsigned char decimalPlaces)
{
static size_t const DOUBLE_BUF_SIZE = DBL_MAX_10_EXP + DBL_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */;
init();
char buf[33];
char buf[DOUBLE_BUF_SIZE];
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
}

Expand Down
3 changes: 3 additions & 0 deletions api/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ class String
typedef void (String::*StringIfHelperType)() const;
void StringIfHelper() const {}

static size_t const FLT_MAX_DECIMAL_PLACES = 10;
static size_t const DBL_MAX_DECIMAL_PLACES = FLT_MAX_DECIMAL_PLACES;

public:
// constructors
// creates a copy of the initial value.
Expand Down

0 comments on commit 952d776

Please sign in to comment.