Skip to content

Fix json buffer guard #3743

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

Closed
wants to merge 1 commit into from
Closed
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
11 changes: 7 additions & 4 deletions wled00/json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,7 +1017,10 @@ void serializeModeNames(JsonArray arr)
// Global buffer locking response helper class
class GlobalBufferAsyncJsonResponse: public JSONBufferGuard, public AsyncJsonResponse {
public:
inline GlobalBufferAsyncJsonResponse(bool isArray) : JSONBufferGuard(17), AsyncJsonResponse(&doc, isArray) {};
// This is safe if and only if the guard owns the lock.
inline GlobalBufferAsyncJsonResponse(JSONBufferGuard&& guard, bool isArray) : JSONBufferGuard(std::move(guard)), AsyncJsonResponse(owns_lock() ? &doc : nullptr, isArray) {
assert(owns_lock());
};
virtual ~GlobalBufferAsyncJsonResponse() {};

// Other members are inherited
Expand Down Expand Up @@ -1054,12 +1057,12 @@ void serveJson(AsyncWebServerRequest* request)
return;
}

GlobalBufferAsyncJsonResponse *response = new GlobalBufferAsyncJsonResponse(subJson==JSON_PATH_FXDATA || subJson==JSON_PATH_EFFECTS); // will clear and convert JsonDocument into JsonArray if necessary
if (!response->owns_lock()) {
JSONBufferGuard guard(17);
if (!guard.owns_lock()) {
request->send(503, "application/json", F("{\"error\":3}"));
delete response;
return;
}
GlobalBufferAsyncJsonResponse *response = new GlobalBufferAsyncJsonResponse(std::move(guard), subJson==JSON_PATH_FXDATA || subJson==JSON_PATH_EFFECTS); // will clear and convert JsonDocument into JsonArray if necessary

JsonVariant lDoc = response->getRoot();

Expand Down