Skip to content

Commit

Permalink
Ensure that no buffer overflow can occur by limiting the number of po…
Browse files Browse the repository at this point in the history
…st-comma digits
  • Loading branch information
aentinger committed Dec 9, 2020
1 parent 952d776 commit 3c76ef2
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions api/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/

#include "String.h"
#include "Common.h"
#include "itoa.h"
#include "deprecated-avr-comp/avr/dtostrf.h"

Expand Down Expand Up @@ -123,6 +124,7 @@ 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[FLOAT_BUF_SIZE];
decimalPlaces = min(decimalPlaces, FLT_MAX_DECIMAL_PLACES);
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
}

Expand All @@ -131,6 +133,7 @@ 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[DOUBLE_BUF_SIZE];
decimalPlaces = min(decimalPlaces, DBL_MAX_DECIMAL_PLACES);
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
}

Expand Down

0 comments on commit 3c76ef2

Please sign in to comment.