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 754873c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions api/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "itoa.h"
#include "deprecated-avr-comp/avr/dtostrf.h"

#include <float.h>

/*********************************************/
/* Constructors */
/*********************************************/
Expand Down Expand Up @@ -111,15 +113,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 754873c

Please sign in to comment.