Skip to content

Fixes String(float) issue with Stack Smashing #6138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions cores/esp32/WString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions cores/esp32/WString.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ class String {
explicit String(unsigned int, unsigned char base = 10);
explicit String(long, unsigned char base = 10);
explicit String(unsigned long, unsigned char base = 10);
explicit String(float, unsigned char decimalPlaces = 2);
explicit String(double, unsigned char decimalPlaces = 2);
explicit String(float, unsigned int decimalPlaces = 2);
explicit String(double, unsigned int decimalPlaces = 2);
~String(void);

// memory management
Expand Down
4 changes: 2 additions & 2 deletions cores/esp32/stdlib_noniso.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ char* ultoa(unsigned long value, char* result, int base) {
return result;
}

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

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

Expand Down
2 changes: 1 addition & 1 deletion cores/esp32/stdlib_noniso.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ char* utoa (unsigned int val, char *s, int radix);

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

char* dtostrf (double val, signed char width, unsigned char prec, char *s);
char* dtostrf (double val, signed int width, unsigned int prec, char *s);

#ifdef __cplusplus
} // extern "C"
Expand Down