/* Make sure we allow paths like '~/.i3/layout.json' */
path = resolve_tilde(path);
- json_content_t content = json_determine_content(path);
+ char *buf = NULL;
+ ssize_t len;
+ if ((len = slurp(path, &buf)) < 0) {
+ /* slurp already logged an error. */
+ goto out;
+ }
+
+ if (!json_validate(buf, len)) {
+ ELOG("Could not parse \"%s\" as JSON, not loading.\n", path);
+ yerror("Could not parse \"%s\" as JSON.", path);
+ goto out;
+ }
+
+ json_content_t content = json_determine_content(buf, len);
LOG("JSON content = %d\n", content);
if (content == JSON_CONTENT_UNKNOWN) {
ELOG("Could not determine the contents of \"%s\", not loading.\n", path);
yerror("Could not determine the contents of \"%s\".", path);
- free(path);
- return;
+ goto out;
}
Con *parent = focused;
}
DLOG("Appending to parent=%p instead of focused=%p\n", parent, focused);
char *errormsg = NULL;
- tree_append_json(parent, path, &errormsg);
+ tree_append_json(parent, buf, len, &errormsg);
if (errormsg != NULL) {
yerror(errormsg);
free(errormsg);
if (content == JSON_CONTENT_WORKSPACE)
ipc_send_workspace_event("restored", parent, NULL);
- free(path);
cmd_output->needs_tree_render = true;
+out:
+ free(path);
+ free(buf);
}
/*