From ce4969b02e02ec6a0132635cc8ebe166c36404ff Mon Sep 17 00:00:00 2001 From: Raul Metsma Date: Fri, 12 Jun 2026 13:05:09 +0300 Subject: [PATCH] Check std::stream read result WE2-1249 Signed-off-by: Raul Metsma --- src/controller/inputoutputmode.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/controller/inputoutputmode.cpp b/src/controller/inputoutputmode.cpp index 86db600d..8bc433ec 100644 --- a/src/controller/inputoutputmode.cpp +++ b/src/controller/inputoutputmode.cpp @@ -51,13 +51,6 @@ void setIoStreamsToBinaryMode() using namespace pcsc_cpp; -uint32_t readMessageLength(std::istream& input) -{ - uint32_t messageLength = 0; - input.read(reinterpret_cast(&messageLength), sizeof(messageLength)); - return messageLength; -} - void writeResponseLength(std::ostream& stream, const uint32_t responseLength) { stream.write(reinterpret_cast(&responseLength), sizeof(responseLength)); @@ -70,7 +63,11 @@ CommandWithArguments readCommandFromStdin() setIoStreamsToBinaryMode(); #endif - const auto messageLength = readMessageLength(std::cin); + uint32_t messageLength = 0; + std::cin.read(reinterpret_cast(&messageLength), sizeof(messageLength)); + if (std::cin.gcount() != sizeof(messageLength) || !std::cin.good()) { + throw std::runtime_error("readCommandFromStdin: Failed to read message length from stdin"); + } if (messageLength < 5) { throw std::invalid_argument("readCommandFromStdin: Message length is " @@ -83,8 +80,11 @@ CommandWithArguments readCommandFromStdin() + " exceeds maximum allowed length 8192"); } - auto message = QByteArray(int(messageLength), '\0'); + auto message = QByteArray(qsizetype(messageLength), '\0'); std::cin.read(message.data(), messageLength); + if (std::cin.gcount() != messageLength || !std::cin.good()) { + throw std::runtime_error("readCommandFromStdin: Failed to read the expected number of bytes from stdin"); + } const auto json = QJsonDocument::fromJson(message);