From 210d3e2aa89e2ae0593b4e70328f399fe0e76bb5 Mon Sep 17 00:00:00 2001 From: Michael Orlitzky Date: Sun, 6 Aug 2023 13:13:39 -0400 Subject: [PATCH] io-svg.c: clean up compiler warnings --- io-svg.c | 81 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 50 insertions(+), 31 deletions(-) diff --git a/io-svg.c b/io-svg.c index c08fa7d..d0c2a1b 100644 --- a/io-svg.c +++ b/io-svg.c @@ -1,9 +1,9 @@ -#include /* fprintf, printf */ -#include /* memcpy, memset, strstr */ +#include /* memcpy, memset */ #include #include #include /* includes glib.h */ +#include #include /* @@ -137,8 +137,10 @@ static diagram_t* svgtiny_diagram_from_buffer(char* buffer, code = svgtiny_parse(diagram, buffer, bytecount, "", width, height); - if (code != svgtiny_OK) { - switch (code) { + switch(code) { + case svgtiny_OK: + /* The one success case. */ + return diagram; case svgtiny_OUT_OF_MEMORY: g_set_error_literal(error, G_FILE_ERROR, @@ -165,11 +167,10 @@ static diagram_t* svgtiny_diagram_from_buffer(char* buffer, diagram->error_line, diagram->error_message); break; - } - return NULL; } - return diagram; + /* All other cases above are failure */ + return NULL; } /** @@ -194,9 +195,9 @@ static cairo_t* cairo_context_from_diagram(diagram_t* diagram) { crs = cairo_surface_status(surface); if (crs != CAIRO_STATUS_SUCCESS) { - fprintf(stderr, - "cairo_image_surface_create failed: %s\n", - cairo_status_to_string(crs)); + g_fprintf(stderr, + "cairo_image_surface_create failed: %s\n", + cairo_status_to_string(crs)); cairo_surface_destroy(surface); return NULL; } @@ -209,9 +210,9 @@ static cairo_t* cairo_context_from_diagram(diagram_t* diagram) { cairo_surface_destroy(surface); if (crs != CAIRO_STATUS_SUCCESS) { - fprintf(stderr, - "cairo_create failed: %s\n", - cairo_status_to_string(crs)); + g_fprintf(stderr, + "cairo_create failed: %s\n", + cairo_status_to_string(crs)); cairo_destroy(cr); return NULL; } @@ -251,9 +252,9 @@ static cairo_t* cairo_context_from_diagram(diagram_t* diagram) { /* Check the status again just for good measure? */ crs = cairo_status(cr); if (crs != CAIRO_STATUS_SUCCESS) { - fprintf(stderr, - "cairo error: %s\n", - cairo_status_to_string(crs)); + g_fprintf(stderr, + "cairo error: %s\n", + cairo_status_to_string(crs)); cairo_destroy(cr); return NULL; } @@ -354,10 +355,9 @@ static gboolean gdk_pixbuf_load_increment(gpointer data, const guchar* buf, guint buf_size, GError** error) { - size_t increment = 0; SvgTinyContext* context = (SvgTinyContext*)data; - /* YOLO, no error checking */ + /* YOLO, what is error checking? */ context->svg_data = g_realloc(context->svg_data, context->svg_data_size + buf_size); memcpy(context->svg_data + context->svg_data_size, buf, buf_size); @@ -426,13 +426,13 @@ static char* process_gtk_symbolic_svg_xinclude(const char* buffer, char* xi; char* xi_stop; - xi_start = strstr(buffer, XI_SIGNATURE); + xi_start = g_strstr_len(buffer, buf_size, XI_SIGNATURE); if (xi_start == NULL) { return NULL; } xi = xi_start + strlen(XI_SIGNATURE); - xi_stop = strstr(xi, "\""); + xi_stop = g_strstr_len(xi, (buffer + buf_size) - xi, "\""); if(xi_stop == NULL) { /* We found the start of an XInclude, but not the end of its base64-encoded data? Play it safe and do nothing. */ @@ -443,7 +443,15 @@ static char* process_gtk_symbolic_svg_xinclude(const char* buffer, "xi" into one */ *xi_stop = 0; gsize decoded_size; - guchar* decoded = g_base64_decode(xi, &decoded_size); + + /* All files are ASCII, right? In GTK's gdkpixbufutils.c, our base64 + * data is encoded from a binary file stream, i.e. bytes, without + * any regard for what the text inside represents. Elsewhere we are + * pretending that gchar is a reasonable data type to use for the + * contents of an SVG file; here we are saying that again out loud. + */ + gchar* decoded = (gchar*)g_base64_decode(xi, &decoded_size); + *xi_stop = '"'; /* We need another round of processing to strip the and * elements out of "decoded", but it's simpler to just overwrite @@ -453,9 +461,21 @@ static char* process_gtk_symbolic_svg_xinclude(const char* buffer, * element so much, because if one exists, it has to come before the * . As a result, we just need to strip everything up to the * leading tag. */ - guchar* svg_open_start = strstr(decoded, ""); - memset(decoded, ' ', (1 + (svg_open_end - decoded))); + gchar* svg_open_start = g_strstr_len(decoded, decoded_size, ""); + if (svg_open_end == NULL) { + /* Uhhhhh */ + g_free(decoded); + return NULL; + } + memset(decoded, ' ', (1 + (svg_open_end - decoded))); + } /* We're going to keep everything up to xi_start. If the code, - err->message); + g_fprintf(stderr, + "Error %d in gdk_pixbuf_new_from_file: %s\n", + err->code, + err->message); g_error_free(err); return 1; } -- 2.43.2