Fixed some issues with truncating strings in exif_entry_get_value
authorDan Fandrich <dan@coneharvesters.com>
Thu, 1 Feb 2018 00:26:54 +0000 (01:26 +0100)
committerDan Fandrich <dan@coneharvesters.com>
Fri, 1 Jun 2018 17:42:45 +0000 (19:42 +0200)
If the buffer size provided forced the result to be truncated, the
truncation was not always properly performed. test-values.c was enhanced
to check proper truncation in a much wider variety of cases.

libexif/exif-entry.c
test/test-value.c

index bb42473..7edd130 100644 (file)
@@ -438,7 +438,7 @@ exif_entry_fix (ExifEntry *e)
  * \pre The ExifEntry is already a member of an ExifData.
  * \param[in] e EXIF entry
  * \param[out] val buffer in which to store value
- * \param[in] maxlen one less than the length of the buffer val
+ * \param[in] maxlen the length of the buffer val
  */
 static void
 exif_entry_format_value(ExifEntry *e, char *val, size_t maxlen)
@@ -456,7 +456,6 @@ exif_entry_format_value(ExifEntry *e, char *val, size_t maxlen)
 
        if (!e->size || !maxlen)
                return;
-       ++maxlen; /* include the terminating NUL */
        switch (e->format) {
        case EXIF_FORMAT_UNDEFINED:
                snprintf (val, maxlen, _("%i bytes undefined data"), e->size);
@@ -866,9 +865,9 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                return val;
 
        /* make sure the returned string is zero terminated */
+       /* FIXME: this is inefficient in the case of long buffers and should
+        * instead be taken care of on each write instead. */
        memset (val, 0, maxlen);
-       maxlen--;
-       memset (b, 0, sizeof (b));
 
        /* We need the byte order */
        o = exif_data_get_byte_order (e->parent->parent);
@@ -904,11 +903,11 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                 * NULL terminated.
                 */
                if ((e->size >= 8) && !memcmp (e->data, "ASCII\0\0\0", 8)) {
-                       strncpy (val, (char *) e->data + 8, MIN (e->size - 8, maxlen));
+                       strncpy (val, (char *) e->data + 8, MIN (e->size - 8, maxlen-1));
                        break;
                }
                if ((e->size >= 8) && !memcmp (e->data, "UNICODE\0", 8)) {
-                       strncpy (val, _("Unsupported UNICODE string"), maxlen);
+                       strncpy (val, _("Unsupported UNICODE string"), maxlen-1);
                /* FIXME: use iconv to convert into the locale encoding.
                 * EXIF 2.2 implies (but does not say) that this encoding is
                 * UCS-2.
@@ -916,7 +915,7 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                        break;
                }
                if ((e->size >= 8) && !memcmp (e->data, "JIS\0\0\0\0\0", 8)) {
-                       strncpy (val, _("Unsupported JIS string"), maxlen);
+                       strncpy (val, _("Unsupported JIS string"), maxlen-1);
                /* FIXME: use iconv to convert into the locale encoding */
                        break;
                }
@@ -944,7 +943,7 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
        case EXIF_TAG_EXIF_VERSION:
                CF (e, EXIF_FORMAT_UNDEFINED, val, maxlen);
                CC (e, 4, val, maxlen);
-               strncpy (val, _("Unknown Exif Version"), maxlen);
+               strncpy (val, _("Unknown Exif Version"), maxlen-1);
                for (i = 0; *versions[i].label; i++) {
                        if (!memcmp (e->data, versions[i].label, 4)) {
                                snprintf (val, maxlen,
@@ -959,11 +958,11 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                CF (e, EXIF_FORMAT_UNDEFINED, val, maxlen);
                CC (e, 4, val, maxlen);
                if (!memcmp (e->data, "0100", 4))
-                       strncpy (val, _("FlashPix Version 1.0"), maxlen);
+                       strncpy (val, _("FlashPix Version 1.0"), maxlen-1);
                else if (!memcmp (e->data, "0101", 4))
-                       strncpy (val, _("FlashPix Version 1.01"), maxlen);
+                       strncpy (val, _("FlashPix Version 1.01"), maxlen-1);
                else
-                       strncpy (val, _("Unknown FlashPix Version"), maxlen);
+                       strncpy (val, _("Unknown FlashPix Version"), maxlen-1);
                break;
        case EXIF_TAG_COPYRIGHT:
                CF (e, EXIF_FORMAT_ASCII, val, maxlen);
@@ -974,14 +973,14 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                 * Remember that a corrupted tag might not be NUL-terminated
                 */
                if (e->size && e->data && match_repeated_char(e->data, ' ', e->size))
-                       strncpy (val, (char *) e->data, MIN (maxlen, e->size));
+                       strncpy (val, (char *) e->data, MIN (maxlen-1, e->size));
                else
-                       strncpy (val, _("[None]"), maxlen);
-               strncat (val, " ", maxlen - strlen (val));
-               strncat (val, _("(Photographer)"), maxlen - strlen (val));
+                       strncpy (val, _("[None]"), maxlen-1);
+               strncat (val, " ", maxlen-1 - strlen (val));
+               strncat (val, _("(Photographer)"), maxlen-1 - strlen (val));
 
                /* Second part: Editor. */
-               strncat (val, " - ", maxlen - strlen (val));
+               strncat (val, " - ", maxlen-1 - strlen (val));
                k = 0;
                if (e->size && e->data) {
                        const unsigned char *tagdata = memchr(e->data, 0, e->size);
@@ -989,15 +988,15 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                                int editor_ofs = tagdata - e->data;
                                int remaining = e->size - editor_ofs;
                                if (match_repeated_char(tagdata, ' ', remaining)) {
-                                       strncat (val, (const char*)tagdata, MIN (maxlen - strlen (val), remaining));
+                                       strncat (val, (const char*)tagdata, MIN (maxlen-1 - strlen (val), remaining));
                                        ++k;
                                }
                        }
                }
                if (!k)
-                       strncat (val, _("[None]"), maxlen - strlen (val));
-               strncat (val, " ", maxlen - strlen (val));
-               strncat (val, _("(Editor)"), maxlen - strlen (val));
+                       strncat (val, _("[None]"), maxlen-1 - strlen (val));
+               strncat (val, " ", maxlen-1 - strlen (val));
+               strncat (val, _("(Editor)"), maxlen-1 - strlen (val));
 
                break;
        case EXIF_TAG_FNUMBER:
@@ -1023,8 +1022,7 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                d = (double) v_rat.numerator / (double) v_rat.denominator;
                snprintf (val, maxlen, _("%.02f EV"), d);
                snprintf (b, sizeof (b), _(" (f/%.01f)"), pow (2, d / 2.));
-               if (maxlen > strlen (val) + strlen (b))
-                       strncat (val, b, maxlen - strlen (val));
+               strncat (val, b, maxlen-1 - strlen (val));
                break;
        case EXIF_TAG_FOCAL_LENGTH:
                CF (e, EXIF_FORMAT_RATIONAL, val, maxlen);
@@ -1059,11 +1057,12 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                        snprintf (b, sizeof (b), _(" (35 equivalent: %d mm)"),
                                  (int) (d * (double) v_rat.numerator /
                                             (double) v_rat.denominator));
+               else
+                       b[0] = 0;
 
                d = (double) v_rat.numerator / (double) v_rat.denominator;
                snprintf (val, maxlen, "%.1f mm", d);
-               if (maxlen > strlen (val) + strlen (b))
-                       strncat (val, b, maxlen - strlen (val));
+               strncat (val, b, maxlen-1 - strlen (val));
                break;
        case EXIF_TAG_SUBJECT_DISTANCE:
                CF (e, EXIF_FORMAT_RATIONAL, val, maxlen);
@@ -1089,8 +1088,7 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                        snprintf (val, maxlen, _("1/%i"), (int) (0.5 + 1. / d));
                else
                        snprintf (val, maxlen, "%i", (int) d);
-               if (maxlen > strlen (val) + strlen (_(" sec.")))
-                       strncat (val, _(" sec."), maxlen - strlen (val));
+               strncat (val, _(" sec."), maxlen-1 - strlen (val));
                break;
        case EXIF_TAG_SHUTTER_SPEED_VALUE:
                CF (e, EXIF_FORMAT_SRATIONAL, val, maxlen);
@@ -1107,7 +1105,7 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                  snprintf (b, sizeof (b), _(" (1/%d sec.)"), (int) (1. / d));
                else
                  snprintf (b, sizeof (b), _(" (%d sec.)"), (int) d);
-               strncat (val, b, maxlen - strlen (val));
+               strncat (val, b, maxlen-1 - strlen (val));
                break;
        case EXIF_TAG_BRIGHTNESS_VALUE:
                CF (e, EXIF_FORMAT_SRATIONAL, val, maxlen);
@@ -1121,15 +1119,14 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                snprintf (val, maxlen, _("%.02f EV"), d);
                snprintf (b, sizeof (b), _(" (%.02f cd/m^2)"),
                        1. / (M_PI * 0.3048 * 0.3048) * pow (2, d));
-               if (maxlen > strlen (val) + strlen (b))
-                       strncat (val, b, maxlen - strlen (val));
+               strncat (val, b, maxlen-1 - strlen (val));
                break;
        case EXIF_TAG_FILE_SOURCE:
                CF (e, EXIF_FORMAT_UNDEFINED, val, maxlen);
                CC (e, 1, val, maxlen);
                v_byte = e->data[0];
                if (v_byte == 3)
-                       strncpy (val, _("DSC"), maxlen);
+                       strncpy (val, _("DSC"), maxlen-1);
                else
                        snprintf (val, maxlen, _("Internal error (unknown "
                                  "value %i)"), v_byte);
@@ -1148,9 +1145,9 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                        case 6: c = _("B"); break;
                        default: c = _("Reserved"); break;
                        }
-                       strncat (val, c, maxlen - strlen (val));
+                       strncat (val, c, maxlen-1 - strlen (val));
                        if (i < 3)
-                               strncat (val, " ", maxlen - strlen (val));
+                               strncat (val, " ", maxlen-1 - strlen (val));
                }
                break;
        case EXIF_TAG_EXPOSURE_BIAS_VALUE:
@@ -1169,7 +1166,7 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                CC (e, 1, val, maxlen);
                v_byte = e->data[0];
                if (v_byte == 1)
-                       strncpy (val, _("Directly photographed"), maxlen);
+                       strncpy (val, _("Directly photographed"), maxlen-1);
                else
                        snprintf (val, maxlen, _("Internal error (unknown "
                                  "value %i)"), v_byte);
@@ -1182,9 +1179,9 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                        e->data + exif_format_get_size (e->format),
                        o);
                if ((v_short == 2) && (v_short2 == 1))
