Skip to content

Commit

Permalink
Fixed error handling when promise modules send bad responses
Browse files Browse the repository at this point in the history
Changelog: None
Ticket: None
Signed-off-by: Ole Herman Schumacher Elgesem <ole@northern.tech>
  • Loading branch information
olehermanse committed Dec 7, 2020
1 parent b8a7ad2 commit 9695be3
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions libpromises/mod_custom.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ static JsonElement *PromiseModule_Receive(PromiseModule *module)
Log(LOG_LEVEL_ERR,
"Promise module sent invalid log line: '%s'",
line);
// Skip this line but keep parsing
FREE_AND_NULL(line);
size = 0;
continue;
}
const char *const message = equal_sign + 1;
const char *const level_start = line + strlen("log_");
Expand Down Expand Up @@ -231,12 +235,24 @@ static JsonElement *PromiseModule_Receive(PromiseModule *module)
free(line);
return NULL;
}
assert(response != NULL);
}

FREE_AND_NULL(line);
size = 0;
}

if (response == NULL)
{
// This can happen if using the JSON protocol, and the module sends
// nothing (newlines) or only log= lines.
assert(!line_based);
Log(LOG_LEVEL_ERR,
"The '%s' promise module sent an invalid/incomplete response with JSON based protocol",
module->path);
return NULL;
}

if (line_based)
{
JsonObjectAppendArray(response, "log", log_array);
Expand Down Expand Up @@ -283,6 +299,7 @@ static JsonElement *PromiseModule_Receive(PromiseModule *module)
}
JsonDestroy(log_array);

assert(response != NULL);
return response;
}

Expand Down Expand Up @@ -582,6 +599,12 @@ static bool PromiseModule_Validate(PromiseModule *module, const Promise *pp)
// Prints errors / log messages from module:
JsonElement *response = PromiseModule_Receive(module);

if (response == NULL)
{
// Error already printed in PromiseModule_Receive()
return false;
}

const bool valid = HasResultAndResultIsValid(response);

JsonDestroy(response);
Expand Down

0 comments on commit 9695be3

Please sign in to comment.