diff options
author | Emmanuele Bassi <ebassi@gmail.com> | 2020-11-10 02:26:56 +0000 |
---|---|---|
committer | Emmanuele Bassi <ebassi@gmail.com> | 2020-11-10 02:26:56 +0000 |
commit | b462b3d8160cbcf2d9a6ccb8cd40b102a29a6059 (patch) | |
tree | 21f5114e7427698155a5713a6906e2a02892333a | |
parent | 70b28ccb557f9a61b80fb490df9a7e943c836d8c (diff) | |
parent | 9d0b9d0eb6a4fb8781584c817b95e2aa2e750542 (diff) | |
download | gdk-pixbuf-b462b3d8160cbcf2d9a6ccb8cd40b102a29a6059.tar.gz |
Merge branch 'static-analysis' into 'master'
Rough pass at making the static analysis tool happy
See merge request GNOME/gdk-pixbuf!89
-rw-r--r-- | .clang-format | 20 | ||||
-rw-r--r-- | gdk-pixbuf/gdk-pixbuf-io.c | 9 | ||||
-rw-r--r-- | gdk-pixbuf/io-png.c | 534 | ||||
-rw-r--r-- | gdk-pixbuf/io-xbm.c | 9 |
4 files changed, 290 insertions, 282 deletions
diff --git a/.clang-format b/.clang-format index 13fd0fbd4..4e9958657 100644 --- a/.clang-format +++ b/.clang-format @@ -1,9 +1,25 @@ # See https://wiki.apertis.org/Guidelines/Coding_conventions#Code_formatting -BasedOnStyle: GNU +Language: Cpp +BasedOnStyle: GNU +IndentWidth: 8 AlwaysBreakAfterDefinitionReturnType: All -BreakBeforeBinaryOperators: None +BreakBeforeBinaryOperators: true BinPackParameters: false SpaceAfterCStyleCast: true +IndentCaseLabels: false +IndentCaseBlocks: false +# BitFieldColonSpacing: Both +BreakBeforeBraces: Custom +AlignAfterOpenBracket: Align +BraceWrapping: + AfterCaseLabel: false + AfterEnum: true + AfterFunction: true + AfterStruct: true + AfterUnion: true + BeforeElse: false + BeforeWhile: false + IndentBraces: false # Our column limit is actually 80, but setting that results in clang-format # making a lot of dubious hanging-indent choices; disable it and assume the # developer will line wrap appropriately. clang-format will still check diff --git a/gdk-pixbuf/gdk-pixbuf-io.c b/gdk-pixbuf/gdk-pixbuf-io.c index 58bb74879..40eb920c8 100644 --- a/gdk-pixbuf/gdk-pixbuf-io.c +++ b/gdk-pixbuf/gdk-pixbuf-io.c @@ -396,7 +396,7 @@ gdk_pixbuf_io_init_modules (const char *filename, GString *tmp_buf = g_string_new (NULL); gboolean have_error = FALSE; GdkPixbufModule *module = NULL; - int flags; + int flags = 0; int n_patterns = 0; GdkPixbufModulePattern *pattern; GError *local_error = NULL; @@ -428,8 +428,7 @@ gdk_pixbuf_io_init_modules (const char *filename, line_buf[term] = 0; if (!skip_space (&p)) { - /* Blank line marking the end of a module - */ + /* Blank line marking the end of a module */ if (module && *p != '#') { file_formats = g_slist_prepend (file_formats, module); module = NULL; @@ -442,8 +441,7 @@ gdk_pixbuf_io_init_modules (const char *filename, goto next_line; if (!module) { - /* Read a module location - */ + /* Read a module location */ module = g_new0 (GdkPixbufModule, 1); n_patterns = 0; @@ -464,6 +462,7 @@ gdk_pixbuf_io_init_modules (const char *filename, module->info->name = g_strdup (tmp_buf->str); module->module_name = module->info->name; + flags = 0; if (!scan_int (&p, &flags)) { g_warning ("Error parsing loader info in '%s'\n %s", filename, line_buf); diff --git a/gdk-pixbuf/io-png.c b/gdk-pixbuf/io-png.c index 3778f3469..26130fddb 100644 --- a/gdk-pixbuf/io-png.c +++ b/gdk-pixbuf/io-png.c @@ -885,300 +885,288 @@ png_save_to_callback_flush_func (png_structp png_ptr) ; } -static gboolean real_save_png (GdkPixbuf *pixbuf, - gchar **keys, - gchar **values, - GError **error, - gboolean to_callback, - FILE *f, - GdkPixbufSaveFunc save_func, - gpointer user_data) +static gboolean +real_save_png (GdkPixbuf *pixbuf, + int n_keys, + gchar **keys, + gchar **values, + GError **error, + gboolean to_callback, + FILE *f, + GdkPixbufSaveFunc save_func, + gpointer user_data) { - png_structp png_ptr = NULL; - png_infop info_ptr; - png_textp text_ptr = NULL; - guchar *ptr; - guchar *pixels; - int y; - int i; - png_bytep row_ptr; - png_color_8 sig_bit; - int w, h, rowstride; - int has_alpha; - int bpc; - int num_keys; - int compression = -1; - int x_density = 0; - int y_density = 0; - gboolean success = TRUE; - guchar *icc_profile = NULL; - gsize icc_profile_size = 0; - SaveToFunctionIoPtr to_callback_ioptr; - - num_keys = 0; - - if (keys && *keys) { - gchar **kiter = keys; - gchar **viter = values; - - while (*kiter) { - if (strncmp (*kiter, "tEXt::", 6) == 0) { - gchar *key = *kiter + 6; - int len = strlen (key); - if (len < 1 || len > 79) { - g_set_error_literal (error, - GDK_PIXBUF_ERROR, - GDK_PIXBUF_ERROR_BAD_OPTION, - _("Keys for PNG text chunks must have at least 1 and at most 79 characters.")); - success = FALSE; - goto cleanup; - } - for (i = 0; i < len; i++) { - if ((guchar) key[i] > 127) { - g_set_error_literal (error, - GDK_PIXBUF_ERROR, - GDK_PIXBUF_ERROR_BAD_OPTION, - _("Keys for PNG text chunks must be ASCII characters.")); - success = FALSE; - goto cleanup; - } - } - num_keys++; - } else if (strcmp (*kiter, "icc-profile") == 0) { - /* decode from base64 */ - icc_profile = g_base64_decode (*viter, &icc_profile_size); - if (icc_profile_size < 127) { - /* This is a user-visible error */ - g_set_error (error, - GDK_PIXBUF_ERROR, - GDK_PIXBUF_ERROR_BAD_OPTION, - _("Color profile has invalid length %d."), - (gint)icc_profile_size); - success = FALSE; - goto cleanup; - } - } else if (strcmp (*kiter, "compression") == 0) { - char *endptr = NULL; - compression = strtol (*viter, &endptr, 10); - - if (endptr == *viter) { - g_set_error (error, - GDK_PIXBUF_ERROR, - GDK_PIXBUF_ERROR_BAD_OPTION, - _("PNG compression level must be a value between 0 and 9; value “%s” could not be parsed."), - *viter); - success = FALSE; - goto cleanup; - } - if (compression < 0 || compression > 9) { - /* This is a user-visible error; - * lets people skip the range-checking - * in their app. - */ - g_set_error (error, - GDK_PIXBUF_ERROR, - GDK_PIXBUF_ERROR_BAD_OPTION, - _("PNG compression level must be a value between 0 and 9; value “%d” is not allowed."), - compression); - success = FALSE; - goto cleanup; - } - } else if (strcmp (*kiter, "x-dpi") == 0) { - char *endptr = NULL; - x_density = strtol (*viter, &endptr, 10); - if (endptr == *viter) - x_density = -1; - - if (x_density <= 0) { - /* This is a user-visible error; - * lets people skip the range-checking - * in their app. - */ - g_set_error (error, - GDK_PIXBUF_ERROR, - GDK_PIXBUF_ERROR_BAD_OPTION, - _("PNG x-dpi must be greater than zero; value “%s” is not allowed."), - *viter); - - success = FALSE; - goto cleanup; - } - } else if (strcmp (*kiter, "y-dpi") == 0) { - char *endptr = NULL; - y_density = strtol (*viter, &endptr, 10); - if (endptr == *viter) - y_density = -1; - - if (y_density <= 0) { - /* This is a user-visible error; - * lets people skip the range-checking - * in their app. - */ - g_set_error (error, - GDK_PIXBUF_ERROR, - GDK_PIXBUF_ERROR_BAD_OPTION, - _("PNG y-dpi must be greater than zero; value “%s” is not allowed."), - *viter); - - success = FALSE; - goto cleanup; - } - } else { - g_warning ("Unrecognized parameter (%s) passed to PNG saver.", *kiter); - } - - ++kiter; - ++viter; - } - } - - if (num_keys > 0) { - gchar **kiter = keys; - gchar **viter = values; - - text_ptr = g_new0 (png_text, num_keys); - for (i = 0; i < num_keys; i++) { - if (strncmp (*kiter, "tEXt::", 6) != 0) { - kiter++; - viter++; - } - - text_ptr[i].compression = PNG_TEXT_COMPRESSION_NONE; - text_ptr[i].key = *kiter + 6; - text_ptr[i].text = g_convert (*viter, -1, - "ISO-8859-1", "UTF-8", - NULL, &text_ptr[i].text_length, - NULL); + png_structp png_ptr = NULL; + png_infop info_ptr; + guchar *ptr; + guchar *pixels; + int y; + png_bytep row_ptr; + png_color_8 sig_bit; + int w, h, rowstride; + int has_alpha; + int bpc; + int compression = -1; + int x_density = 0; + int y_density = 0; + gboolean success = TRUE; + guchar *icc_profile = NULL; + gsize icc_profile_size = 0; + SaveToFunctionIoPtr to_callback_ioptr; + int num_keys = 0; + png_textp text_ptr = NULL; + GArray *text_data = NULL; + + text_data = g_array_sized_new (FALSE, TRUE, sizeof (png_text), n_keys); + + for (int i = 0; i < n_keys; i++) { + const char *key = keys[i]; + const char *value = values[i]; + + if (strncmp (key, "tEXt::", 6) == 0) { + const char *unprefixed_key = key + 6; + int len = strlen (unprefixed_key); + png_text text; + + if (len < 1 || len > 79) { + /* Translators notice: '%s' is the name of the + * PNG text key + */ + g_set_error (error, GDK_PIXBUF_ERROR, + GDK_PIXBUF_ERROR_BAD_OPTION, + _("Invalid key “%s”. Keys for PNG text chunks must have at least 1 and at most 79 characters."), + unprefixed_key); + success = FALSE; + goto cleanup; + } + + for (int i = 0; i < len; i++) { + if ((guchar) unprefixed_key[i] > 127) { + /* Translators notice: '%s' is the name of + * the PNG text key + */ + g_set_error (error, GDK_PIXBUF_ERROR, + GDK_PIXBUF_ERROR_BAD_OPTION, + _("Invalid key “%s”. Keys for PNG text chunks must be ASCII characters."), + unprefixed_key); + success = FALSE; + goto cleanup; + } + } + + text.compression = PNG_TEXT_COMPRESSION_NONE; + text.key = unprefixed_key; + text.text = g_convert (value, -1, + "ISO-8859-1", "UTF-8", + NULL, + &text.text_length, + NULL); #ifdef PNG_iTXt_SUPPORTED - if (!text_ptr[i].text) { - text_ptr[i].compression = PNG_ITXT_COMPRESSION_NONE; - text_ptr[i].text = g_strdup (*viter); - text_ptr[i].text_length = 0; - text_ptr[i].itxt_length = strlen (text_ptr[i].text); - text_ptr[i].lang = NULL; - text_ptr[i].lang_key = NULL; - } + if (text.text == NULL) { + text.compression = PNG_ITXT_COMPRESSION_NONE; + text.text = g_strdup (value); + text.text_length = 0; + text.itxt_length = strlen (value); + text.lang = NULL; + text.lang_key = NULL; + } #endif - if (!text_ptr[i].text) { - gint j; - g_set_error (error, + if (text.text == NULL) { + g_set_error (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_BAD_OPTION, - _("Value for PNG text chunk %s cannot be converted to ISO-8859-1 encoding."), *kiter + 6); - for (j = 0; j < i; j++) - g_free (text_ptr[j].text); - g_free (text_ptr); - return FALSE; - } - - kiter++; - viter++; - } - } - - bpc = gdk_pixbuf_get_bits_per_sample (pixbuf); - w = gdk_pixbuf_get_width (pixbuf); - h = gdk_pixbuf_get_height (pixbuf); - rowstride = gdk_pixbuf_get_rowstride (pixbuf); - has_alpha = gdk_pixbuf_get_has_alpha (pixbuf); - pixels = gdk_pixbuf_get_pixels (pixbuf); - - /* Guaranteed by the caller. */ - g_assert (w >= 0); - g_assert (h >= 0); - g_assert (rowstride >= 0); - - png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, - error, - png_simple_error_callback, - png_simple_warning_callback); - if (png_ptr == NULL) { - success = FALSE; - goto cleanup; - } - - info_ptr = png_create_info_struct (png_ptr); - if (info_ptr == NULL) { - success = FALSE; - goto cleanup; - } - if (setjmp (png_jmpbuf(png_ptr))) { - success = FALSE; - goto cleanup; - } - - if (num_keys > 0) { - png_set_text (png_ptr, info_ptr, text_ptr, num_keys); - } - - if (to_callback) { - to_callback_ioptr.save_func = save_func; - to_callback_ioptr.user_data = user_data; - to_callback_ioptr.error = error; - png_set_write_fn (png_ptr, &to_callback_ioptr, - png_save_to_callback_write_func, - png_save_to_callback_flush_func); - } else { - png_init_io (png_ptr, f); - } - - if (compression >= 0) - png_set_compression_level (png_ptr, compression); + _("Value for PNG text chunk '%s' cannot be converted to ISO-8859-1 encoding."), unprefixed_key); + success = FALSE; + goto cleanup; + } + + g_array_append_val (text_data, text); + } else if (strcmp (key, "icc-profile") == 0) { + icc_profile = g_base64_decode (value, &icc_profile_size); + + if (icc_profile_size < 127) { + g_set_error (error, GDK_PIXBUF_ERROR, + GDK_PIXBUF_ERROR_BAD_OPTION, + _("Color profile has invalid length %d"), + (int) icc_profile_size); + success = FALSE; + goto cleanup; + } + } else if (strcmp (key, "compression") == 0) { + char *endptr = NULL; + + compression = strtol (value, &endptr, 10); + if (endptr == value || (compression < 0 || compression > 9)) { + g_set_error (error, GDK_PIXBUF_ERROR, + GDK_PIXBUF_ERROR_BAD_OPTION, + _("PNG compression level must be a value between 0 and 9; value “%s” is invalid"), + value); + success = FALSE; + goto cleanup; + } + } else if (strcmp (key, "x-dpi") == 0 || strcmp (key, "y-dpi") == 0) { + gboolean is_horizontal = strcmp (key, "x-dpi") == 0; + char *endptr = NULL; + + int dpi = strtol (value, &endptr, 10); + + if (endptr == value || dpi <= 0) { + g_set_error (error, GDK_PIXBUF_ERROR, + GDK_PIXBUF_ERROR_BAD_OPTION, + _("PNG %s must be greater than zero; value “%s” is not allowed"), + is_horizontal ? "x-dpi" : "y-dpi", + value); + success = FALSE; + goto cleanup; + } + + if (is_horizontal) { + x_density = dpi; + } else { + y_density = dpi; + } + } else { + g_warning ("Unrecognized parameter “%s” passed to the PNG saver", key); + } + } + + bpc = gdk_pixbuf_get_bits_per_sample (pixbuf); + w = gdk_pixbuf_get_width (pixbuf); + h = gdk_pixbuf_get_height (pixbuf); + rowstride = gdk_pixbuf_get_rowstride (pixbuf); + has_alpha = gdk_pixbuf_get_has_alpha (pixbuf); + pixels = gdk_pixbuf_get_pixels (pixbuf); + + if (text_data->len > 0) { + num_keys = text_data->len; + text_ptr = (png_textp) g_array_free (text_data, FALSE); + text_data = NULL; + } else { + g_clear_pointer (&text_data, g_array_unref); + num_keys = 0; + text_ptr = NULL; + } + + /* Guaranteed by the caller. */ + g_assert (w >= 0); + g_assert (h >= 0); + g_assert (rowstride >= 0); + + png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING, + error, + png_simple_error_callback, + png_simple_warning_callback); + if (png_ptr == NULL) { + success = FALSE; + goto cleanup; + } + + info_ptr = png_create_info_struct (png_ptr); + if (info_ptr == NULL) { + success = FALSE; + goto cleanup; + } + + if (setjmp (png_jmpbuf (png_ptr))) { + success = FALSE; + goto cleanup; + } + + if (num_keys > 0) { + png_set_text (png_ptr, info_ptr, text_ptr, num_keys); + } + + if (to_callback) { + to_callback_ioptr.save_func = save_func; + to_callback_ioptr.user_data = user_data; + to_callback_ioptr.error = error; + png_set_write_fn (png_ptr, &to_callback_ioptr, + png_save_to_callback_write_func, + png_save_to_callback_flush_func); + } else { + png_init_io (png_ptr, f); + } + + if (compression >= 0) { + png_set_compression_level (png_ptr, compression); + } #ifdef PNG_pHYs_SUPPORTED - if (x_density > 0 && y_density > 0) - png_set_pHYs (png_ptr, info_ptr, DPI_TO_DPM (x_density), DPI_TO_DPM (y_density), PNG_RESOLUTION_METER); + if (x_density > 0 && y_density > 0) { + png_set_pHYs (png_ptr, info_ptr, + DPI_TO_DPM (x_density), + DPI_TO_DPM (y_density), + PNG_RESOLUTION_METER); + } #endif #if defined(PNG_iCCP_SUPPORTED) /* the proper ICC profile title is encoded in the profile */ if (icc_profile != NULL) { png_set_iCCP (png_ptr, info_ptr, - "ICC profile", PNG_COMPRESSION_TYPE_BASE, - (png_bytep) icc_profile, icc_profile_size); + "ICC profile", + PNG_COMPRESSION_TYPE_BASE, + (png_bytep) icc_profile, + icc_profile_size); } #endif - if (has_alpha) { - png_set_IHDR (png_ptr, info_ptr, w, h, bpc, - PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); - } else { - png_set_IHDR (png_ptr, info_ptr, w, h, bpc, - PNG_COLOR_TYPE_RGB, PNG_INTERLACE_NONE, - PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); - } - /* Note bpc is always 8 */ - sig_bit.red = bpc; - sig_bit.green = bpc; - sig_bit.blue = bpc; - sig_bit.alpha = bpc; - png_set_sBIT (png_ptr, info_ptr, &sig_bit); - png_write_info (png_ptr, info_ptr); - png_set_packing (png_ptr); - - for (y = 0, ptr = pixels; y < h; y++, ptr += rowstride) { - row_ptr = (png_bytep)ptr; - png_write_rows (png_ptr, &row_ptr, 1); - } - - png_write_end (png_ptr, info_ptr); + if (has_alpha) { + png_set_IHDR (png_ptr, info_ptr, w, h, bpc, + PNG_COLOR_TYPE_RGB_ALPHA, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_BASE, + PNG_FILTER_TYPE_BASE); + } else { + png_set_IHDR (png_ptr, info_ptr, w, h, bpc, + PNG_COLOR_TYPE_RGB, + PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_BASE, + PNG_FILTER_TYPE_BASE); + } + + /* Note bpc is always 8 */ + sig_bit.red = bpc; + sig_bit.green = bpc; + sig_bit.blue = bpc; + sig_bit.alpha = bpc; + png_set_sBIT (png_ptr, info_ptr, &sig_bit); + png_write_info (png_ptr, info_ptr); + png_set_packing (png_ptr); + + for (y = 0, ptr = pixels; y < h; y++, ptr += rowstride) { + row_ptr = (png_bytep)ptr; + png_write_rows (png_ptr, &row_ptr, 1); + } + + png_write_end (png_ptr, info_ptr); + + for (int i = 0; i < num_keys; i++) { + g_free (text_ptr[i].text); + } + + g_free (text_ptr); cleanup: - if (png_ptr != NULL) + if (png_ptr != NULL) { png_destroy_write_struct (&png_ptr, &info_ptr); + } - g_free (icc_profile); + if (text_data != NULL) { + for (guint i = 0; i < text_data->len; i++) { + png_textp text = &g_array_index (text_data, png_text, i); - if (text_ptr != NULL) { - for (i = 0; i < num_keys; i++) - g_free (text_ptr[i].text); - g_free (text_ptr); + g_free (text->text); + } + + g_array_unref (text_data); } - return success; + g_free (icc_profile); + + return success; } static gboolean @@ -1188,7 +1176,9 @@ gdk_pixbuf__png_image_save (FILE *f, gchar **values, GError **error) { - return real_save_png (pixbuf, keys, values, error, + int n_keys = keys != NULL ? g_strv_length (keys) : 0; + + return real_save_png (pixbuf, n_keys, keys, values, error, FALSE, f, NULL, NULL); } @@ -1200,7 +1190,9 @@ gdk_pixbuf__png_image_save_to_callback (GdkPixbufSaveFunc save_func, gchar **values, GError **error) { - return real_save_png (pixbuf, keys, values, error, + int n_keys = keys != NULL ? g_strv_length (keys) : 0; + + return real_save_png (pixbuf, n_keys, keys, values, error, TRUE, NULL, save_func, user_data); } diff --git a/gdk-pixbuf/io-xbm.c b/gdk-pixbuf/io-xbm.c index 5bf71e1bf..c78734cbb 100644 --- a/gdk-pixbuf/io-xbm.c +++ b/gdk-pixbuf/io-xbm.c @@ -304,7 +304,7 @@ gdk_pixbuf__xbm_image_load_real (FILE *f, { guint w, h; int x_hot, y_hot; - guchar *data, *ptr; + guchar *data = NULL, *ptr; guchar *pixels; guint row_stride; int x, y; @@ -324,6 +324,7 @@ gdk_pixbuf__xbm_image_load_real (FILE *f, pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB, FALSE, 8, w, h); if (pixbuf == NULL) { + g_free (data); g_set_error_literal (error, GDK_PIXBUF_ERROR, GDK_PIXBUF_ERROR_INSUFFICIENT_MEMORY, @@ -362,9 +363,9 @@ gdk_pixbuf__xbm_image_load_real (FILE *f, reg >>= 1; bits--; - pixels[x*3+0] = channel; - pixels[x*3+1] = channel; - pixels[x*3+2] = channel; + pixels[x * 3 + 0] = channel; + pixels[x * 3 + 1] = channel; + pixels[x * 3 + 2] = channel; } pixels += row_stride; } |