From da05b706e4880afc63c44ed16967f6c7ce3a8335 Mon Sep 17 00:00:00 2001 From: LordGrimmauld Date: Wed, 6 Mar 2024 17:48:56 +0100 Subject: [PATCH] terminate read strings so that json parsing no longer fails --- main.cpp | 5 ----- sway_bindings/swaymsg.h | 5 +++-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/main.cpp b/main.cpp index b855c3b..21e1c1e 100644 --- a/main.cpp +++ b/main.cpp @@ -21,11 +21,6 @@ using json = nlohmann::json; int main(int argc, char *argv[]) { - auto sway = Sway(); - auto resp = sway.sendIPC(SWAY_GET_TREE); - json rep = json::parse(resp.msg); - auto rootRecord = SwayRecord(rep); - QApplication a(argc, argv); QCoreApplication::setOrganizationName("Grimmauld"); QCoreApplication::setApplicationName("SwayMux"); diff --git a/sway_bindings/swaymsg.h b/sway_bindings/swaymsg.h index bb195bd..50eff16 100644 --- a/sway_bindings/swaymsg.h +++ b/sway_bindings/swaymsg.h @@ -59,13 +59,14 @@ struct swaymsg { >> Formatter::to_str); } - char msgBuff[payloadLen]; + char msgBuff[payloadLen + 1]; if ((dlen = recv(sockfd, msgBuff, payloadLen, 0)) != payloadLen) { throw std::runtime_error( Formatter() << "Swaysock: Unexpected length on recv() call for message: " << dlen >> Formatter::to_str); } - return swaymsg(payloadType, msgBuff, payloadLen); + msgBuff[payloadLen] = '\00'; // make sure we have a c-style string that is properly terminated or json parsing gets all weird... + return swaymsg(payloadType, msgBuff, payloadLen + 1); }; constexpr const static char *magic = "i3-ipc";