Skip to content

Commit

Permalink
Fix memory leaks in url parsing utility function.
Browse files Browse the repository at this point in the history
  • Loading branch information
mobizt committed Mar 5, 2021
1 parent 704a295 commit 8d3dfb8
Show file tree
Hide file tree
Showing 46 changed files with 372 additions and 1,398 deletions.
46 changes: 17 additions & 29 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Firebase Arduino Client Library for ESP8266 and ESP32


Google's Firebase Arduino Client Library for ESP8266 and ESP32 v 2.0.3
Google's Firebase Arduino Client Library for ESP8266 and ESP32 v 2.0.4


This library supports ESP8266 and ESP32 MCU from Espressif. The following are platforms in which the libraries are also available (RTDB only).
Expand All @@ -18,7 +18,7 @@ This library supports ESP8266 and ESP32 MCU from Espressif. The following are pl
* NodeMCU (ESP8266)
* ESP-12F
* LinkNode (ESP8266)
* Sparkfun ESP32 Thing
* Sparkfun ESP32 Thing
* NodeMCU-32
* WEMOS LOLIN32
* TTGO T8 V1.8
Expand Down Expand Up @@ -337,12 +337,8 @@ The server's **Timestamp** can be stored in the database through `Firebase.RTDB.
The returned **Timestamp** value can get from `fbdo.intData()`.
The file systems for flash and sd memory can be changed in [**FirebaseFS.h**](/src/FirebaseFS.h).
To use LittleFS file system for flash memory instead of SPIFFS (for ESP8266 at this time), add the following macro in **FirebaseFS.h**
```C++
#define USE_LITTLEFS
```
The following example showed how to store file data to flash memory at node "/test/file_data".
Expand All @@ -352,7 +348,7 @@ The following example showed how to store file data to flash memory at node "/te
if (Firebase.RTDB.getFile(&fbdo, mem_storage_type_flash, "/test/file_data", "/test.txt"))
{
//To use LittleFS file system, add #define USE_LITTLEFS in FirebaseFS.h
//The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h.
File file = FLASH_FS.open("/test.txt", "r");
while (file.available())
Expand Down Expand Up @@ -732,13 +728,9 @@ if (fbdo.streamAvailable())

This library allows you to backup and restores the database at the defined path.

The backup file will store in SD card or flash memory (SPIFFS or LittleFS file systems).
The backup file will store in SD/SDMMC card or flash memory.

To use LittleFS file system for flash memory instead of SPIFFS (for ESP8266 only at this time), add the following macro in **FirebaseFS.h**

```C++
#define USE_LITTLEFS
```
The file systems for flash and sd memory can be changed in [**FirebaseFS.h**](/src/FirebaseFS.h).

Due to SD library used, only 8.3 DOS format file name supported.

Expand Down Expand Up @@ -907,13 +899,9 @@ Serial.println();
```


Error Queues can be saved as a file in SD card or flash memory with function `saveErrorQueue`.

To use LittleFS file system for flash memory instead of SPIFFS, add the following macro in **FirebaseFS.h**
Error Queues can be saved as a file in SD/SDMMC card or flash memory with function `saveErrorQueue`.

```C++
#define USE_LITTLEFS
```
The file systems for flash and sd memory can be changed in [**FirebaseFS.h**](/src/FirebaseFS.h).

Error Queues store as a file can be restored to Error Queue collection with function `restoreErrorQueue`.

Expand Down Expand Up @@ -1189,7 +1177,7 @@ String jsonStr;
json.toString(jsonStr, true);
Serial.println(jsonStr);

/*
/**
This is the result of the above code
{
Expand All @@ -1215,7 +1203,7 @@ json.set("temp1/[7]", 25); //null will be created at array index 6
json.toString(jsonStr, true);
Serial.println(jsonStr);

/*
/**
The result of the above code
{
Expand Down Expand Up @@ -1248,7 +1236,7 @@ json.remove("temp2");
json.toString(jsonStr, true);
Serial.println(jsonStr);

/*
/**
The result of the above code
{
Expand Down Expand Up @@ -1288,7 +1276,7 @@ if (jsonData.success)
}

//The above code will show
/*
/**
string
Celcius
*/
Expand Down Expand Up @@ -1318,7 +1306,7 @@ for (size_t i = 0; i < myArr.size(); i++)
Serial.println(jsonData.stringValue);
}

/*
/**
The result of above code
Array index: 0, type: int, value: 47
Array index: 1, type: int, value: 34
Expand Down Expand Up @@ -1359,7 +1347,7 @@ String arrStr;
arr.toString(arrStr, true);
Serial.println(arrStr);

/*
/**
This is the result of the above code
[
Expand Down Expand Up @@ -1389,7 +1377,7 @@ arr.remove("[4]/[0]/[1]/amount");
arr.toString(arrStr, true);
Serial.println(arrStr);

/*
/**
The result of the above code
[
Expand Down Expand Up @@ -1430,7 +1418,7 @@ if(jsonData.success)
}

//The above code will show
/*
/**
string
salad
*/
Expand Down Expand Up @@ -1471,7 +1459,7 @@ for (size_t i = 0; i < len; i++)
myJson.iteratorEnd();


/*
/**
The result of the above code
0, Type: object, Key: food, Value: salad
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ void setup()
/* Assign the project host (required) */
config.host = FIREBASE_HOST;

/* The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h. */

/* Assign the sevice account JSON file and the file storage type (required) */
config.service_account.json.path = "/service_account_file.json"; //change this for your json file
config.service_account.json.storage_type = mem_storage_type_flash;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ void setup()
config.host = FIREBASE_HOST;
config.api_key = API_KEY;

/* The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h. */

/* Assign the sevice account JSON file and the file storage type (required) */
config.service_account.json.path = "/service_account_file.json"; //change this for your json file
config.service_account.json.storage_type = mem_storage_type_flash; //or mem_storage_type_sd
Expand Down
10 changes: 6 additions & 4 deletions examples/RTDB/AutomaticPlantWatering/AutomaticPlantWatering.ino
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,10 @@ void setup()

Firebase.reconnectWiFi(true);

#ifdef ESP8266
//Set the size of WiFi rx/tx buffers in the case where we want to work with large data.
fbdo2.setBSSLBufferSize(1024, 1024);
fbdo.setBSSLBufferSize(1024, 1024);
#endif

//Set the size of HTTP response buffers in the case where we want to work with large data.
fbdo2.setResponseSize(1024);
Expand Down Expand Up @@ -359,7 +361,7 @@ void setClock(float time_zone, float daylight_offset_in_sec)
if (timeReady)
Firebase.RTDB.set(&fbdo2, Path.c_str(), "idle");
else
Firebase.RTDB.set(&fbdo2,Path.c_str(), "cannot get time");
Firebase.RTDB.set(&fbdo2, Path.c_str(), "cannot get time");
}

void addPump(String id, String name, String location, int gpio, int state, FirebaseJsonArray *pumpConfig)
Expand Down Expand Up @@ -471,10 +473,10 @@ void runSchedule()

scheduleInfo[i].state = 2;
Path = path + "/control/" + pumpInfo[index].id;
Firebase.RTDB.set(&fbdo2,Path.c_str(), scheduleInfo[i].active);
Firebase.RTDB.set(&fbdo2, Path.c_str(), scheduleInfo[i].active);
setPumpState(index, scheduleInfo[i].active);
Path = path + "/status/terminal";
Firebase.RTDB.set(&fbdo2,Path.c_str(), status);
Firebase.RTDB.set(&fbdo2, Path.c_str(), status);
}
}
}
Expand Down
11 changes: 7 additions & 4 deletions examples/RTDB/Backup_and_Restore/Backup_and_Restore.ino
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,10 @@ void setup()
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);

#ifdef ESP8266
//Set the size of WiFi rx/tx buffers in the case where we want to work with large data.
fbdo.setBSSLBufferSize(1024, 1024);
#endif

//Set the size of HTTP response buffers in the case where we want to work with large data.
fbdo.setResponseSize(1024);
Expand All @@ -81,15 +83,15 @@ void setup()
{
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.fileTransferError());
Serial.println("------------------------------------");
Serial.println("------------------------------------");
Serial.println();
}
else
{
Serial.println("PASSED");
Serial.println("BACKUP FILE: " + fbdo.getBackupFilename());
Serial.println("FILE SIZE: " + String(fbdo.getBackupFileSize()));
Serial.println("------------------------------------");
Serial.println("------------------------------------");
Serial.println();
}

Expand All @@ -100,18 +102,19 @@ void setup()
//{TARGET_NODE_PATH} is the full path of database to restore
//{FILE_NAME} is file name in 8.3 DOS format (max. 8 bytes file name and 3 bytes file extension)

//The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h.
if (!Firebase.RTDB.restore(&fbdo, mem_storage_type_sd, "/{TARGET_NODE_PATH}", "/{FILE_NAME}"))
{
Serial.println("FAILED");
Serial.println("REASON: " + fbdo.fileTransferError());
Serial.println("------------------------------------");
Serial.println("------------------------------------");
Serial.println();
}
else
{
Serial.println("PASSED");
Serial.println("BACKUP FILE: " + fbdo.getBackupFilename());
Serial.println("------------------------------------");
Serial.println("------------------------------------");
Serial.println();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,10 @@ void setup()
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);

#ifdef ESP8266
//Set the size of WiFi rx/tx buffers in the case where we want to work with large data.
fbdo.setBSSLBufferSize(1024, 1024);
#endif

//Set the size of HTTP response buffers in the case where we want to work with large data.
fbdo.setResponseSize(1024);
Expand All @@ -75,7 +77,7 @@ void setup()
//Download and save data to Flash memory.
//{TARGET_NODE_PATH} is the full path of database to backup and restore.
//{FILE_NAME} is file name included path to save to Flash meory

//The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h.
if (!Firebase.RTDB.backup(&fbdo, mem_storage_type_flash, "/{TARGET_NODE_PATH}", "/{FILE_NAME}"))
{
Serial.println("FAILED");
Expand All @@ -98,7 +100,7 @@ void setup()
//Restore data to defined database path using backup file on Flash memory.
//{TARGET_NODE_PATH} is the full path of database to restore
//{FILE_NAME} is file name included path of backed up file.

//The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h.
if (!Firebase.RTDB.restore(&fbdo, mem_storage_type_flash, "/{TARGET_NODE_PATH}", "/{FILE_NAME}"))
{
Serial.println("FAILED");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ void setup()

Serial.println("------------------------------------");
Serial.println("Backup test...");


//The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h.
if (!Firebase.RTDB.backup(&fbdo, mem_storage_type_flash, "/PATH_TO_THE_NODE", "/PATH_TO_SAVE_FILE"))
{
Serial.println("FAILED");
Expand Down
6 changes: 4 additions & 2 deletions examples/RTDB/Basic_with_Cert/Basic_with_Cert.ino
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,14 @@ void setup()

/**
* In case using flash mem to keep the certificate files
* Upload the certificate files cert.cer and cert.der to flash memory.
* Upload the certificate files cert.pem and cert.der to flash memory.
* Use the following lines to set the certificate file.
*
* config.cert.file = "/cert.der"; or
* config.cert.file = "/cert.cer";
* config.cert.file = "/cert.pem";
* config.cert.file_storage = mem_storage_type_flash; //or mem_storage_type_sd
*
* The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h.
*/

/* In case the certificate data was used */
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions examples/RTDB/MultiPath_Stream/MultiPath_Stream.ino
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ void setup()
//Set the size of HTTP response buffers in the case where we want to work with large data.
fbdo2.setResponseSize(1024);

//The data under the node being stream (parent path) should keep small
//Large stream payload leads to the parsing error due to memory allocation.

//The operations is the same as normal stream unless the JSON stream payload will be parsed
//with the predefined node path (child paths).

//The changes occurred in any child node that is not in the child paths array will sent to the
//client as usual.
if (!Firebase.RTDB.beginMultiPathStream(&fbdo1, parentPath.c_str(), childPath, childPathSize))
{
Serial.println("------------------------------------");
Expand Down
2 changes: 2 additions & 0 deletions examples/RTDB/Retry_and_Queue/Retry_and_Queue.ino
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ void setup()
fbdo.setResponseSize(1024);

//Open and retore Firebase Error Queues from file.
//The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h.
if (Firebase.RTDB.errorQueueCount(&fbdo, "/test.txt", mem_storage_type_flash) > 0)
{
Firebase.RTDB.restoreErrorQueue(&fbdo, "/test.txt", mem_storage_type_flash);
Expand Down Expand Up @@ -285,6 +286,7 @@ void setup()
Serial.println();

//Save Error Queues to file
//The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h.
Firebase.RTDB.saveErrorQueue(&fbdo, "/test.txt", mem_storage_type_flash);
}

Expand Down
2 changes: 2 additions & 0 deletions examples/RTDB/Stream/Stream.ino
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ void setup()
//Set the size of HTTP response buffers in the case where we want to work with large data.
fbdo.setResponseSize(1024);

//The data under the node being stream (parent path) should keep small
//Large stream payload leads to the parsing error due to memory allocation.
if (!Firebase.RTDB.beginStream(&fbdo, path.c_str()))
{
Serial.println("------------------------------------");
Expand Down
2 changes: 2 additions & 0 deletions examples/RTDB/Stream_Callback/Stream_Callback.ino
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ void setup()
//Set the size of HTTP response buffers in the case where we want to work with large data.
fbdo2.setResponseSize(1024);

//The data under the node being stream (parent path) should keep small
//Large stream payload leads to the parsing error due to memory allocation.
if (!Firebase.RTDB.beginStream(&fbdo1, path.c_str()))
{
Serial.println("------------------------------------");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void setup()

Serial.println("------------------------------------");
Serial.println("Download file test...");

//The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h.
if (Firebase.Storage.download(&fbdo, STORAGE_BUCKET_ID /* Firebase Storage bucket id */, "path/to/file/filename" /* path of remote file stored in the bucket */, "/path/to/save/filename" /* path to local file */, mem_storage_type_flash /* memory storage type, mem_storage_type_flash and mem_storage_type_sd */))
{
Serial.println("PASSED");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ void setup()

Serial.println("------------------------------------");
Serial.println("Upload byte array test...");


//MIME type should be valid to avoid the download problem.
if (Firebase.Storage.upload(&fbdo, STORAGE_BUCKET_ID /* Firebase Storage bucket id */, test_data /* byte array from ram or flash */, 256 /* size of data in bytes */, "test.dat" /* path of remote file stored in the bucket */, "application/octet-stream" /* mime type */))
{

Expand Down
2 changes: 2 additions & 0 deletions examples/Storage/Firebase_Storage/Upload_File/Upload_File.ino
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ void setup()
//Upload large file over fews hundreds KiB is not allowable in Firebase Storage by using this method.
//To upload the large file, please use the Firebase.GCStorage.upload instead.
//The following upload will be error 503 service unavailable (upload rejected due to the large file size)
//MIME type should be valid to avoid the download problem.
//The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h.
if (Firebase.Storage.upload(&fbdo, STORAGE_BUCKET_ID /* Firebase Storage bucket id */, "/media.mp4" /* path to local file */, mem_storage_type_flash /* memory storage type, mem_storage_type_flash and mem_storage_type_sd */, "media.mp4" /* path of remote file stored in the bucket */, "video/mp4" /* mime type */))
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ void setup()

//StorageGetOptions option;
//For query parameters description of StorageGetOptions, see https://cloud.google.com/storage/docs/json_api/v1/objects/get#optional-parameters

//The file systems for flash and SD/SDMMC can be changed in FirebaseFS.h.
if (Firebase.GCStorage.download(&fbdo, STORAGE_BUCKET_ID /* Firebase or Google Cloud Storage bucket id */, "path/to/file/filename" /* path of remote file stored in the bucket */, "/path/to/save/filename" /* path to local file */, mem_storage_type_flash /* memory storage type, mem_storage_type_flash and mem_storage_type_sd */, nullptr /* StorageGetOptions data */))
{
Serial.println("PASSED");
Expand Down
Loading

0 comments on commit 8d3dfb8

Please sign in to comment.