bytedeco/javacpp

Virtualizing a class causes a method returning a String return a dangling pointer

Open

#656 aperta il 25 feb 2023

Vedi su GitHub
 (6 commenti) (0 reazioni) (0 assegnatari)Java (620 fork)batch import
enhancementhelp wanted

Metriche repository

Star
 (4279 star)
Metriche merge PR
 (Nessuna PR mergiata in 30 g)

Descrizione

The JNI code for method name of Module in Pytorch contains:

signed char* rptr;
StringAdapter< char > radapter(ptr->name());
rptr = radapter;

If we add an Info virtualize() on Module, the code becomes:

const signed char* rptr;
StringAdapter< char > radapter((dynamic_cast<JavaCPP_torch_0003a_0003ann_0003a_0003aModule*>(ptr) != NULL ? ((JavaCPP_torch_0003a_0003ann_0003a_0003aModule*)ptr)->name() : ptr->name()));
rptr = radapter;

The const modifier of rptr, when present or not, changes which overload of the implicit cast on the last line is applied. When absent, a copy of the string is performed. When present, no copy is performed and the returned pointer is dangling because the string it points to is freed at the end of the block.

Probably related to #374

Guida contributor