Skip to content

Commit 3ebcabc

Browse files
committed
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
1 parent ef81fbd commit 3ebcabc

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

include/CSFML/Network/Ftp.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ CSFML_NETWORK_API const char* sfFtpDirectoryResponse_getMessage(const sfFtpDirec
225225
///
226226
/// \param ftpDirectoryResponse Ftp directory response
227227
///
228-
/// \return Directory name
228+
/// \return Directory name or NULL if it failed
229229
///
230230
////////////////////////////////////////////////////////////
231231
CSFML_NETWORK_API const char* sfFtpDirectoryResponse_getDirectory(const sfFtpDirectoryResponse* ftpDirectoryResponse);
@@ -239,7 +239,7 @@ CSFML_NETWORK_API const char* sfFtpDirectoryResponse_getDirectory(const sfFtpDir
239239
///
240240
/// \param ftpDirectoryResponse Ftp directory response
241241
///
242-
/// \return Directory name
242+
/// \return Directory name or NULL if it failed
243243
///
244244
////////////////////////////////////////////////////////////
245245
CSFML_NETWORK_API const sfChar32* sfFtpDirectoryResponse_getDirectoryUnicode(const sfFtpDirectoryResponse* ftpDirectoryResponse);

src/CSFML/Network/Ftp.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ static_assert(alignof(sfChar32) == alignof(char32_t));
5050
[[nodiscard]] sfChar32* copyToChar32(const sf::String& str)
5151
{
5252
const std::size_t byteCount = sizeof(sfChar32) * str.getSize();
53-
auto* utf32 = static_cast<sfChar32*>(std::malloc(byteCount + sizeof(sfChar32)));
53+
auto* utf32 = static_cast<sfChar32*>(std::malloc(byteCount) + sizeof(sfChar32));
54+
if (!utf32)
55+
return nullptr;
5456
std::memcpy(utf32, str.getData(), byteCount);
55-
utf32[str.getSize()] = 0;
56-
57+
utf32[str.getSize()] = 0;
5758
return utf32;
5859
}
5960
} // namespace
6061

61-
6262
////////////////////////////////////////////////////////////
6363
void sfFtpListingResponse_destroy(const sfFtpListingResponse* ftpListingResponse)
6464
{

0 commit comments

Comments
 (0)