Transceiver.cpp: properly zero-terminate received commands
Previously it was assumed that a sender should zero-terminate
each command being sent. Otherwise, this could cause to printing
garbage. Let's do this manually, using the length of received
data as a position for '\0'.
Change-Id: I69f413f33156c38a853efc5a8cdc66fbfb0ca6af
diff --git a/Transceiver52M/Transceiver.cpp b/Transceiver52M/Transceiver.cpp
index 7dc4c1c..8f41c5e 100644
--- a/Transceiver52M/Transceiver.cpp
+++ b/Transceiver52M/Transceiver.cpp
@@ -661,20 +661,20 @@
void Transceiver::driveControl(size_t chan)
{
- // check control socket
- char buffer[MAX_PACKET_LENGTH];
- int msgLen = -1;
- buffer[0] = '\0';
+ char buffer[MAX_PACKET_LENGTH + 1];
+ char response[MAX_PACKET_LENGTH + 1];
+ int msgLen;
- msgLen = mCtrlSockets[chan]->read(buffer, sizeof(buffer));
-
- if (msgLen < 1) {
+ /* Attempt to read from control socket */
+ msgLen = mCtrlSockets[chan]->read(buffer, MAX_PACKET_LENGTH);
+ if (msgLen < 1)
return;
- }
+
+ /* Zero-terminate received string */
+ buffer[msgLen] = '\0';
char cmdcheck[4];
char command[MAX_PACKET_LENGTH];
- char response[MAX_PACKET_LENGTH];
sscanf(buffer,"%3s %s",cmdcheck,command);