From: Michael Orlitzky Date: Thu, 24 Oct 2024 12:02:18 +0000 (-0400) Subject: src/svgtiny.c: add assertions to unlikely error paths X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=refs%2Fheads%2Flibcss;p=libsvgtiny.git src/svgtiny.c: add assertions to unlikely error paths 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. --- diff --git a/src/svgtiny.c b/src/svgtiny.c index 53ced0a..6938131 100644 --- a/src/svgtiny.c +++ b/src/svgtiny.c @@ -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);