Skip to content

Commit 66e7af4

Browse files
ZXShadyChrisThrasher
authored andcommitted
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 6ce8f95 commit 66e7af4

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ static_assert(alignof(sfChar32) == alignof(char32_t));
5151
{
5252
const std::size_t byteCount = sizeof(sfChar32) * str.getSize();
5353
auto* utf32 = static_cast<sfChar32*>(std::malloc(byteCount + sizeof(sfChar32)));
54+
if (!utf32)
55+
return nullptr;
5456
std::memcpy(utf32, str.getData(), byteCount);
5557
utf32[str.getSize()] = 0;
5658

0 commit comments

Comments
 (0)