Skip to content

Commit

Permalink
Fix the memory leaks in internal JSON parser and the token pre-refres…
Browse files Browse the repository at this point in the history
…hment issue.
  • Loading branch information
mobizt committed Apr 4, 2021
1 parent d7bca1e commit 754c3d3
Show file tree
Hide file tree
Showing 13 changed files with 157 additions and 199 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.4390794.svg)](https://doi.org/10.5281/zenodo.4390794)


Google's Firebase Realtime Database Arduino Library for ESP8266 v 3.1.8
Google's Firebase Realtime Database Arduino Library for ESP8266 v 3.1.9


This library supports ESP8266 MCU from Espressif. The following are platforms in which libraries are also available.
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name=Firebase ESP8266 Client

version=3.1.8
version=3.1.9

author=Mobizt

Expand Down
47 changes: 3 additions & 44 deletions src/FirebaseESP8266.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**
* Google's Firebase Realtime Database Arduino Library for ESP8266, version 3.1.9
*
* April 3, 2021
* April 4, 2021
*
* Updates:
* - Allows other Firebase calls inside the stream and multipath stream callback function.
* - Fix the memory leaks in internal JSON parser.
* - Fix the token pre-refreshment issue.
*
* This library provides ESP8266 to perform REST API by GET PUT, POST, PATCH, DELETE data from/to with Google's Firebase database using get, set, update
* and delete calls.
Expand Down Expand Up @@ -317,11 +318,6 @@ bool FirebaseESP8266::push(FirebaseData &fbdo, const String &path, const String
return RTDB.pushString(&fbdo, path.c_str(), stringValue);
}

bool FirebaseESP8266::push(FirebaseData &fbdo, const String &path, const StringSumHelper &stringValue)
{
return RTDB.pushString(&fbdo, path.c_str(), stringValue);
}

bool FirebaseESP8266::push(FirebaseData &fbdo, const String &path, const char *stringValue, float priority)
{
return RTDB.pushString(&fbdo, path.c_str(), stringValue, priority);
Expand All @@ -332,11 +328,6 @@ bool FirebaseESP8266::push(FirebaseData &fbdo, const String &path, const String
return RTDB.pushString(&fbdo, path.c_str(), stringValue, priority);
}

bool FirebaseESP8266::push(FirebaseData &fbdo, const String &path, const StringSumHelper &stringValue, float priority)
{
return RTDB.pushString(&fbdo, path.c_str(), stringValue, priority);
}

bool FirebaseESP8266::push(FirebaseData &fbdo, const String &path, FirebaseJson &json)
{
return RTDB.pushJSON(&fbdo, path.c_str(), &json);
Expand Down Expand Up @@ -390,8 +381,6 @@ bool FirebaseESP8266::push(FirebaseData &fbdo, const String &path, T value)
return pushString(fbdo, path, value);
else if (std::is_same<T, const String &>::value)
return pushString(fbdo, path, value);
else if (std::is_same<T, const StringSumHelper &>::value)
return pushString(fbdo, path, value);
else if (std::is_same<T, FirebaseJson &>::value)
return pushJson(fbdo, path, value);
else if (std::is_same<T, FirebaseJsonArray &>::value)
Expand All @@ -418,8 +407,6 @@ bool FirebaseESP8266::push(FirebaseData &fbdo, const String &path, T value, floa
return pushString(fbdo, path, value, priority);
else if (std::is_same<T, const String &>::value)
return pushString(fbdo, path, value, priority);
else if (std::is_same<T, const StringSumHelper &>::value)
return pushString(fbdo, path, value, priority);
else if (std::is_same<T, FirebaseJson &>::value)
return pushJson(fbdo, path, value, priority);
else if (std::is_same<T, FirebaseJsonArray &>::value)
Expand Down Expand Up @@ -618,11 +605,6 @@ bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, const String &
return RTDB.setString(&fbdo, path.c_str(), stringValue);
}

bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, const StringSumHelper &stringValue)
{
return RTDB.setString(&fbdo, path.c_str(), stringValue);
}

bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, const char *stringValue, float priority)
{
return RTDB.setString(&fbdo, path.c_str(), stringValue, priority);
Expand All @@ -633,11 +615,6 @@ bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, const String &
return RTDB.setString(&fbdo, path.c_str(), stringValue, priority);
}

bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, const StringSumHelper &stringValue, float priority)
{
return RTDB.setString(&fbdo, path.c_str(), stringValue, priority);
}

bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, const char *stringValue, const String &ETag)
{
return RTDB.setString(&fbdo, path.c_str(), stringValue, ETag.c_str());
Expand All @@ -648,11 +625,6 @@ bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, const String &
return RTDB.setString(&fbdo, path.c_str(), stringValue, ETag.c_str());
}

bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, const StringSumHelper &stringValue, const String &ETag)
{
return RTDB.setString(&fbdo, path.c_str(), stringValue, ETag.c_str());
}

bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, const char *stringValue, float priority, const String &ETag)
{
return RTDB.setString(&fbdo, path.c_str(), stringValue, priority, ETag.c_str());
Expand All @@ -663,11 +635,6 @@ bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, const String &
return RTDB.setString(&fbdo, path.c_str(), stringValue, priority, ETag.c_str());
}

bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, const StringSumHelper &stringValue, float priority, const String &ETag)
{
return RTDB.setString(&fbdo, path.c_str(), stringValue, priority, ETag.c_str());
}

bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, FirebaseJson &json)
{
return RTDB.setJSON(&fbdo, path.c_str(), &json);
Expand Down Expand Up @@ -761,8 +728,6 @@ bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, T value)
return setString(fbdo, path, value);
else if (std::is_same<T, const String &>::value)
return setString(fbdo, path, value);
else if (std::is_same<T, const StringSumHelper &>::value)
return setString(fbdo, path, value);
else if (std::is_same<T, FirebaseJson &>::value)
return setJson(fbdo, path, value);
else if (std::is_same<T, FirebaseJson *>::value)
Expand Down Expand Up @@ -791,8 +756,6 @@ bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, T value, float
return setString(fbdo, path, value, priority);
else if (std::is_same<T, const String &>::value)
return setString(fbdo, path, value, priority);
else if (std::is_same<T, const StringSumHelper &>::value)
return setString(fbdo, path, value, priority);
else if (std::is_same<T, FirebaseJson &>::value)
return setJson(fbdo, path, value, priority);
else if (std::is_same<T, FirebaseJsonArray &>::value)
Expand All @@ -819,8 +782,6 @@ bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, T value, const
return setString(fbdo, path, value, ETag);
else if (std::is_same<T, const String &>::value)
return setString(fbdo, path, value, ETag);
else if (std::is_same<T, const StringSumHelper &>::value)
return setString(fbdo, path, value, ETag);
else if (std::is_same<T, FirebaseJson &>::value)
return setJson(fbdo, path, value, ETag);
else if (std::is_same<T, FirebaseJsonArray &>::value)
Expand All @@ -847,8 +808,6 @@ bool FirebaseESP8266::set(FirebaseData &fbdo, const String &path, T value, float
return setString(fbdo, path, value, priority, ETag);
else if (std::is_same<T, const String &>::value)
return setString(fbdo, path, value, priority, ETag);
else if (std::is_same<T, const StringSumHelper &>::value)
return setString(fbdo, path, value, priority, ETag);
else if (std::is_same<T, FirebaseJson &>::value)
return setJson(fbdo, path, value, priority, ETag);
else if (std::is_same<T, FirebaseJsonArray &>::value)
Expand Down
19 changes: 4 additions & 15 deletions src/FirebaseESP8266.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
/**
* Google's Firebase Realtime Database Arduino Library for ESP8266, version 3.1.8
* Google's Firebase Realtime Database Arduino Library for ESP8266, version 3.1.9
*
* April 3, 2021
* April 4, 2021
*
* Updates:
* - Allows other Firebase calls inside the stream and multipath stream callback function.
* - Fix the memory leaks in internal JSON parser.
* - Fix the token pre-refreshment issue.
*
* This library provides ESP8266 to perform REST API by GET PUT, POST, PATCH, DELETE data from/to with Google's Firebase database using get, set, update
* and delete calls.
Expand Down Expand Up @@ -382,8 +383,6 @@ class FirebaseESP8266

bool push(FirebaseData &fbdo, const String &path, const String &stringValue);

bool push(FirebaseData &fbdo, const String &path, const StringSumHelper &stringValue);

/** Append new string (text) and the virtual child ".priority" to the defined database path.
*/
bool pushString(FirebaseData &fbdo, const String &path, const String &stringValue, float priority);
Expand All @@ -392,8 +391,6 @@ class FirebaseESP8266

bool push(FirebaseData &fbdo, const String &path, const String &stringValue, float priority);

bool push(FirebaseData &fbdo, const String &path, const StringSumHelper &stringValue, float priority);

/** Append new child nodes key and value (using FirebaseJson object) to the defined database path.
*
* @param fbdo Firebase Data Object to hold data and instance.
Expand Down Expand Up @@ -704,8 +701,6 @@ class FirebaseESP8266

bool set(FirebaseData &fbdo, const String &path, const String &stringValue);

bool set(FirebaseData &fbdo, const String &path, const StringSumHelper &stringValue);

/** Set string data and virtual child ".priority" at the defined database path.
*/
bool setString(FirebaseData &fbdo, const String &path, const String &stringValue, float priority);
Expand All @@ -714,8 +709,6 @@ class FirebaseESP8266

bool set(FirebaseData &fbdo, const String &path, const String &stringValue, float priority);

bool set(FirebaseData &fbdo, const String &path, const StringSumHelper &stringValue, float priority);

/** Set string (text) at the defined database path if defined database path's ETag matched the ETag value.
*
* @param fbdo Firebase Data Object to hold data and instance.
Expand All @@ -741,8 +734,6 @@ class FirebaseESP8266

bool set(FirebaseData &fbdo, const String &path, const String &stringValue, const String &ETag);

bool set(FirebaseData &fbdo, const String &path, const StringSumHelper &stringValue, const String &ETag);

/** Set string data and the virtual child ".priority" if defined ETag matches at the defined database path
*/
bool setString(FirebaseData &fbdo, const String &path, const String &stringValue, float priority, const String &ETag);
Expand All @@ -751,8 +742,6 @@ class FirebaseESP8266

bool set(FirebaseData &fbdo, const String &path, const String &stringValue, float priority, const String &ETag);

bool set(FirebaseData &fbdo, const String &path, const StringSumHelper &stringValue, float priority, const String &ETag);

/** Set the child node key and value (using FirebaseJson object) to the defined database path.
* This will replace any child nodes inside the defined path with node' s key
* and value defined in FirebaseJson object.
Expand Down
2 changes: 1 addition & 1 deletion src/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Firebase Realtime Database Arduino Library for ESP8266


Google's Firebase Realtime Database Arduino Library for ESP8266 v 3.1.8
Google's Firebase Realtime Database Arduino Library for ESP8266 v 3.1.9


## Global functions
Expand Down
101 changes: 99 additions & 2 deletions src/Utils.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Google's Firebase Util class, Utils.h version 1.0.9
* Google's Firebase Util class, Utils.h version 1.0.10
*
* This library supports Espressif ESP8266 and ESP32
*
* Created March 31, 2021
* Created April 4, 2021
*
* This work is a part of Firebase ESP Client library
* Copyright (c) 2021 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -569,6 +569,26 @@ class UtilsClass
return idx;
}

int readLine(WiFiClient *stream, std::string &buf)
{
int res = -1;
char c = 0;
int idx = 0;
while (stream->available())
{
res = stream->read();
if (res > -1)
{
c = (char)res;
buf += c;
idx++;
if (c == '\n')
return idx;
}
}
return idx;
}

int readChunkedData(WiFiClient *stream, char *out, int &chunkState, int &chunkedSize, int &dataLen, int bufLen)
{

Expand Down Expand Up @@ -651,6 +671,83 @@ class UtilsClass
return olen;
}

int readChunkedData(WiFiClient *stream, std::string &out, int &chunkState, int &chunkedSize, int &dataLen)
{

char *tmp = nullptr;
int p1 = 0;
int olen = 0;

if (chunkState == 0)
{
chunkState = 1;
chunkedSize = -1;
dataLen = 0;
std::string s;
int readLen = readLine(stream, s);
if (readLen)
{
tmp = strP(fb_esp_pgm_str_79);
p1 = strpos(s.c_str(), tmp, 0);
delS(tmp);
if (p1 == -1)
{
tmp = strP(fb_esp_pgm_str_21);
p1 = strpos(s.c_str(), tmp, 0);
delS(tmp);
}

if (p1 != -1)
{
tmp = newS(p1 + 1);
memcpy(tmp, s.c_str(), p1);
chunkedSize = hex2int(tmp);
delS(tmp);
}

//last chunk
if (chunkedSize < 1)
olen = -1;
}
else
chunkState = 0;
}
else
{

if (chunkedSize > -1)
{
std::string s;
int readLen = readLine(stream, s);

if (readLen > 0)
{
//chunk may contain trailing
if (dataLen + readLen - 2 < chunkedSize)
{
dataLen += readLen;
out += s;
olen = readLen;
}
else
{
if (chunkedSize - dataLen > 0)
out += s;
dataLen = chunkedSize;
chunkState = 0;
olen = readLen;
}
}
else
{
olen = -1;
}
}
}

return olen;
}

char *getHeader(const char *buf, PGM_P beginH, PGM_P endH, int &beginPos, int endPos)
{

Expand Down
3 changes: 2 additions & 1 deletion src/common.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/**
* Created March 23, 2021
* Created April 3, 2021
*
* This work is a part of Firebase ESP Client library
* Copyright (c) 2020, 2021 K. Suwatchai (Mobizt)
Expand Down Expand Up @@ -65,6 +65,7 @@ class FunctionsConfig;
#define MAX_BLOB_PAYLOAD_SIZE 1024
#define MAX_EXCHANGE_TOKEN_ATTEMPTS 5
#define ESP_DEFAULT_TS 1510644967
#define ESP_SIGNER_DEFAULT_RESPONSE_BUFFER_SIZE 2560

enum fb_esp_fcm_msg_mode
{
Expand Down
Loading

0 comments on commit 754c3d3

Please sign in to comment.