Ensure logs don't indefinitely append past capacity

Summary:
^

There could be cases, albeit unlikely, that logs could be appended for the current state indefintely that would ultimate fail due to not having enough memory.

This change puts a cap on that.

Reviewed By: mweststrate

Differential Revision: D42313904

fbshipit-source-id: 7fd96be822c9427720bccb41c6c32a39213c7652
This commit is contained in:
Lorenzo Blasa
2023-01-10 06:48:53 -08:00
committed by Facebook GitHub Bot
parent 882b969931
commit a040fb8d4e
2 changed files with 23 additions and 6 deletions

View File

@@ -10,6 +10,8 @@
#include "FlipperStateUpdateListener.h"
#include "FlipperStep.h"
#define FLIPPER_LOGS_CAPACITY 4096
#if FLIPPER_DEBUG_LOG
#include "Log.h"
#endif
@@ -19,7 +21,8 @@ using namespace facebook::flipper;
/* Class responsible for collecting state updates and combining them into a
* view of the current state of the flipper client. */
FlipperState::FlipperState() : logs("") {}
FlipperState::FlipperState() {}
void FlipperState::setUpdateListener(
std::shared_ptr<FlipperStateUpdateListener> listener) {
std::lock_guard<std::mutex> lock(mutex);
@@ -46,14 +49,25 @@ void FlipperState::started(std::string step) {
}
}
void FlipperState::ensureLogsCapacity() {
if (logs.tellp() > FLIPPER_LOGS_CAPACITY) {
logs.str("");
logs.clear();
logs << "[Truncated]" << std::endl;
}
}
void FlipperState::success(std::string step) {
std::shared_ptr<FlipperStateUpdateListener> localListener;
{
std::lock_guard<std::mutex> lock(mutex);
std::string message = "[Success] " + step;
#if FLIPPER_DEBUG_LOG
log("[finished] " + step);
log(message);
#endif
logs = logs + "[Success] " + step + "\n";
ensureLogsCapacity();
logs << message << std::endl;
stateMap[step] = State::success;
localListener = mListener;
}
@@ -72,7 +86,8 @@ void FlipperState::failed(std::string step, std::string errorMessage) {
#if FLIPPER_DEBUG_LOG
log(message);
#endif
logs = logs + message + "\n";
ensureLogsCapacity();
logs << message << std::endl;
stateMap[step] = State::failed;
localListener = mListener;
}
@@ -88,7 +103,7 @@ void FlipperState::failed(std::string step, std::string errorMessage) {
// way
std::string FlipperState::getState() {
std::lock_guard<std::mutex> lock(mutex);
return logs;
return logs.str();
}
std::vector<StateElement> FlipperState::getStateElements() {