improve NULL-safety - fixes #690

Sun, 22 Jun 2025 11:15:53 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 22 Jun 2025 11:15:53 +0200
changeset 162
d3598c834f9b
parent 161
4c8b9c6d241f
child 163
3628cc3c0483

improve NULL-safety - fixes #690

src/2d.c file | annotate | diff | comparison | revisions
src/ascension/input.h file | annotate | diff | comparison | revisions
src/ascension/shader.h file | annotate | diff | comparison | revisions
src/ascension/ui/font.h file | annotate | diff | comparison | revisions
src/behavior.c file | annotate | diff | comparison | revisions
src/shader.c file | annotate | diff | comparison | revisions
src/sprite.c file | annotate | diff | comparison | revisions
src/text.c file | annotate | diff | comparison | revisions
--- a/src/2d.c	Sat Jun 21 22:10:54 2025 +0200
+++ b/src/2d.c	Sun Jun 22 11:15:53 2025 +0200
@@ -65,9 +65,8 @@
         return NULL;
     }
     AscRectangleShader *shader = asc_shader_create(codes, sizeof(*shader));
-    if (asc_has_error()) {
+    if (shader == NULL) {
         asc_shader_free_codes(codes);
-        // TODO: must not return NULL or must add error handling on caller site
         return NULL;
     }
     shader->size = glGetUniformLocation(shader->program.gl_id, "size");
--- a/src/ascension/input.h	Sat Jun 21 22:10:54 2025 +0200
+++ b/src/ascension/input.h	Sun Jun 22 11:15:53 2025 +0200
@@ -32,11 +32,11 @@
 
 typedef struct asc_input_s {
     /**
-     * The last X position of the mouse in \c mouse_window.
+     * The last X position of the mouse in @c mouse_window.
      */
     int mouse_x;
     /**
-     * The last Y position of the mouse in \c mouse_window.
+     * The last Y position of the mouse in @c mouse_window.
      */
     int mouse_y;
     /**
--- a/src/ascension/shader.h	Sat Jun 21 22:10:54 2025 +0200
+++ b/src/ascension/shader.h	Sun Jun 22 11:15:53 2025 +0200
@@ -166,7 +166,7 @@
  * @param id the custom ID of the shader
  * @param create_func the function that creates the shader
  * @param create_flags flags passed to create_func
- * @return the shader created by the @c create_func
+ * @return the shader created by the @c create_func or @c NULL when shader compilation failed
  * @see asc_shader_lookup()
  */
 const void *asc_shader_register(unsigned int id, asc_shader_create_func create_func, int create_flags);
@@ -196,7 +196,7 @@
  * @param id the custom ID of the shader
  * @param create_func the function to create the shader
  * @param create_flags flags passed to create_func
- * @return the found shader or the newly created shader
+ * @return the found shader or the newly created shader or @c NULL when shader compilation failed
  * @see asc_shader_lookup()
  * @see asc_shader_register()
  */
--- a/src/ascension/ui/font.h	Sat Jun 21 22:10:54 2025 +0200
+++ b/src/ascension/ui/font.h	Sun Jun 22 11:15:53 2025 +0200
@@ -73,7 +73,7 @@
  * Ascension UI elements are loading the required fonts for you.
  *
  * @param font the font description
- * @return a pointer to the SDL TTF font handle, or \c NULL on error
+ * @return a pointer to the SDL TTF font handle, or @c NULL on error
  */
 TTF_Font *asc_font_load(AscFont font);
 
--- a/src/behavior.c	Sat Jun 21 22:10:54 2025 +0200
+++ b/src/behavior.c	Sun Jun 22 11:15:53 2025 +0200
@@ -27,12 +27,13 @@
 
 #include "ascension/behavior.h"
 #include "ascension/context.h"
-#include "ascension/error.h"
 #include "ascension/scene.h"
 
 #include <cx/array_list.h>
 #include <cx/tree.h>
 
+#include <assert.h>
+
 static void asc_behavior_destroy(void *b) {
     AscBehavior *behavior = b;
     if (behavior->destroy_func) {
@@ -52,11 +53,7 @@
         node->behaviors = asc_behavior_new_list();
     }
     AscBehavior *behavior = cxListEmplace(node->behaviors);
-    if (behavior == NULL) {
-        const cxstring node_name = asc_scene_node_get_name(node);
-        asc_error("Failed to add behavior to scene node %"CX_PRIstr, CX_SFMT(node_name));
-        return NULL;
-    }
+    assert(behavior != NULL);
     behavior->enabled = true;
     behavior->node = node;
     behavior->func = args.func;
--- a/src/shader.c	Sat Jun 21 22:10:54 2025 +0200
+++ b/src/shader.c	Sun Jun 22 11:15:53 2025 +0200
@@ -155,19 +155,27 @@
     AscShaderProgram *prog = cxZallocDefault(mem_size);
     unsigned shader[2];
     unsigned n = 0;
+    bool shader_compile_error = false;
+    // TODO: clean up this pp mess by introducing proper nested structs
     if (codes.vtx) {
-        shader[n++] = asc_shader_compile(GL_VERTEX_SHADER, codes.vtx, codes.vtx_pp, codes.vtx_pp_list, codes.vtx_pp_list_select);
+        shader[n] = asc_shader_compile(GL_VERTEX_SHADER, codes.vtx, codes.vtx_pp, codes.vtx_pp_list, codes.vtx_pp_list_select);
+        shader_compile_error |= shader[n] == 0;
+        n++;
     }
     if (codes.frag) {
-        shader[n++] = asc_shader_compile(GL_FRAGMENT_SHADER, codes.frag, codes.frag_pp, codes.frag_pp_list, codes.frag_pp_list_select);
+        shader[n] = asc_shader_compile(GL_FRAGMENT_SHADER, codes.frag, codes.frag_pp, codes.frag_pp_list, codes.frag_pp_list_select);
+        shader_compile_error |= shader[n] == 0;
+        n++;
     }
-    if (asc_shader_link(shader, n, prog)) {
+    if (shader_compile_error || asc_shader_link(shader, n, prog)) {
         cxFreeDefault(prog);
         prog = NULL;
     }
     for (unsigned i = 0; i < n; i++) {
-        asc_dprintf("Delete shader: %u", shader[i]);
-        glDeleteShader(shader[i]);
+        if (shader[i] > 0) {
+            asc_dprintf("Delete shader: %u", shader[i]);
+            glDeleteShader(shader[i]);
+        }
     }
     return prog;
 }
--- a/src/sprite.c	Sat Jun 21 22:10:54 2025 +0200
+++ b/src/sprite.c	Sun Jun 22 11:15:53 2025 +0200
@@ -53,7 +53,7 @@
         return NULL;
     }
     AscSpriteShader *shader = asc_shader_create(codes, sizeof(*shader));
-    if (asc_has_error()) {
+    if (shader == NULL) {
         asc_shader_free_codes(codes);
         return NULL;
     }
--- a/src/text.c	Sat Jun 21 22:10:54 2025 +0200
+++ b/src/text.c	Sun Jun 22 11:15:53 2025 +0200
@@ -40,6 +40,7 @@
 
     // Render text onto a surface
     TTF_Font *font = asc_font_load(text->font);
+    if (font == NULL) return;
     static int alignments[] = {
             TTF_WRAPPED_ALIGN_LEFT,
             TTF_WRAPPED_ALIGN_CENTER,

mercurial