Skip to content

Commit 3c76ef2

Browse files
committed
Ensure that no buffer overflow can occur by limiting the number of post-comma digits
1 parent 952d776 commit 3c76ef2

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

Diff for: api/String.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
*/
2121

2222
#include "String.h"
23+
#include "Common.h"
2324
#include "itoa.h"
2425
#include "deprecated-avr-comp/avr/dtostrf.h"
2526

@@ -123,6 +124,7 @@ String::String(float value, unsigned char decimalPlaces)
123124
static size_t const FLOAT_BUF_SIZE = FLT_MAX_10_EXP + FLT_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */;
124125
init();
125126
char buf[FLOAT_BUF_SIZE];
127+
decimalPlaces = min(decimalPlaces, FLT_MAX_DECIMAL_PLACES);
126128
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
127129
}
128130

@@ -131,6 +133,7 @@ String::String(double value, unsigned char decimalPlaces)
131133
static size_t const DOUBLE_BUF_SIZE = DBL_MAX_10_EXP + DBL_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */;
132134
init();
133135
char buf[DOUBLE_BUF_SIZE];
136+
decimalPlaces = min(decimalPlaces, DBL_MAX_DECIMAL_PLACES);
134137
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
135138
}
136139

0 commit comments

Comments
 (0)