Language
C++
Compiler
gcc 13.2.0
Options
Warnings
Optimization
Boost 1.83.0
C++11
no pedantic
$ g++ prog.cc -Wall -Wextra -O2 -march=native -I/opt/wandbox/boost-1.83.0-gcc-13.2.0/include -std=c++11 --- ArduinoJson Dangling Pointer Proof ---
Using ArduinoJson version: 7.4.2
====== TEST 1: The Buggy Case (Pointer to Local Variable) ======
--- Inside createJsonFromLocalVar_Buggy() ---
-> The 'label' is valid here: This is a local string
--- Exiting function. 'localData' is about to be destroyed... ---
-> Main: Calling overwriteStack() to corrupt the freed memory...
-> Main: Now serializing the JsonDocument, which reads from the dangling pointer...
CORRUPTED OUTPUT:
{"value":123,"label":""}
====== TEST 2: The Fixed Case (Forcing a Copy with std::string) ======
CORRECT OUTPUT:
{"value":123,"label":"This is a local string"}
====== TEST 3: The 'Direct Access' Case (Pointer to Global Variable) ======
CORRECT OUTPUT:
{"value":999,"label":"This is a global string"}
Exit Code:
0