Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 952d776

Browse filesBrowse files
committed
Increase buffer size in order to avoid buffer overflow when using large floating point numbers
1 parent f3cfa2f commit 952d776
Copy full SHA for 952d776

File tree

2 files changed

+18
-4
lines changed
Filter options

2 files changed

+18
-4
lines changed

‎api/String.cpp

Copy file name to clipboardExpand all lines: api/String.cpp
+15-4Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,20 @@
2323
#include "itoa.h"
2424
#include "deprecated-avr-comp/avr/dtostrf.h"
2525

26+
#include <float.h>
27+
28+
namespace arduino {
29+
2630
/*********************************************/
27-
/* Constructors */
31+
/* Static Member Initialisation */
2832
/*********************************************/
2933

30-
namespace arduino {
34+
size_t const String::FLT_MAX_DECIMAL_PLACES;
35+
size_t const String::DBL_MAX_DECIMAL_PLACES;
36+
37+
/*********************************************/
38+
/* Constructors */
39+
/*********************************************/
3140

3241
String::String(const char *cstr)
3342
{
@@ -111,15 +120,17 @@ String::String(unsigned long value, unsigned char base)
111120

112121
String::String(float value, unsigned char decimalPlaces)
113122
{
123+
static size_t const FLOAT_BUF_SIZE = FLT_MAX_10_EXP + FLT_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */;
114124
init();
115-
char buf[33];
125+
char buf[FLOAT_BUF_SIZE];
116126
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
117127
}
118128

119129
String::String(double value, unsigned char decimalPlaces)
120130
{
131+
static size_t const DOUBLE_BUF_SIZE = DBL_MAX_10_EXP + DBL_MAX_DECIMAL_PLACES + 1 /* '-' */ + 1 /* '.' */ + 1 /* '\0' */;
121132
init();
122-
char buf[33];
133+
char buf[DOUBLE_BUF_SIZE];
123134
*this = dtostrf(value, (decimalPlaces + 2), decimalPlaces, buf);
124135
}
125136

‎api/String.h

Copy file name to clipboardExpand all lines: api/String.h
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ class String
5858
typedef void (String::*StringIfHelperType)() const;
5959
void StringIfHelper() const {}
6060

61+
static size_t const FLT_MAX_DECIMAL_PLACES = 10;
62+
static size_t const DBL_MAX_DECIMAL_PLACES = FLT_MAX_DECIMAL_PLACES;
63+
6164
public:
6265
// constructors
6366
// creates a copy of the initial value.

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.