Skip to content

Commit

Permalink
Report an error if DWC sends a command string that is too long
Browse files Browse the repository at this point in the history
  • Loading branch information
dc42 committed Nov 30, 2022
1 parent 46bb072 commit 8ba4605
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/GCodes/GCodeInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ void NetworkGCodeInput::Put(MessageType mtype, char c) noexcept
}
}

void NetworkGCodeInput::Put(MessageType mtype, const char *buf) noexcept
bool NetworkGCodeInput::Put(MessageType mtype, const char *buf) noexcept
{
const size_t len = strlen(buf) + 1;
MutexLocker lock(bufMutex, 200);
Expand All @@ -230,8 +230,10 @@ void NetworkGCodeInput::Put(MessageType mtype, const char *buf) noexcept
{
Put(mtype, buf[i]);
}
return true;
}
}
return false;
}

NetworkGCodeInput::NetworkGCodeInput() noexcept : RegularGCodeInput()
Expand Down
2 changes: 1 addition & 1 deletion src/GCodes/GCodeInput.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class NetworkGCodeInput : public RegularGCodeInput
NetworkGCodeInput() noexcept;

bool FillBuffer(GCodeBuffer *gb) noexcept override; // Fill a GCodeBuffer with the last available G-code
void Put(MessageType mtype, const char *buf) noexcept; // Append a null-terminated string to the buffer
bool Put(MessageType mtype, const char *buf) noexcept; // Append a null-terminated string to the buffer returning true if success

private:
void Put(MessageType mtype, char c) noexcept; // Append a single character. This does NOT lock the mutex!
Expand Down
9 changes: 6 additions & 3 deletions src/Networking/HttpResponder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,11 +529,14 @@ bool HttpResponder::GetJsonResponse(const char *_ecv_array request, OutputBuffer
const char *command = GetKeyValue("gcode");
NetworkGCodeInput * const httpInput = reprap.GetGCodes().GetHTTPInput();
// If the command is empty, just report the buffer space. This allows rr_gcode to be used to poll the buffer space without using it up.
if (command != nullptr && command[0] != 0)
if (command != nullptr && command[0] != 0 && !httpInput->Put(HttpMessage, command))
{
httpInput->Put(HttpMessage, command);
response->copy("{\"err\":1}"); // the command string wasn't accepted, it's probably too long
}
else
{
response->printf("{\"buff\":%u}", httpInput->BufferSpaceLeft());
}
response->printf("{\"buff\":%u}", httpInput->BufferSpaceLeft());
}
#if HAS_MASS_STORAGE
else if (StringEqualsIgnoreCase(request, "upload"))
Expand Down

0 comments on commit 8ba4605

Please sign in to comment.