Skip to content

Commit

Permalink
harden smem export
Browse files Browse the repository at this point in the history
Harden the logic for exporting smem to prevent segfaults. Previously we had a
segfault with just `smem -x file.soar` because the smem DB is not connected.
Adding an LTI option, however, would connect to the DB first. Change it so that
export (as with backup) requires connecting to the DB first, and gracefully
returns false if not connected.
  • Loading branch information
garfieldnate committed Aug 23, 2023
1 parent 39d3d66 commit 343e367
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 41 deletions.
3 changes: 3 additions & 0 deletions Core/CLI/src/cli_load_save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,9 @@ bool CommandLineInterface::DoSave(std::vector<std::string>& argv, const std::str
if (thisAgent->SMem->connected() && (thisAgent->SMem->statistics->nodes->get_value() > 0))
{
result = thisAgent->SMem->export_smem(0, export_text, &(err));
if (!result) {
SetError(*err);
}
AddSaveText("# Semantic Memory\n");
if (!DoCLog(LOG_ADD, 0, &export_text, true)) return false;
} else {
Expand Down
48 changes: 7 additions & 41 deletions Core/CLI/src/cli_smem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -403,14 +403,13 @@ bool CommandLineInterface::DoSMem(const char pOp, const std::string* pArg1, cons
std::string* err = new std::string("");
uint64_t lti_id = NIL;

if (pArg2)
if (!thisAgent->SMem->connected())
{
thisAgent->SMem->attach();
if (!thisAgent->SMem->connected())
{
return SetError("Semantic memory database not connected.");
}
return SetError("Cannot export smem: smem database not connected.");
}

if (pArg2)
{
const char* pAttr_c_str = pArg2->c_str();
soar::Lexer lexer(thisAgent, pAttr_c_str);
if (!lexer.get_lexeme()) return SetError("LTI not found.");
Expand Down Expand Up @@ -456,41 +455,8 @@ bool CommandLineInterface::DoSMem(const char pOp, const std::string* pArg1, cons
return false;
}

PrintCLIMessage("Exported semantic memory to file.");
}
delete err;
return result;
}
else if (pOp == 'x')
{
std::string* err = new std::string("smem_export.soar");
uint64_t lti_id = NIL;

std::string export_text;
bool result = thisAgent->SMem->export_smem(0, export_text, &(err));

if (!result)
{
SetError(*err);
}
else
{
if (!DoCLog(LOG_NEW, err, 0, true))
{
return false;
}

if (!DoCLog(LOG_ADD, 0, &export_text, true))
{
return false;
}

if (!DoCLog(LOG_CLOSE, 0, 0, true))
{
return false;
}

PrintCLIMessage("Exported semantic memory to file.");
tempString << "Exported semantic memory to file '" << pArg1->c_str() << "'.";
PrintCLIMessage(&tempString);
}
delete err;
return result;
Expand Down
4 changes: 4 additions & 0 deletions Core/SoarKernel/src/semantic_memory/smem_print.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
bool SMem_Manager::export_smem(uint64_t lti_id, std::string& result_text, std::string** err_msg)
{
ltm_set store_set;
if (!connected()) {
(*err_msg)->append("Cannot export semantic memory if it is not connected.");
return false;
}

if (!lti_id)
{
Expand Down

0 comments on commit 343e367

Please sign in to comment.