From f638237a6b4f3d3edf9bdd995c15df537f8d7e7c Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Fri, 24 Jan 2025 18:14:52 -0500 Subject: Fix multiple bugs encountered during stress test - Basic server resource cleanup Signed-off-by: Andrew Opalach --- src/util/color_palette.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'src/util') diff --git a/src/util/color_palette.c b/src/util/color_palette.c index 73dfc83..9cfdf32 100644 --- a/src/util/color_palette.c +++ b/src/util/color_palette.c @@ -40,31 +40,40 @@ bool camu_color_palette_init(str *path) if (!nn_file_open(&file, path, NNWT_FILE_READONLY)) { return false; } + str s; nn_file_read_as_str(&file, &s); nn_file_close(&file); json_error_t error; json_t *root = json_loadb(s.data, s.length, 0, &error); + al_str_free(&s); if (!root) { - al_log_error("color_palette", "Failed to load %.*s as a json (%s).", - al_str_fmt(path), error.text); + al_log_error("color_palette", "Failed to load %.*s as a json (%s).", al_str_fmt(path), error.text); return false; } + + bool parsed = true; + json_t *special = json_object_get(root, "special"); if (!special) { al_log_error("color_palette", "\"special\" field missing from the color palette."); - return false; + parsed = false; + goto out; } + json_t *colors = json_object_get(root, "colors"); if (!colors) { al_log_error("color_palette", "\"colors\" field missing from the color palette."); - return false; + parsed = false; + goto out; } + json_t *object; const char *color; u32 value; u32 index; bool to_long_error; + for (u32 i = 0; i < ARRAY_SIZE(special_colors); i++) { object = json_object_get(special, special_colors[i]); if (!object) { @@ -80,6 +89,7 @@ bool camu_color_palette_init(str *path) al_log_warn("color_palette", "%s is not a valid hex color.", normal_colors[i]); } } + for (u32 i = 0; i < ARRAY_SIZE(normal_colors); i++) { object = json_object_get(colors, normal_colors[i]); if (!object) { @@ -95,8 +105,13 @@ bool camu_color_palette_init(str *path) al_log_warn("color_palette", "%s is not a valid hex color.", normal_colors[i]); } } + +out: + json_decref(root); + + return parsed; #else (void)path; + return false; #endif - return true; } -- cgit v1.2.3-101-g0448