Skip to content

Commit

Permalink
check malloc return value for NULL and update docs
Browse files Browse the repository at this point in the history
`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
  • Loading branch information
ZXShady authored and ChrisThrasher committed Sep 29, 2024
1 parent 6ce8f95 commit 66e7af4
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 2 additions & 2 deletions include/CSFML/Network/Ftp.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/CSFML/Network/Ftp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ static_assert(alignof(sfChar32) == alignof(char32_t));
{
const std::size_t byteCount = sizeof(sfChar32) * str.getSize();
auto* utf32 = static_cast<sfChar32*>(std::malloc(byteCount + sizeof(sfChar32)));
if (!utf32)
return nullptr;
std::memcpy(utf32, str.getData(), byteCount);
utf32[str.getSize()] = 0;

Expand Down

0 comments on commit 66e7af4

Please sign in to comment.