Skip to content

Commit 841599c

Browse files
authored
Fixes String(float) issue with Stack Smashing (espressif#6138)
Fixes espressif#5873
1 parent caef400 commit 841599c

File tree

4 files changed

+23
-11
lines changed

4 files changed

+23
-11
lines changed

Diff for: cores/esp32/WString.cpp

+18-6
Original file line numberDiff line numberDiff line change
@@ -112,16 +112,28 @@ String::String(unsigned long value, unsigned char base) {
112112
*this = buf;
113113
}
114114

115-
String::String(float value, unsigned char decimalPlaces) {
115+
String::String(float value, unsigned int decimalPlaces) {
116116
init();
117-
char buf[33];
118-
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
117+
char *buf = (char*)malloc(decimalPlaces + 42);
118+
if (buf) {
119+
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
120+
free(buf);
121+
} else {
122+
*this = "nan";
123+
log_e("No enought memory for the operation.");
124+
}
119125
}
120126

121-
String::String(double value, unsigned char decimalPlaces) {
127+
String::String(double value, unsigned int decimalPlaces) {
122128
init();
123-
char buf[33];
124-
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
129+
char *buf = (char*)malloc(decimalPlaces + 312);
130+
if (buf) {
131+
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
132+
free(buf);
133+
} else {
134+
*this = "nan";
135+
log_e("No enought memory for the operation.");
136+
}
125137
}
126138

127139
String::~String() {

Diff for: cores/esp32/WString.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ class String {
7171
explicit String(unsigned int, unsigned char base = 10);
7272
explicit String(long, unsigned char base = 10);
7373
explicit String(unsigned long, unsigned char base = 10);
74-
explicit String(float, unsigned char decimalPlaces = 2);
75-
explicit String(double, unsigned char decimalPlaces = 2);
74+
explicit String(float, unsigned int decimalPlaces = 2);
75+
explicit String(double, unsigned int decimalPlaces = 2);
7676
~String(void);
7777

7878
// memory management

Diff for: cores/esp32/stdlib_noniso.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ char* ultoa(unsigned long value, char* result, int base) {
8888
return result;
8989
}
9090

91-
char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
91+
char * dtostrf(double number, signed int width, unsigned int prec, char *s) {
9292
bool negative = false;
9393

9494
if (isnan(number)) {
@@ -117,7 +117,7 @@ char * dtostrf(double number, signed char width, unsigned char prec, char *s) {
117117
// Round correctly so that print(1.999, 2) prints as "2.00"
118118
// I optimized out most of the divisions
119119
double rounding = 2.0;
120-
for (uint8_t i = 0; i < prec; ++i)
120+
for (uint32_t i = 0; i < prec; ++i)
121121
rounding *= 10.0;
122122
rounding = 1.0 / rounding;
123123

Diff for: cores/esp32/stdlib_noniso.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ char* utoa (unsigned int val, char *s, int radix);
3939

4040
char* ultoa (unsigned long val, char *s, int radix);
4141

42-
char* dtostrf (double val, signed char width, unsigned char prec, char *s);
42+
char* dtostrf (double val, signed int width, unsigned int prec, char *s);
4343

4444
#ifdef __cplusplus
4545
} // extern "C"

0 commit comments

Comments
 (0)