Skip to content

Commit

Permalink
restrict scope of eval parse result
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Sep 5, 2024
1 parent bd9dc63 commit 77b4e36
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,10 @@ void rest_start(void *arg) {

}

SEXP nano_eval_res;
static SEXP nano_parse_eval_res;

void parse_eval_safe(void *data) {
nano_eval_res = R_ParseEvalString((const char *) data, R_GlobalEnv);
nano_parse_eval_res = R_ParseEvalString((const char *) data, R_GlobalEnv);
}

void inproc_server(const char* url) {
Expand All @@ -252,27 +252,25 @@ void inproc_server(const char* url) {
if ((xc = nng_rep0_open(&s)) || (xc = nng_listen(s, url, NULL, 0)))
fatal("unable to set up inproc", xc);

nano_eval_res = R_BlankScalarString;

for (;;) {
if ((xc = nng_recvmsg(s, &msg, 0)))
fatal("inproc recvmsg", xc);

const char *body = nng_msg_body(msg);
nano_buf buf;

nano_parse_eval_res = R_BlankScalarString;
R_ToplevelExec(parse_eval_safe, (void *) body);

if (TYPEOF(nano_eval_res) == STRSXP) {
const char *string = NANO_STRING(nano_eval_res);
if (TYPEOF(nano_parse_eval_res) == STRSXP) {
const char *string = NANO_STRING(nano_parse_eval_res);
buf.buf = (unsigned char *) string;
buf.cur = strlen(string);
} else {
nano_serialize(&buf, nano_eval_res, R_NilValue);
nano_serialize(&buf, nano_parse_eval_res, R_NilValue);
}
nng_msg_clear(msg);
nng_msg_append(msg, buf.buf, buf.cur);
nano_eval_res = R_BlankScalarString;
if ((xc = nng_sendmsg(s, msg, 0)))
fatal("inproc sendmsg", xc);

Expand Down

0 comments on commit 77b4e36

Please sign in to comment.