From 0efbfe0bbc003e1a15a91c4c833e27f597274923 Mon Sep 17 00:00:00 2001 From: ZXShady <153229951+ZXShady@users.noreply.github.com> Date: Sat, 28 Sep 2024 22:05:18 +0100 Subject: [PATCH] check malloc return value for NULL and update docs `sfFtpDirectoryResponse_getDirectory` can fail and return NULL becuase 'strdup' returns null on failure but `sfFtpDirectoryResponse_getDirectoryUnicode` didn't and just had UB due not checking return value of malloc. typing NULL in the docs made me age 30 years btw --- include/CSFML/Network/Ftp.h | 4 ++-- src/CSFML/Network/Ftp.cpp | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/include/CSFML/Network/Ftp.h b/include/CSFML/Network/Ftp.h index 4d07b11f..c1739c27 100644 --- a/include/CSFML/Network/Ftp.h +++ b/include/CSFML/Network/Ftp.h @@ -225,7 +225,7 @@ CSFML_NETWORK_API const char* sfFtpDirectoryResponse_getMessage(const sfFtpDirec /// /// \param ftpDirectoryResponse Ftp directory response /// -/// \return Directory name +/// \return Directory name or NULL if it failed /// //////////////////////////////////////////////////////////// CSFML_NETWORK_API const char* sfFtpDirectoryResponse_getDirectory(const sfFtpDirectoryResponse* ftpDirectoryResponse); @@ -239,7 +239,7 @@ CSFML_NETWORK_API const char* sfFtpDirectoryResponse_getDirectory(const sfFtpDir /// /// \param ftpDirectoryResponse Ftp directory response /// -/// \return Directory name +/// \return Directory name or NULL if it failed /// //////////////////////////////////////////////////////////// CSFML_NETWORK_API const sfChar32* sfFtpDirectoryResponse_getDirectoryUnicode(const sfFtpDirectoryResponse* ftpDirectoryResponse); diff --git a/src/CSFML/Network/Ftp.cpp b/src/CSFML/Network/Ftp.cpp index 24f412de..f7ab3b8c 100644 --- a/src/CSFML/Network/Ftp.cpp +++ b/src/CSFML/Network/Ftp.cpp @@ -51,6 +51,8 @@ static_assert(alignof(sfChar32) == alignof(char32_t)); { const std::size_t byteCount = sizeof(sfChar32) * str.getSize(); auto* utf32 = static_cast(std::malloc(byteCount + sizeof(sfChar32))); + if (!utf32) + return nullptr; std::memcpy(utf32, str.getData(), byteCount); utf32[str.getSize()] = 0;