[posix] prevent implicit conversion of aUrl to RadioUrl (#9722)

When compiling `mRadioUrl=aUrl` in `Radio::Init()`, some compilers may
implicitly convert `aUrl` to a temporary `RadioUrl` object and then
copy the temporary `RadioUrl` to `mRadioUrl` via a default copy
constructor. The pointer members of `mRadioUrl` point to the content
of the temporary `RadioUrl`, and it eventually causes the program to
access the memory that has been released, which causes the program to
crash.

This commit disables the compiler from implicitly converting `aUrl` to
a `RadioUrl` object and deletes the default copy and move
constructors.
This commit is contained in:
Zhanglong Xia
2023-12-18 10:37:31 -08:00
committed by GitHub
parent 551df11ec1
commit cca77a7948
3 changed files with 16 additions and 3 deletions
+1 -1
View File
@@ -71,7 +71,7 @@ void Radio::Init(const char *aUrl)
spinel_iid_t iidList[Spinel::kSpinelHeaderMaxNumIid];
struct ot::Spinel::RadioSpinelCallbacks callbacks;
mRadioUrl = aUrl;
mRadioUrl.Init(aUrl);
VerifyOrDie(mRadioUrl.GetPath() != nullptr, OT_EXIT_INVALID_ARGUMENTS);
memset(&callbacks, 0, sizeof(callbacks));
+1 -1
View File
@@ -135,7 +135,7 @@ const char *otSysGetRadioUrlHelpString(void)
namespace ot {
namespace Posix {
RadioUrl::RadioUrl(const char *aUrl)
void RadioUrl::Init(const char *aUrl)
{
if (aUrl != nullptr)
{
+14 -1
View File
@@ -52,7 +52,20 @@ public:
* @param[in] aUrl The null-terminated URL string.
*
*/
RadioUrl(const char *aUrl);
explicit RadioUrl(const char *aUrl) { Init(aUrl); };
/**
* Initializes the radio URL.
*
* @param[in] aUrl The null-terminated URL string.
*
*/
void Init(const char *aUrl);
RadioUrl(const RadioUrl &) = delete;
RadioUrl(RadioUrl &&) = delete;
RadioUrl &operator=(const RadioUrl &) = delete;
RadioUrl &operator=(RadioUrl &&) = delete;
private:
enum