0

I have the following structure,

struct MyStruct{
bool myBool,
int myInt,
std::unique_ptr<boost::circular_buffer<int>> myUniquePtr;
} ;

Let's say I construct the following object,

MyStruct* myStruct = new MyStruct();

I then update the size of my buffer with

myStruct->myUniquePtr = std::make_unique<boost::circular_buffer<int>>(DIM) 

where

 #define DIM 8

It works as expected but my static code analyzer (Klocwork) flags the previous line (the one with make_unique)

LOCRET.ARG: Address of local variable is returned through formal argument

Details:

  • Local address "&std::make_unique<boost::circular_buffer < int > >(DIM)" is passed through 'operator=' and stored into 'myStruct->myUniquePtr._M_t'

  • 'myStruct->myUniquePtr._M_t' can be used outside of this function

In fact, all lines relying on make_unique yield this warning. I am wondering whether it's a false positive or not.

Minimal Reproducible Example:

struct MyStruct{
        bool myBool,
        int myInt,
        std::unique_ptr<boost::circular_buffer<int>> myUniquePtr;
        } ;

void initMyStruct(MyStruct* pStruct)
{
 pStruct->myUniquePtr = std::make_unique<<boost::circular_buffer<int>>(DIM);
}

and as main:

MyStruct* myStruct = new MyStruct();
initMyStruct(myStruct);
floflo29
  • 2,261
  • 2
  • 22
  • 45
  • 1
    Why not `boost::circular_buffer myMember;`? There is no much sense in using pointers. Could you post a [mcve]? The shown code has no functions to show. – 273K Apr 27 '23 at 08:05
  • And why not initalize that boost::circular_buffer or its unique_pointer in the constructor of the struct (a struct and class are the same thing except for default public/private behavior). There is nothing dynamic about it is always the same size and it is not huge (so will fit on stack if needed). – Pepijn Kramer Apr 27 '23 at 08:32
  • @PepijnKramer: In this example yes, it's the same size, but the code (I did not code it from scratch, I got it as is) first constructs objects then initializes it. Historically, it's a pure C code, so it uses C structures and uses pointers (with malloc) to set arrays size during init (sizes are not necessarily known at compile time, they can be read from a config file for example) – floflo29 Apr 27 '23 at 08:42
  • Ok with malloc then initializing members is out of the picture ;) I don't understand the klocwork warning either. – Pepijn Kramer Apr 27 '23 at 08:45
  • @273K added. I could also get rid of the (smart) pointer but I currently just want to understand the warning origin. – floflo29 Apr 27 '23 at 11:26
  • I've added details from Klocwork – floflo29 Apr 27 '23 at 14:24

0 Answers0