-                       strncpy (val, _("YCbCr4:2:2"), maxlen);
+                       strncpy (val, _("YCbCr4:2:2"), maxlen-1);
                else if ((v_short == 2) && (v_short2 == 2))
-                       strncpy (val, _("YCbCr4:2:0"), maxlen);
+                       strncpy (val, _("YCbCr4:2:0"), maxlen-1);
                else
                        snprintf (val, maxlen, "%u, %u", v_short, v_short2);
                break;
@@ -1227,20 +1224,17 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                CC (e, 4, val, maxlen);
                v_byte = e->data[0];
                snprintf (val, maxlen, "%u", v_byte);
-               maxlen -= strlen (val);
                for (i = 1; i < e->components; i++) {
                        v_byte = e->data[i];
                        snprintf (b, sizeof (b), ".%u", v_byte);
-                       strncat (val, b, maxlen);
-                       maxlen -= strlen (b);
-                       if ((signed)maxlen <= 0) break;
+                       strncat (val, b, maxlen-1 - strlen (val));
                }
                break;
        case EXIF_TAG_INTEROPERABILITY_VERSION:
        /* a.k.a. case EXIF_TAG_GPS_LATITUDE: */
                /* This tag occurs in EXIF_IFD_INTEROPERABILITY */
                if (e->format == EXIF_FORMAT_UNDEFINED) {
-                       strncpy (val, (char *) e->data, MIN (maxlen, e->size));
+                       strncpy (val, (char *) e->data, MIN (maxlen-1, e->size));
                        break;
                }
                /* EXIF_TAG_GPS_LATITUDE is the same numerically as
@@ -1254,9 +1248,9 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                CC (e, 1, val, maxlen);
                v_byte = e->data[0];
                if (v_byte == 0)
-                       strncpy (val, _("Sea level"), maxlen);
+                       strncpy (val, _("Sea level"), maxlen-1);
                else if (v_byte == 1)
-                       strncpy (val, _("Sea level reference"), maxlen);
+                       strncpy (val, _("Sea level reference"), maxlen-1);
                else
                        snprintf (val, maxlen, _("Internal error (unknown "
                                  "value %i)"), v_byte);
@@ -1328,7 +1322,7 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                for (k = 0; list2[i].elem[j].values[k]; k++) {
                        size_t l = strlen (_(list2[i].elem[j].values[k]));
                        if ((maxlen > l) && (strlen (val) < l))
-                               strncpy (val, _(list2[i].elem[j].values[k]), maxlen);
+                               strncpy (val, _(list2[i].elem[j].values[k]), maxlen-1);
                }
                if (!val[0]) snprintf (val, maxlen, "%i", v_short);
 
@@ -1366,7 +1360,7 @@ exif_entry_get_value (ExifEntry *e, char *val, unsigned int maxlen)
                else if (!*list[i].strings[j])
                        snprintf (val, maxlen, _("Unknown value %i"), v_short);
                else
-                       strncpy (val, _(list[i].strings[j]), maxlen);
+                       strncpy (val, _(list[i].strings[j]), maxlen-1);
                break;
 
        case EXIF_TAG_XP_TITLE:
index 6772739..557bb3a 100644 (file)
@@ -1,5 +1,9 @@
 /* test-value.c
  *
+ * Creates all the types of tags supported in exif_entry_initialize() and
+ * ensures that exif_entry_get_value() properly truncates the output of each
+ * one according to the buffer size available.
+ *
  * Copyright 2002 Lutz Mueller <lutz@users.sourceforge.net>
  *
  * This library is free software; you can redistribute it and/or
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+
+/*
+ * List of tags to test, one per default initialized type.
+ * There should be one for every block in exif_entry_initialize() and
+ * exif_entry_get_value().
+ */
+ExifTag trunc_test_tags[] = {
+       EXIF_TAG_PIXEL_X_DIMENSION,
+       EXIF_TAG_SUBJECT_LOCATION,
+       EXIF_TAG_IMAGE_WIDTH,
+       EXIF_TAG_ORIENTATION,
+       EXIF_TAG_SAMPLES_PER_PIXEL,
+       EXIF_TAG_BITS_PER_SAMPLE,
+       EXIF_TAG_X_RESOLUTION,
+       EXIF_TAG_WHITE_POINT,
+       EXIF_TAG_REFERENCE_BLACK_WHITE,
+       EXIF_TAG_DATE_TIME,
+       EXIF_TAG_IMAGE_DESCRIPTION,
+       EXIF_TAG_EXIF_VERSION,
+       EXIF_TAG_FLASH_PIX_VERSION,
+       EXIF_TAG_COPYRIGHT,
+       EXIF_TAG_FILE_SOURCE,
+       EXIF_TAG_COMPONENTS_CONFIGURATION,
+       EXIF_TAG_SCENE_TYPE,
+       EXIF_TAG_YCBCR_SUB_SAMPLING,
+       EXIF_TAG_PLANAR_CONFIGURATION,
+};
+
+/*
+ * These tags produce different outputs depending on the amount of buffer space
+ * available.
+ */
+ExifTag nonuniform_test_tags[] = {
+       EXIF_TAG_RESOLUTION_UNIT,
+       EXIF_TAG_COLOR_SPACE,
+       EXIF_TAG_METERING_MODE,
+};
+
+/*
+ * These tags need a nonzero rational number to be interesting.
+ * They must have space for a rational or srational created automatically by
+ * exif_entry_initialize().
+ */
+ExifTag rational_test_tags[] = {
+       EXIF_TAG_FNUMBER,
+       EXIF_TAG_APERTURE_VALUE,
+       EXIF_TAG_MAX_APERTURE_VALUE,
+       EXIF_TAG_FOCAL_LENGTH,
+       EXIF_TAG_SUBJECT_DISTANCE,
+       EXIF_TAG_EXPOSURE_TIME,
+       EXIF_TAG_SHUTTER_SPEED_VALUE,
+       EXIF_TAG_BRIGHTNESS_VALUE,
+       EXIF_TAG_EXPOSURE_BIAS_VALUE,
+};
+
+/*
+ * Verify that the entry is properly truncated to the buffer length within
+ * exif_entry_get_value().  If uniform is zero, then only check that the
+ * resulting string fits within the buffer and don't check its content.
+ */
+static void check_entry_trunc(ExifEntry *e, int uniform)
+{
+       unsigned int i;
+       char v[1024], full[1024];  /* Large enough to never truncate output */
+
+       printf ("Tag 0x%x\n", (int) e->tag);
+
+       /* Get the full, untruncated string to use as the expected value */
+       exif_entry_get_value (e, full, sizeof(full));
+       printf ("Full: '%s'\n", full);
+
+       for (i = strlen(full); i > 0; i--) {
+               /* Make sure the buffer isn't NUL-terminated to begin with */
+               memset(v, '*', sizeof(v));
+               exif_entry_get_value (e, v, i);
+               /* Truncate the full string by one on each iteration */
+               full[i-1] = '\0';
+               if ((strlen(v) >= i) || (uniform && strcmp(full, v))) {
+                       printf("Bad truncation!\n");
+                       printf("Length %2i: '%s'\n", i, v);
+                       exit(1);
+               }
+       }
+}
 
 int
 main ()
 {
-       ExifData *d;
+       ExifData *data;
        ExifEntry *e;
-       char v[1024];
-       ExifSRational r = {1., 20.};
-       unsigned int i;
+       ExifMem *mem;
+       unsigned i;
+       static const ExifSRational r = {1., 20.};  /* a nonzero number */
+       static const char user_comment[] = "ASCII\0\0\0A Long User Comment";
+       static const char xp_comment[] = "U\0C\0S\0-\0002\0 \0C\0o\0m\0m\0e\0n\0t\0";
+       static const char interop[] = "R98";
+       static const char subsec[] = "130 ";
+       static const ExifRational gpsh = {12., 1.};
+       static const ExifRational gpsm = {34., 1.};
+       static const ExifRational gpss = {56780., 1000.};
 
-       d = exif_data_new ();
-       if (!d) {
+       data = exif_data_new ();
+       if (!data) {
                printf ("Error running exif_data_new()\n");
                exit(13);
        }
 
-       e = exif_entry_new ();
+       /* Full initialization/truncation tests */
+       for (i=0; i < sizeof(trunc_test_tags)/sizeof(trunc_test_tags[0]); ++i) {
+               e = exif_entry_new ();
+               if (!e) {
+                       printf ("Error running exif_entry_new()\n");
+                       exit(13);
+               }
+               exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
+               exif_entry_initialize (e, trunc_test_tags[i]);
+               check_entry_trunc(e, 1);
+               exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
+               exif_entry_unref (e);
+       }
+
+       /* Nonuniform initialization/truncation tests */
+       for (i=0; i < sizeof(nonuniform_test_tags)/sizeof(nonuniform_test_tags[0]);
+                ++i) {
+               e = exif_entry_new ();
+               if (!e) {
+                       printf ("Error running exif_entry_new()\n");
+                       exit(13);
+               }
+               exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
+               exif_entry_initialize (e, nonuniform_test_tags[i]);
+               check_entry_trunc(e, 0);
+               exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
+               exif_entry_unref (e);
+       }
+
+       /* Rational number initialization/truncation tests */
+       for (i=0; i < sizeof(rational_test_tags)/sizeof(rational_test_tags[0]);
+                ++i) {
+               e = exif_entry_new ();
+               if (!e) {
+                       printf ("Error running exif_entry_new()\n");
+                       exit(13);
+               }
+               exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
+               exif_entry_initialize (e, rational_test_tags[i]);
+               exif_set_srational (e->data, exif_data_get_byte_order (data), r);
+               /* In case this tag needs an unsigned rational instead,
+                * fix the type automatically */
+               exif_entry_fix (e);
+               check_entry_trunc(e, 1);
+               exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
+               exif_entry_unref (e);
+       }
+
+       /* Create a memory allocator to manage the remaining ExifEntry structs */
+       mem = exif_mem_new_default();
+       if (!mem) {
+               printf ("Out of memory\n");
+               exit(13);
+       }
+
+       /* EXIF_TAG_SUB_SEC_TIME initialization/truncation tests */
+       e = exif_entry_new_mem (mem);
+       if (!e) {
+               printf ("Out of memory\n");
+               exit(13);
+       }
+       exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
+       exif_entry_initialize (e, EXIF_TAG_SUB_SEC_TIME);
+       e->size = sizeof(subsec);  /* include NUL */
+       e->components = e->size;
+       /* Allocate memory to use for holding the tag data */
+       e->data = exif_mem_alloc(mem, e->size);
+       if (!e->data) {
+               printf ("Out of memory\n");
+               exit(13);
+       }
+       memcpy(e->data, subsec, e->size);
+       check_entry_trunc(e, 1);
+       exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
+       exif_entry_unref (e);
+
+       /* EXIF_TAG_USER_COMMENT initialization/truncation tests */
+       e = exif_entry_new_mem (mem);
+       if (!e) {
+               printf ("Out of memory\n");
+               exit(13);
+       }
+       exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
+       exif_entry_initialize (e, EXIF_TAG_USER_COMMENT);
+       e->size = sizeof(user_comment) - 1;
+       e->components = e->size;
+       /* Allocate memory to use for holding the tag data */
+       e->data = exif_mem_alloc(mem, e->size);
+       if (!e->data) {
+               printf ("Out of memory\n");
+               exit(13);
+       }
+       memcpy(e->data, user_comment, e->size);
+       check_entry_trunc(e, 1);
+       exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
+       exif_entry_unref (e);
+
+       /* EXIF_TAG_XP_COMMENT truncation tests */
+       e = exif_entry_new_mem (mem);
        if (!e) {
-               printf ("Error running exif_entry_new()\n");
+               printf ("Out of memory\n");
+               exit(13);
+       }
+       exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
+       exif_entry_initialize (e, EXIF_TAG_XP_COMMENT);
+       e->format = EXIF_FORMAT_BYTE;
+       e->size = sizeof(xp_comment) - 1;
+       e->components = e->size;
+       /* Allocate memory to use for holding the tag data */
+       e->data = exif_mem_alloc(mem, e->size);
+       if (!e->data) {
+               printf ("Out of memory\n");
                exit(13);
        }
+       memcpy(e->data, xp_comment, e->size);
+       check_entry_trunc(e, 1);
+       exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
+       exif_entry_unref (e);
 
-       exif_content_add_entry (d->ifd[EXIF_IFD_0], e);
-       exif_entry_initialize (e, EXIF_TAG_SHUTTER_SPEED_VALUE);
-       exif_set_srational (e->data, exif_data_get_byte_order (d), r);
+       /* EXIF_TAG_INTEROPERABILITY_VERSION truncation tests */
+       e = exif_entry_new_mem (mem);
+       if (!e) {
+               printf ("Out of memory\n");
+               exit(13);
+       }
+       exif_content_add_entry (data->ifd[EXIF_IFD_INTEROPERABILITY], e);
+       exif_entry_initialize (e, EXIF_TAG_INTEROPERABILITY_VERSION);
+       e->format = EXIF_FORMAT_UNDEFINED;  /* The spec says ASCII, but libexif
+                                              allows UNDEFINED */
+       e->size = sizeof(interop);  /* include NUL */
+       e->components = e->size;
+       /* Allocate memory to use for holding the tag data */
+       e->data = exif_mem_alloc(mem, e->size);
+       if (!e->data) {
+               printf ("Out of memory\n");
+               exit(13);
+       }
+       memcpy(e->data, interop, e->size);
+       check_entry_trunc(e, 1);
+       exif_content_remove_entry (data->ifd[EXIF_IFD_INTEROPERABILITY], e);
+       exif_entry_unref (e);
 
-       for (i = 30; i > 0; i--) {
-               printf ("Length %2i: '%s'\n", i, 
-                       exif_entry_get_value (e, v, i));
+       /* EXIF_TAG_GPS_VERSION_ID truncation tests */
+       e = exif_entry_new_mem (mem);
+       if (!e) {
+               printf ("Out of memory\n");
+               exit(13);
        }
+       exif_content_add_entry (data->ifd[EXIF_IFD_GPS], e);
+       exif_entry_initialize (e, EXIF_TAG_GPS_VERSION_ID);
+       e->format = EXIF_FORMAT_BYTE;
+       e->size = 4;
+       e->components = e->size;
+       /* Allocate memory to use for holding the tag data */
+       e->data = exif_mem_alloc(mem, e->size);
+       if (!e->data) {
+               printf ("Out of memory\n");
+               exit(13);
+       }
+       e->data[0] = 2;
+       e->data[1] = 2;
+       e->data[2] = 0;
+       e->data[3] = 0;
+       check_entry_trunc(e, 1);
+       exif_content_remove_entry (data->ifd[EXIF_IFD_GPS], e);
+       exif_entry_unref (e);
 
+       /* EXIF_TAG_GPS_ALTITUDE_REF truncation tests */
+       e = exif_entry_new_mem (mem);
+       if (!e) {
+               printf ("Out of memory\n");
+               exit(13);
+       }
+       exif_content_add_entry (data->ifd[EXIF_IFD_GPS], e);
+       exif_entry_initialize (e, EXIF_TAG_GPS_ALTITUDE_REF);
+       e->format = EXIF_FORMAT_BYTE;
+       e->size = 1;
+       e->components = e->size;
+       /* Allocate memory to use for holding the tag data */
+       e->data = exif_mem_alloc(mem, e->size);
+       if (!e->data) {
+               printf ("Out of memory\n");
+               exit(13);
+       }
+       e->data[0] = 1;
+       check_entry_trunc(e, 1);
+       exif_content_remove_entry (data->ifd[EXIF_IFD_GPS], e);
+       exif_entry_unref (e);
+
+       /* EXIF_TAG_GPS_TIME_STAMP truncation tests */
+       e = exif_entry_new_mem (mem);
+       if (!e) {
+               printf ("Out of memory\n");
+               exit(13);
+       }
+       exif_content_add_entry (data->ifd[EXIF_IFD_GPS], e);
+       exif_entry_initialize (e, EXIF_TAG_GPS_TIME_STAMP);
+       e->format = EXIF_FORMAT_RATIONAL;
+       e->components = 3;
+       e->size = e->components * exif_format_get_size(EXIF_FORMAT_RATIONAL);
+       /* Allocate memory to use for holding the tag data */
+       e->data = exif_mem_alloc(mem, e->size);
+       if (!e->data) {
+               printf ("Out of memory\n");
+               exit(13);
+       }
+       exif_set_rational(e->data, exif_data_get_byte_order (data), gpsh);
+       exif_set_rational(e->data+8, exif_data_get_byte_order (data), gpsm);
+       exif_set_rational(e->data+16, exif_data_get_byte_order (data), gpss);
+       check_entry_trunc(e, 1);
+       exif_content_remove_entry (data->ifd[EXIF_IFD_GPS], e);
        exif_entry_unref (e);
-       exif_data_unref (d);
+
+       /* EXIF_TAG_SUBJECT_AREA truncation tests */
+       e = exif_entry_new_mem (mem);
+       if (!e) {
+               printf ("Out of memory\n");
+               exit(13);
+       }
+       exif_content_add_entry (data->ifd[EXIF_IFD_0], e);
+       exif_entry_initialize (e, EXIF_TAG_SUBJECT_AREA);
+       e->format = EXIF_FORMAT_SHORT;
+       /* This tags is interpreted differently depending on # components */
+       /* Rectangle */
+       e->components = 4;
+       e->size = e->components * exif_format_get_size(EXIF_FORMAT_SHORT);
+       /* Allocate memory to use for holding the tag data */
+       e->data = exif_mem_alloc(mem, e->size);
+       if (!e->data) {
+               printf ("Out of memory\n");
+               exit(13);
+       }
+       exif_set_short(e->data, exif_data_get_byte_order (data), 123);
+       exif_set_short(e->data+2, exif_data_get_byte_order (data), 456);
+       exif_set_short(e->data+4, exif_data_get_byte_order (data), 78);
+       exif_set_short(e->data+6, exif_data_get_byte_order (data), 90);
+       check_entry_trunc(e, 1);
+       /* Circle */
+       e->components = 3;
+       e->size = e->components * exif_format_get_size(EXIF_FORMAT_SHORT);
+       check_entry_trunc(e, 1);
+       /* Centre */
+       e->components = 2;
+       e->size = e->components * exif_format_get_size(EXIF_FORMAT_SHORT);
+       check_entry_trunc(e, 1);
+       /* Invalid */
+       e->components = 1;
+       e->size = e->components * exif_format_get_size(EXIF_FORMAT_SHORT);
+       check_entry_trunc(e, 1);
+       exif_content_remove_entry (data->ifd[EXIF_IFD_0], e);
+       exif_entry_unref (e);
+
+       exif_mem_unref(mem);
+       exif_data_unref (data);
 
        return 0;
 }