]> gitweb.michael.orlitzky.com - libsvgtiny.git/commitdiff
src/svgtiny.c: add assertions to unlikely error paths libcss
authorMichael Orlitzky <michael@orlitzky.com>
Thu, 24 Oct 2024 12:02:18 +0000 (08:02 -0400)
committerMichael Orlitzky <michael@orlitzky.com>
Thu, 24 Oct 2024 12:21:35 +0000 (08:21 -0400)
While cleaning up our select_ctx at the end of svgtiny_parse(), we
loop through and get/remove/destroy any sheets it may have. If any of
these operations fails, we catch and propagate the error. However, we
only get/remove/destroy sheets that the libcss API has just told us
are valid. As a result, these error paths should never be taken. To
emphasize that, we add some assertions to the error paths in debug
builds. Production builds will still catch and propagate.

src/svgtiny.c

index 53ced0afd13a543b4652734047f801555b1e1b50..6938131e2f21ee8c54e295cc9cb04cdc4d00091e 100644 (file)
@@ -808,6 +808,12 @@ cleanup:
                                                        n_sheets - 1 - i,
                                                        &sheet);
                if (css_code != CSS_OK) {
+                       /* The API just told us that there were n_sheets
+                        * valid sheets. So while we attempt to handle
+                        * the error gracefully in production builds,
+                        * this should never happen. */
+                       assert(0);
+
                        if (code == svgtiny_OK) {
                                code = svgtiny_LIBCSS_ERROR;
                        }
@@ -821,6 +827,11 @@ cleanup:
                 * will not try to access them. */
                css_code = css_select_ctx_remove_sheet(state.select_ctx, sheet);
                if (css_code != CSS_OK) {
+                       /* Same as the assert() above. This sheet arose
+                        * from a call to css_select_ctx_get_sheet() a
+                        * moment ago, it should be valid! */
+                       assert(0);
+
                        if (code == svgtiny_OK) {
                                code = svgtiny_LIBCSS_ERROR;
                        }
@@ -831,8 +842,14 @@ cleanup:
                        continue;
                }
                css_code = css_stylesheet_destroy((css_stylesheet*)sheet);
-               if (css_code != CSS_OK && code == svgtiny_OK) {
-                 code = svgtiny_LIBCSS_ERROR;
+               if (css_code != CSS_OK) {
+                       /* Once more, "sheet" should have been valid
+                        * and un-destroyed. */
+                       assert(0);
+
+                       if (code == svgtiny_OK) {
+                               code = svgtiny_LIBCSS_ERROR;
+                       }
                }
        }
        css_code = css_select_ctx_destroy(state.select_ctx);