Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(zenroom): initialize zenroom before script check, in this way the error on empty script follows the logfmt specification #892

Merged
merged 2 commits into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions bindings/javascript/src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,34 @@ test("does broke gracefully", async (t) => {
test("does handle empty zencode", async (t) => {
try {
await zencode_exec(null);
t.fail("NULL zencode script does not fail");
} catch (e) {
t.true(e.logs.includes("NULL string as script argument"));
const lines = JSON.parse(e.logs);
t.true(lines.includes("[!] NULL string as script argument"), e.logs);
}
try {
await zencode_exec(``);
} catch (e) {
t.true(e.logs.includes("Empty string as script argument"));
t.fail("empty zencode script does not fail");
} catch(e) {
const lines = JSON.parse(e.logs);
t.true(lines.includes("[!] Empty string as script argument"), e.logs);
}
});

test("does handle empty lua", async (t) => {
try {
await zenroom_exec(null);
t.fail("NULL lua script does not fail");
} catch (e) {
t.true(e.logs.includes("NULL string as script argument"));
const lines = JSON.parse(e.logs);
t.true(lines.includes("[!] NULL string as script argument"), e.logs);
}
try {
await zenroom_exec(``);
t.fail("empty lua script does not fail");
} catch (e) {
t.true(e.logs.includes("Empty string as script argument"));
const lines = JSON.parse(e.logs);
t.true(lines.includes("[!] Empty string as script argument"), e.logs);
}
});

Expand Down
36 changes: 18 additions & 18 deletions src/zenroom.c
Original file line number Diff line number Diff line change
Expand Up @@ -429,19 +429,21 @@ int zen_exec_lua(zenroom_t *ZZ, const char *script) {
return ZZ->exitcode;
}

int _check_script_arg(const char *s) {
int _check_script_arg(zenroom_t *ZZ, const char *s) {
if(!s) {
_err( "NULL string as script argument");
_err( "Execution aborted");
zerror(ZZ->lua, "NULL string as script argument");
zerror(ZZ->lua, "Execution aborted");
zen_teardown(ZZ);
#ifdef __EMSCRIPTEN__
EM_ASM({Module.exec_error();});
EM_ASM(Module.onAbort(););
#endif
return ERR_INIT;
}
if(s[0] == '\0') {
_err( "Empty string as script argument");
_err( "Execution aborted");
zerror(ZZ->lua, "Empty string as script argument");
zerror(ZZ->lua, "Execution aborted");
zen_teardown(ZZ);
#ifdef __EMSCRIPTEN__
EM_ASM({Module.exec_error();});
EM_ASM(Module.onAbort(););
Expand Down Expand Up @@ -495,30 +497,30 @@ int _check_zenroom_result(zenroom_t *zz) {

int zencode_exec(const char *script, const char *conf, const char *keys, const char *data) {

if (_check_script_arg(script) != SUCCESS) return ERR_INIT;

const char *c, *k, *d;
c = conf ? (conf[0] == '\0') ? NULL : conf : NULL;
k = keys ? (keys[0] == '\0') ? NULL : keys : NULL;
d = data ? (data[0] == '\0') ? NULL : data : NULL;
zenroom_t *Z = zen_init(c, k, d);

zenroom_t *Z = zen_init(c, k, d);
if (_check_zenroom_init(Z) != SUCCESS) return ERR_INIT;
if (_check_script_arg(Z, script) != SUCCESS) return ERR_INIT;

zen_exec_zencode(Z, script);
return( _check_zenroom_result(Z) );
}

int zenroom_exec(const char *script, const char *conf, const char *keys, const char *data) {

if (_check_script_arg(script) != SUCCESS) return ERR_INIT;

const char *c, *k, *d;
c = conf ? (conf[0] == '\0') ? NULL : conf : NULL;
k = keys ? (keys[0] == '\0') ? NULL : keys : NULL;
d = data ? (data[0] == '\0') ? NULL : data : NULL;

zenroom_t *Z = zen_init(c, k, d);
if (_check_zenroom_init(Z) != SUCCESS) return ERR_INIT;
if (_check_script_arg(Z, script) != SUCCESS) return ERR_INIT;

zen_exec_lua(Z, script);
return( _check_zenroom_result(Z));
}
Expand All @@ -527,15 +529,14 @@ int zencode_exec_tobuf(const char *script, const char *conf, const char *keys, c
char *stdout_buf, size_t stdout_len,
char *stderr_buf, size_t stderr_len) {

if (_check_script_arg(script) != SUCCESS) return ERR_INIT;

const char *c, *k, *d;
c = conf ? (conf[0] == '\0') ? NULL : conf : NULL;
k = keys ? (keys[0] == '\0') ? NULL : keys : NULL;
d = data ? (data[0] == '\0') ? NULL : data : NULL;

zenroom_t *Z = zen_init(c, k, d);
if (_check_zenroom_init(Z) != SUCCESS) return ERR_INIT;
if (_check_script_arg(Z, script) != SUCCESS) return ERR_INIT;

// setup stdout and stderr buffers
Z->stdout_buf = stdout_buf;
Expand All @@ -551,15 +552,14 @@ int zenroom_exec_tobuf(const char *script, const char *conf, const char *keys, c
char *stdout_buf, size_t stdout_len,
char *stderr_buf, size_t stderr_len) {

if (_check_script_arg(script) != SUCCESS) return ERR_INIT;

const char *c, *k, *d;
c = conf ? (conf[0] == '\0') ? NULL : conf : NULL;
k = keys ? (keys[0] == '\0') ? NULL : keys : NULL;
d = data ? (data[0] == '\0') ? NULL : data : NULL;

zenroom_t *Z = zen_init(c, k, d);
if (_check_zenroom_init(Z) != SUCCESS) return ERR_INIT;
if (_check_script_arg(Z, script) != SUCCESS) return ERR_INIT;

// setup stdout and stderr buffers
Z->stdout_buf = stdout_buf;
Expand All @@ -572,24 +572,24 @@ int zenroom_exec_tobuf(const char *script, const char *conf, const char *keys, c

int zencode_valid_input(const char *script, const char *conf, const char *keys, const char *data, const char *extra) {

if (_check_script_arg(script) != SUCCESS) return ERR_INIT;

const char *c = "scope=given";
const char *k = keys ? (keys[0] == '\0') ? NULL : keys : NULL;
const char *d = data ? (data[0] == '\0') ? NULL : data : NULL;

zenroom_t *Z = zen_init(c, k, d);
if (_check_zenroom_init(Z) != SUCCESS) return ERR_INIT;
if (_check_script_arg(Z, script) != SUCCESS) return ERR_INIT;

zen_exec_zencode(Z, script);
return( _check_zenroom_result(Z));
}

int zencode_valid_code(const char *script, const char *conf, const int strict) {
if (_check_script_arg(script) != SUCCESS) return ERR_INIT;
const char *c, *k, *d;
const char *c;
c = conf ? (conf[0] == '\0') ? NULL : conf : NULL;
zenroom_t *Z = zen_init(c, NULL, NULL);
if (_check_zenroom_init(Z) != SUCCESS) return ERR_INIT;
if (_check_script_arg(Z, script) != SUCCESS) return ERR_INIT;
// disable strict parsing
if (!strict) {
luaL_dostring(Z->lua, "CONF.parser.strict_parse=false");
Expand Down
Loading