From 1e12591f7b5701d6f2eb9ddf4be716fbe50d537b Mon Sep 17 00:00:00 2001 From: Sergey Alexandrovich Date: Wed, 18 Oct 2023 23:16:54 +0600 Subject: [PATCH] Fix saving EXIF strings with invalid UTF8 encoding (#3720) --- libvips/foreign/exif.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/libvips/foreign/exif.c b/libvips/foreign/exif.c index 1b7cc77a6b..b11b604331 100644 --- a/libvips/foreign/exif.c +++ b/libvips/foreign/exif.c @@ -296,7 +296,10 @@ vips_exif_to_s(ExifData *ed, ExifEntry *entry, VipsBuf *buf) int iv; ExifRational rv; ExifSRational srv; - char txt[256]; + char txt[256], *value; + + value = g_utf8_make_valid( + exif_entry_get_value(entry, txt, 256), -1); if (entry->format == EXIF_FORMAT_ASCII) { /* libexif does not null-terminate strings. Copy out and add @@ -306,7 +309,10 @@ vips_exif_to_s(ExifData *ed, ExifEntry *entry, VipsBuf *buf) memcpy(txt, entry->data, len); txt[len] = '\0'; - vips_buf_appendf(buf, "%s ", txt); + + char *utf8 = g_utf8_make_valid(txt, -1); + vips_buf_appendf(buf, "%s ", utf8); + g_free(utf8); } else if (entry->components < 10 && !vips_exif_get_int(ed, entry, 0, &iv)) { @@ -332,14 +338,15 @@ vips_exif_to_s(ExifData *ed, ExifEntry *entry, VipsBuf *buf) } } else - vips_buf_appendf(buf, "%s ", - exif_entry_get_value(entry, txt, 256)); + vips_buf_appendf(buf, "%s ", value); vips_buf_appendf(buf, "(%s, %s, %lu components, %d bytes)", - exif_entry_get_value(entry, txt, 256), + value, exif_format_get_name(entry->format), entry->components, entry->size); + + g_free(value); } typedef struct _VipsExifParams {