Main Content

CERT C++: MEM56-CPP

Do not store an already-owned pointer value in an unrelated smart pointer

Since R2021a

Description

Rule Definition

Do not store an already-owned pointer value in an unrelated smart pointer.1

Polyspace Implementation

The rule checker checks for Use of already-owned pointers

Examples

expand all

Issue

This issue occurs when you use an already-owned pointer as the argument of:

  • A smart pointer constructor. For instance, in this code snippet, raw_ptr is already owned by s_ptr1 and is used to initialize s_ptr2:

    char *raw_ptr = new char;
    std::shared_ptr<char> s_ptr1(raw_ptr);
    std::shared_ptr<char> s_ptr2(raw_ptr); //raw_ptr is already owned by s_ptr1

  • A smart pointer reset operation. For instance, in this code snippet, the reset of s_ptr2 replaces raw_ptr2 with already-owned raw_ptr1:

    char *raw_ptr1 = new char;
    char *raw_ptr2 = new char;
    
    std::shared_ptr<char> s_ptr1(raw_ptr1);
    std::shared_ptr<char> s_ptr2(raw_ptr2);
    
    s_ptr2.reset(raw_ptr1); // s_ptr2 releases raw_ptr2 and owns already owned raw_ptr1

Polyspace® checks only smart pointer types std::shared_ptr and std::unique_ptr and considers that user-defined allocators and deleters have standard allocation and deallocation behavior.

A pointer is already owned by a smart pointer if the pointer type is not std::nullptr_t and either:

  • The pointer was used to initialize the smart pointer.

  • The pointer was used as an argument to the smart pointer reset() member function.

  • The pointer is the return value of the smart pointer get() member function.

  • The pointer is the return value of the smart pointer operator-> member function.

Risk

You use smart pointers to ensure that the memory a pointer points to is automatically deallocated when the pointer is destroyed, for example if the pointer goes out of scope. When unrelated smart pointers manage the same pointer value, one of the smart pointers might attempt to deallocate memory that was already deallocated by the other smart pointer. This results in a double free vulnerability, which corrupts your program's memory management data structure.

Fix

Use std::make_shared to create a smart pointer and then use copy construction to create a related smart pointer. The underlying pointer value is managed by both smart pointers and the memory pointed to is not deallocated until all the smart pointers are destroyed.

If you do not intend to allow multiple smart pointers to manage the same pointer value, use std::make_unique to construct a std::unique_ptr smart pointer. A std::unique_ptr can only be moved, which relinquishes ownership of the underlying managed pointer value.

Example — Use of an Already-Owned Pointer
#include <memory>
#include <string>

struct Profile
{
    virtual ~Profile()=default;
};

struct Player : public Profile
{
    std::string name;
    std::int8_t rank;

    Player();
    Player(const std::string& name_, const std::int8_t& rank_) :
        name{ name_ }, rank{ rank_ } {}
};

void func(){

    Player * player = new Player("Richard Roll",1);
    std::shared_ptr<Player> player1(player);
    std::shared_ptr<Player> top_rank(player); //Non-compliant

}

In this example, the use of pointer value player to construct smart pointer top_rank in function func is non-compliant. player is already owned by smart pointer player1. When player1 is destroyed, it might attempt to delete pointer value player which was already deleted by top_rank.

Correction — Use std::make_shared and Copy Construction to Create Related Smart Pointers
#include <memory>
#include <string>

struct Profile
{
    virtual ~Profile()=default;
};

struct Player : public Profile
{
    std::string name;
    std::int8_t rank;

    Player();
    Player(const std::string& name_, const std::int8_t& rank_) :
        name{ name_ }, rank{ rank_ } {}
};

void func2(){

    std::shared_ptr<Player> player1_shared =
        std::make_shared<Player>("Richard Roll",1);
    std::shared_ptr<Player> top_rank_shared(player1_shared); //Compliant

}

One possible correction is to use std::make_shared to declare player1_shared, and then use copy construction to create related smart pointer top_rank_shared. The underlying pointer value is not deleted until all smart pointers are destroyed.

Check Information

Group: Rule 06. Memory Management (MEM)

Version History

Introduced in R2021a


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.