Fixes String(float) issue with Stack Smashing (#6138)

Fixes #5873
This commit is contained in:
Rodrigo Garcia
2022-01-17 09:44:49 -03:00
committed by GitHub
parent caef4006af
commit 841599c248
4 changed files with 23 additions and 11 deletions

View File

@ -112,16 +112,28 @@ String::String(unsigned long value, unsigned char base) {
*this = buf;
}
String::String(float value, unsigned char decimalPlaces) {
String::String(float value, unsigned int decimalPlaces) {
init();
char buf[33];
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
char *buf = (char*)malloc(decimalPlaces + 42);
if (buf) {
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
free(buf);
} else {
*this = "nan";
log_e("No enought memory for the operation.");
}
}
String::String(double value, unsigned char decimalPlaces) {
String::String(double value, unsigned int decimalPlaces) {
init();
char buf[33];
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
char *buf = (char*)malloc(decimalPlaces + 312);
if (buf) {
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
free(buf);
} else {
*this = "nan";
log_e("No enought memory for the operation.");
}
}
String::~String() {