Skip to content

Commit

Permalink
Calculate buffer len correctly.
Browse files Browse the repository at this point in the history
  • Loading branch information
johanatan committed Jun 27, 2016
1 parent 8aad101 commit 788abbe
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions planck-c/shell.c
Expand Up @@ -92,7 +92,7 @@ struct SystemResult {
char* stderr;
};

static JSValueRef result_to_value_ref(JSContextRef ctx, struct SystemResult* result) {
static JSObjectRef result_to_object_ref(JSContextRef ctx, struct SystemResult* result) {
JSValueRef arguments[3];
arguments[0] = JSValueMakeNumber(ctx, result->status);
arguments[1] = c_string_to_value(ctx, result->stdout);
Expand Down Expand Up @@ -127,13 +127,14 @@ static struct SystemResult* wait_for_child(struct ThreadParams* params) {
if (params->cb_idx == -1)
return &params->res;
else {
const char* format = "window.do_async_sh_callback(%d);";
char buffer[sizeof(format) + sizeof("21474836") + 1];
snprintf(buffer, sizeof(buffer), format, params->cb_idx);
const char format[] = "function() { window.do_async_sh_callback(%d); }();";
const int len = sizeof(format) + sizeof("21474836");
char buffer[len];
snprintf(buffer, len, format, params->cb_idx);
printf("format: %s\n", buffer);
JSValueRef result = result_to_value_ref(params->ctx, &params->res);
JSObjectRef result = result_to_object_ref(params->ctx, &params->res);
JSStringRef bufferStr = (JSStringRef)c_string_to_value(params->ctx, buffer);
JSEvaluateScript(params->ctx, bufferStr, (JSObjectRef)result, NULL, 0, NULL);
JSEvaluateScript(params->ctx, bufferStr, result, NULL, 0, NULL);
JSStringRelease(bufferStr);
free(bufferStr);
free(params);
Expand Down Expand Up @@ -194,7 +195,7 @@ static JSValueRef system_call(JSContextRef ctx, char* cmd, char** env, char* dir
free(cmd); free(env); free(dir);

if (cb_idx != -1) return JSValueMakeNull(ctx);
else return result_to_value_ref(ctx, res);
else return (JSValueRef)result_to_object_ref(ctx, res);
}
return JSValueMakeNull(ctx);
}
Expand Down

0 comments on commit 788abbe

Please sign in to comment.