From c41dd46ca7aa024f057895eda52a9f6b8ea280a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Fri, 23 Dec 2022 17:51:51 +0100 Subject: [PATCH] Check for potential write error (or not buffer fully consumed) --- src/libgourou.cpp | 9 +++++++-- utils/utils_common.cpp | 16 ++++++++++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/libgourou.cpp b/src/libgourou.cpp index cf2e9c2..7341fe9 100644 --- a/src/libgourou.cpp +++ b/src/libgourou.cpp @@ -931,14 +931,19 @@ namespace gourou void DRMProcessor::exportPrivateLicenseKey(std::string path) { int fd = open(path.c_str(), O_CREAT|O_TRUNC|O_WRONLY, S_IRWXU); + int ret; + if (fd <= 0) EXCEPTION(GOUROU_FILE_ERROR, "Unable to open " << path); ByteArray privateLicenseKey = ByteArray::fromBase64(user->getPrivateLicenseKey()); /* In adobekey.py, we get base64 decoded data [26:] */ - write(fd, privateLicenseKey.data()+26, privateLicenseKey.length()-26); - + ret = write(fd, privateLicenseKey.data()+26, privateLicenseKey.length()-26); close(fd); + if (ret != privateLicenseKey.length()-26) + { + EXCEPTION(gourou::GOUROU_FILE_ERROR, "Error writing " << path); + } } int DRMProcessor::getLogLevel() {return (int)gourou::logLevel;} diff --git a/utils/utils_common.cpp b/utils/utils_common.cpp index c66af71..41c0ac7 100644 --- a/utils/utils_common.cpp +++ b/utils/utils_common.cpp @@ -99,7 +99,7 @@ void mkpath(const char *dir) void fileCopy(const char* in, const char* out) { char buffer[4096]; - int ret, fdIn, fdOut; + int ret, ret2, fdIn, fdOut; fdIn = open(in, O_RDONLY); @@ -119,7 +119,19 @@ void fileCopy(const char* in, const char* out) ret = ::read(fdIn, buffer, sizeof(buffer)); if (ret <= 0) break; - ::write(fdOut, buffer, ret); + do + { + ret2 = ::write(fdOut, buffer, ret); + if (ret2 >= 0) + { + ret -= ret2; + buffer += ret2; + } + else + { + EXCEPTION(gourou::CLIENT_FILE_ERROR, "Error writing " << out); + } + } while (ret); } close (fdIn);