Just a plain line like this triggers a Segmentation fault (core dumped):
name = j.at("name").get<std::string>();
This also doesn’t get caught with try and catch blocks even when I try to catch exception &e.
json::dump() prints correctly structured, non null, and correctly constructed. Despite of all of these, it still crashes. the even weirder part is that this line was working before.
I have tried making a string, inserting a structured looking json in it, parse it as a json, and try to deserialize it into my Item class which ends perfectly, oddly enough.
Reproduction steps
Serialize a new json file from an inventory, then try to deserialize it into another new inventory (though it crashes way before it finishes).
Expected vs. actual results
The expected result is that json::at() properly returns values appropriately but instead crashes with Segmentation fault (core dumped).
Minimal code example
inventory.cpp | Inventory::deserialization:
void Inventory::deserialize(const std::string &filename) {
cout << "Deserializing inventory..." << endl;
std::ifstream file(filename);
if (file.is_open()) {
std::cout << "Reading from file..." << std::endl;
json inventoryJson;
file >> inventoryJson;
file.close();
for (int i = 0; i < maxSlots; ++i) {
cout << "Deserializing slot " << i << endl;
if (!inventoryJson["slots"][i].is_null()) {
cout << "Slot j " << i << " is not null" << endl;
slots[i]->from_json(inventoryJson["slots"][i]);
isOccupied[i] = true;
cout << "Slot " << i << " deserialized" << endl;
…
Slot.cpp | Slot::from_json:
void Slot::from_json(const json &j) {
reset();
if (!j.is_null()) {
try {
setQuantity( item->from_json(j) );
maxStackQuantity = item->getMaxStackQuantity();
status = (quantity < maxStackQuantity) ? Status::FILLING : Status::FULL;
} catch (const json::type_error &e) {
std::cerr << "Type error during Slot deserialization: " << e.what()
<< std::endl;
throw;
} catch (const std::exception &e) {
std::cerr << "Error during Slot deserialization: " << e.what()
<< std::endl;
throw;
}
}
}
Item.cpp | Item::from_json:
int Item::from_json(const json &j) {
try {
std::cout << "Input JSON: " << j.dump(4)
<< std::endl; // Pretty print JSON for debugging
// Check and assign JSON fields
if (j.contains("name"))
name = j.at("name").get<std::string>();
…
}
}
Error messages
Segmentation fault (core dumped)
Compiler and operating system
clang++ UNIX (replit.com)
Library version
I strongly believe this is C++11
Validation
[ ] The bug also occurs if the latest version from the develop branch is used.
Description
Just a plain line like this triggers a
Segmentation fault (core dumped)
:name = j.at("name").get<std::string>();
This also doesn’t get caught with try and catch blocks even when I try to catch
exception &e
.json::dump()
prints correctly structured, non null, and correctly constructed. Despite of all of these, it still crashes. the even weirder part is that this line was working before.I have tried making a string, inserting a structured looking json in it, parse it as a json, and try to deserialize it into my
Item
class which ends perfectly, oddly enough.Reproduction steps
Serialize a new json file from an inventory, then try to deserialize it into another new inventory (though it crashes way before it finishes).
Expected vs. actual results
The expected result is that
json::at()
properly returns values appropriately but instead crashes withSegmentation fault (core dumped)
.Minimal code example
Error messages
Compiler and operating system
clang++ UNIX (replit.com)
Library version
I strongly believe this is C++11
Validation
develop
branch is used.