libtiff: Convert uses of _TIFFmalloc/realloc/calloc/free to the Ext functions

From 553bb4b2077ab97bac318a473acfabd4a5ade9ca Mon Sep 17 00:00:00 2001
From: Even Rouault <[EMAIL REDACTED]>
Date: Tue, 22 Nov 2022 18:18:10 +0100
Subject: [PATCH] Convert uses of _TIFFmalloc/realloc/calloc/free to the Ext
 functions

---
 libtiff/tif_aux.c       |  24 ++---
 libtiff/tif_close.c     |  22 ++--
 libtiff/tif_compress.c  |  16 +--
 libtiff/tif_dir.c       | 100 +++++++++--------
 libtiff/tif_dirinfo.c   |  12 +--
 libtiff/tif_dirread.c   | 234 ++++++++++++++++++++--------------------
 libtiff/tif_dirwrite.c  | 100 ++++++++---------
 libtiff/tif_extension.c |   4 +-
 libtiff/tif_fax3.c      |  10 +-
 libtiff/tif_getimage.c  |  52 ++++-----
 libtiff/tif_jpeg.c      |  34 +++---
 libtiff/tif_jpeg_12.c   |   2 +-
 libtiff/tif_lerc.c      |  30 +++---
 libtiff/tif_luv.c       |  10 +-
 libtiff/tif_lzma.c      |   4 +-
 libtiff/tif_lzw.c       |  14 +--
 libtiff/tif_ojpeg.c     |  80 +++++++-------
 libtiff/tif_packbits.c  |   4 +-
 libtiff/tif_pixarlog.c  |  54 +++++-----
 libtiff/tif_predict.c   |  14 +--
 libtiff/tif_print.c     |   6 +-
 libtiff/tif_read.c      |  20 ++--
 libtiff/tif_webp.c      |  16 +--
 libtiff/tif_write.c     |  24 ++---
 libtiff/tif_zip.c       |   4 +-
 libtiff/tif_zstd.c      |   4 +-
 libtiff/tiffiop.h       |   5 +
 27 files changed, 460 insertions(+), 439 deletions(-)

diff --git a/libtiff/tif_aux.c b/libtiff/tif_aux.c
index 6b800b28..0b60f136 100644
--- a/libtiff/tif_aux.c
+++ b/libtiff/tif_aux.c
@@ -103,7 +103,7 @@ _TIFFCheckRealloc(TIFF* tif, void* buffer,
 	 */
 	if (count != 0)
 	{
-		cp = _TIFFrealloc(buffer, count);
+		cp = _TIFFreallocExt(tif, buffer, count);
 	}
 
 	if (cp == NULL) {
@@ -123,7 +123,7 @@ _TIFFCheckMalloc(TIFF* tif, tmsize_t nmemb, tmsize_t elem_size, const char* what
 }
 
 static int
-TIFFDefaultTransferFunction(TIFFDirectory* td)
+TIFFDefaultTransferFunction(TIFF* tif, TIFFDirectory* td)
 {
 	uint16_t **tf = td->td_transferfunction;
 	tmsize_t i, n, nbytes;
@@ -134,7 +134,7 @@ TIFFDefaultTransferFunction(TIFFDirectory* td)
 
 	n = ((tmsize_t)1)<<td->td_bitspersample;
 	nbytes = n * sizeof (uint16_t);
-        tf[0] = (uint16_t *)_TIFFmalloc(nbytes);
+        tf[0] = (uint16_t *)_TIFFmallocExt(tif, nbytes);
 	if (tf[0] == NULL)
 		return 0;
 	tf[0][0] = 0;
@@ -144,11 +144,11 @@ TIFFDefaultTransferFunction(TIFFDirectory* td)
 	}
 
 	if (td->td_samplesperpixel - td->td_extrasamples > 1) {
-                tf[1] = (uint16_t *)_TIFFmalloc(nbytes);
+                tf[1] = (uint16_t *)_TIFFmallocExt(tif, nbytes);
 		if(tf[1] == NULL)
 			goto bad;
 		_TIFFmemcpy(tf[1], tf[0], nbytes);
-                tf[2] = (uint16_t *)_TIFFmalloc(nbytes);
+                tf[2] = (uint16_t *)_TIFFmallocExt(tif, nbytes);
 		if (tf[2] == NULL)
 			goto bad;
 		_TIFFmemcpy(tf[2], tf[0], nbytes);
@@ -157,21 +157,21 @@ TIFFDefaultTransferFunction(TIFFDirectory* td)
 
 bad:
 	if (tf[0])
-		_TIFFfree(tf[0]);
+		_TIFFfreeExt(tif, tf[0]);
 	if (tf[1])
-		_TIFFfree(tf[1]);
+		_TIFFfreeExt(tif, tf[1]);
 	if (tf[2])
-		_TIFFfree(tf[2]);
+		_TIFFfreeExt(tif, tf[2]);
 	tf[0] = tf[1] = tf[2] = 0;
 	return 0;
 }
 
 static int
-TIFFDefaultRefBlackWhite(TIFFDirectory* td)
+TIFFDefaultRefBlackWhite(TIFF* tif, TIFFDirectory* td)
 {
 	int i;
 
-        td->td_refblackwhite = (float *)_TIFFmalloc(6*sizeof (float));
+        td->td_refblackwhite = (float *)_TIFFmallocExt(tif, 6*sizeof (float));
 	if (td->td_refblackwhite == NULL)
 		return 0;
         if (td->td_photometric == PHOTOMETRIC_YCBCR) {
@@ -332,7 +332,7 @@ TIFFVGetFieldDefaulted(TIFF* tif, uint32_t tag, va_list ap)
 		}
 	case TIFFTAG_TRANSFERFUNCTION:
 		if (!td->td_transferfunction[0] &&
-		    !TIFFDefaultTransferFunction(td)) {
+		    !TIFFDefaultTransferFunction(tif, td)) {
 			TIFFErrorExtR(tif, tif->tif_name, "No space for \"TransferFunction\" tag");
 			return (0);
 		}
@@ -343,7 +343,7 @@ TIFFVGetFieldDefaulted(TIFF* tif, uint32_t tag, va_list ap)
 		}
 		return (1);
 	case TIFFTAG_REFERENCEBLACKWHITE:
-		if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(td))
+		if (!td->td_refblackwhite && !TIFFDefaultRefBlackWhite(tif, td))
 			return (0);
 		*va_arg(ap, const float **) = td->td_refblackwhite;
 		return (1);
diff --git a/libtiff/tif_close.c b/libtiff/tif_close.c
index 441b909b..50a9dd8b 100644
--- a/libtiff/tif_close.c
+++ b/libtiff/tif_close.c
@@ -53,9 +53,9 @@ TIFFCleanup(TIFF* tif)
 	TIFFFreeDirectory(tif);
 
 	if (tif->tif_dirlistoff)
-		_TIFFfree(tif->tif_dirlistoff);
+		_TIFFfreeExt(tif, tif->tif_dirlistoff);
 	if (tif->tif_dirlistdirn)
-		_TIFFfree(tif->tif_dirlistdirn);
+		_TIFFfreeExt(tif, tif->tif_dirlistdirn);
 
 	/*
          * Clean up client info links.
@@ -65,12 +65,12 @@ TIFFCleanup(TIFF* tif)
 		TIFFClientInfoLink *psLink = tif->tif_clientinfo;
 
 		tif->tif_clientinfo = psLink->next;
-		_TIFFfree( psLink->name );
-		_TIFFfree( psLink );
+		_TIFFfreeExt(tif, psLink->name );
+		_TIFFfreeExt(tif, psLink );
 	}
 
 	if (tif->tif_rawdata && (tif->tif_flags&TIFF_MYBUFFER))
-		_TIFFfree(tif->tif_rawdata);
+		_TIFFfreeExt(tif, tif->tif_rawdata);
 	if (isMapped(tif))
 		TIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size);
 
@@ -88,13 +88,13 @@ TIFFCleanup(TIFF* tif)
 					 *          Otherwise the following tags are also freed with the first free().
 					 */
 					TIFFFieldIsAnonymous(fld)) {
-					_TIFFfree(fld->field_name);
-					_TIFFfree(fld);
+					_TIFFfreeExt(tif, fld->field_name);
+					_TIFFfreeExt(tif, fld);
 				}
 			}
 		}
 
-		_TIFFfree(tif->tif_fields);
+		_TIFFfreeExt(tif, tif->tif_fields);
 	}
 
         if (tif->tif_nfieldscompat > 0) {
@@ -102,12 +102,12 @@ TIFFCleanup(TIFF* tif)
 
                 for (i = 0; i < tif->tif_nfieldscompat; i++) {
                         if (tif->tif_fieldscompat[i].allocated_size)
-                                _TIFFfree(tif->tif_fieldscompat[i].fields);
+                                _TIFFfreeExt(tif, tif->tif_fieldscompat[i].fields);
                 }
-                _TIFFfree(tif->tif_fieldscompat);
+                _TIFFfreeExt(tif, tif->tif_fieldscompat);
         }
 
-	_TIFFfree(tif);
+	_TIFFfreeExt(NULL, tif);
 }
 
 /************************************************************************/
diff --git a/libtiff/tif_compress.c b/libtiff/tif_compress.c
index 3a9efb75..2a155a61 100644
--- a/libtiff/tif_compress.c
+++ b/libtiff/tif_compress.c
@@ -200,7 +200,7 @@ TIFFCodec*
 TIFFRegisterCODEC(uint16_t scheme, const char* name, TIFFInitMethod init)
 {
 	codec_t* cd = (codec_t*)
-	    _TIFFmalloc((tmsize_t)(sizeof (codec_t) + sizeof (TIFFCodec) + strlen(name)+1));
+	    _TIFFmallocExt(NULL, (tmsize_t)(sizeof (codec_t) + sizeof (TIFFCodec) + strlen(name)+1));
 
 	if (cd != NULL) {
 		cd->info = (TIFFCodec*) ((uint8_t*) cd + sizeof (codec_t));
@@ -228,7 +228,7 @@ TIFFUnRegisterCODEC(TIFFCodec* c)
 	for (pcd = &registeredCODECS; (cd = *pcd) != NULL; pcd = &cd->next)
 		if (cd->info == c) {
 			*pcd = cd->next;
-			_TIFFfree(cd);
+			_TIFFfreeExt(NULL, cd);
 			return;
 		}
 	TIFFErrorExt(0, "TIFFUnRegisterCODEC",
@@ -258,9 +258,9 @@ TIFFGetConfiguredCODECs()
 
 	for (cd = registeredCODECS; cd; cd = cd->next) {
 		new_codecs = (TIFFCodec *)
-			_TIFFrealloc(codecs, i * sizeof(TIFFCodec));
+			_TIFFreallocExt(NULL, codecs, i * sizeof(TIFFCodec));
 		if (!new_codecs) {
-			_TIFFfree (codecs);
+			_TIFFfreeExt (NULL, codecs);
 			return NULL;
 		}
 		codecs = new_codecs;
@@ -270,9 +270,9 @@ TIFFGetConfiguredCODECs()
 	for (c = _TIFFBuiltinCODECS; c->name; c++) {
 		if (TIFFIsCODECConfigured(c->scheme)) {
 			new_codecs = (TIFFCodec *)
-				_TIFFrealloc(codecs, i * sizeof(TIFFCodec));
+				_TIFFreallocExt(NULL, codecs, i * sizeof(TIFFCodec));
 			if (!new_codecs) {
-				_TIFFfree (codecs);
+				_TIFFfreeExt (NULL, codecs);
 				return NULL;
 			}
 			codecs = new_codecs;
@@ -281,9 +281,9 @@ TIFFGetConfiguredCODECs()
 		}
 	}
 
-	new_codecs = (TIFFCodec *) _TIFFrealloc(codecs, i * sizeof(TIFFCodec));
+	new_codecs = (TIFFCodec *) _TIFFreallocExt(NULL, codecs, i * sizeof(TIFFCodec));
 	if (!new_codecs) {
-		_TIFFfree (codecs);
+		_TIFFfreeExt (NULL, codecs);
 		return NULL;
 	}
 	codecs = new_codecs;
diff --git a/libtiff/tif_dir.c b/libtiff/tif_dir.c
index 73fb5b32..a7d7a9ca 100644
--- a/libtiff/tif_dir.c
+++ b/libtiff/tif_dir.c
@@ -40,41 +40,57 @@
 #define DATATYPE_IEEEFP		3       /* !IEEE floating point data */
 
 static void
-setByteArray(void** vpp, const void* vp, size_t nmemb, size_t elem_size)
+setByteArray(TIFF* tif, void** vpp, const void* vp, size_t nmemb, size_t elem_size)
 {
 	if (*vpp) {
-		_TIFFfree(*vpp);
+		_TIFFfreeExt(tif, *vpp);
 		*vpp = 0;
 	}
 	if (vp) {
 		tmsize_t bytes = _TIFFMultiplySSize(NULL, nmemb, elem_size, NULL);
 		if (bytes)
-			*vpp = (void*) _TIFFmalloc(bytes);
+			*vpp = (void*) _TIFFmallocExt(tif, bytes);
 		if (*vpp)
 			_TIFFmemcpy(*vpp, vp, bytes);
 	}
 }
 void _TIFFsetByteArray(void** vpp, const void* vp, uint32_t n)
-    { setByteArray(vpp, vp, n, 1); }
-static void _TIFFsetNString(char** cpp, const char* cp, uint32_t n)
-    { setByteArray((void**) cpp, cp, n, 1); }
+    { setByteArray(NULL, vpp, vp, n, 1); }
+void _TIFFsetByteArrayExt(TIFF* tif, void** vpp, const void* vp, uint32_t n)
+    { setByteArray(tif, vpp, vp, n, 1); }
+
+static void _TIFFsetNString(TIFF* tif, char** cpp, const char* cp, uint32_t n)
+    { setByteArray(tif, (void**) cpp, cp, n, 1); }
+
 void _TIFFsetShortArray(uint16_t** wpp, const uint16_t* wp, uint32_t n)
-    { setByteArray((void**) wpp, wp, n, sizeof (uint16_t)); }
+    { setByteArray(NULL, (void**) wpp, wp, n, sizeof (uint16_t)); }
+void _TIFFsetShortArrayExt(TIFF* tif, uint16_t** wpp, const uint16_t* wp, uint32_t n)
+    { setByteArray(tif, (void**) wpp, wp, n, sizeof (uint16_t)); }
+
 void _TIFFsetLongArray(uint32_t** lpp, const uint32_t* lp, uint32_t n)
-    { setByteArray((void**) lpp, lp, n, sizeof (uint32_t)); }
-static void _TIFFsetLong8Array(uint64_t** lpp, const uint64_t* lp, uint32_t n)
-    { setByteArray((void**) lpp, lp, n, sizeof (uint64_t)); }
+    { setByteArray(NULL, (void**) lpp, lp, n, sizeof (uint32_t)); }
+void _TIFFsetLongArrayExt(TIFF* tif, uint32_t** lpp, const uint32_t* lp, uint32_t n)
+    { setByteArray(tif, (void**) lpp, lp, n, sizeof (uint32_t)); }
+
+static void _TIFFsetLong8Array(TIFF* tif, uint64_t** lpp, const uint64_t* lp, uint32_t n)
+    { setByteArray(tif, (void**) lpp, lp, n, sizeof (uint64_t)); }
+
 void _TIFFsetFloatArray(float** fpp, const float* fp, uint32_t n)
-    { setByteArray((void**) fpp, fp, n, sizeof (float)); }
+    { setByteArray(NULL, (void**) fpp, fp, n, sizeof (float)); }
+void _TIFFsetFloatArrayExt(TIFF* tif, float** fpp, const float* fp, uint32_t n)
+    { setByteArray(tif, (void**) fpp, fp, n, sizeof (float)); }
+
 void _TIFFsetDoubleArray(double** dpp, const double* dp, uint32_t n)
-    { setByteArray((void**) dpp, dp, n, sizeof (double)); }
+    { setByteArray(NULL, (void**) dpp, dp, n, sizeof (double)); }
+void _TIFFsetDoubleArrayExt(TIFF* tif, double** dpp, const double* dp, uint32_t n)
+    { setByteArray(tif, (void**) dpp, dp, n, sizeof (double)); }
 
 static void
-setDoubleArrayOneValue(double** vpp, double value, size_t nmemb)
+setDoubleArrayOneValue(TIFF* tif, double** vpp, double value, size_t nmemb)
 {
 	if (*vpp)
-		_TIFFfree(*vpp);
-	*vpp = _TIFFmalloc(nmemb*sizeof(double));
+		_TIFFfreeExt(tif, *vpp);
+	*vpp = _TIFFmallocExt(tif, nmemb*sizeof(double));
 	if (*vpp)
 	{
 		while (nmemb--)
@@ -124,12 +140,12 @@ setExtraSamples(TIFF* tif, va_list ap, uint32_t* v)
                     "ExtraSamples tag value is changing, "
                     "but TransferFunction was read with a different value. Canceling it");
                 TIFFClrFieldBit(tif,FIELD_TRANSFERFUNCTION);
-                _TIFFfree(td->td_transferfunction[0]);
+                _TIFFfreeExt(tif, td->td_transferfunction[0]);
                 td->td_transferfunction[0] = NULL;
         }
 
 	td->td_extrasamples = (uint16_t) *v;
-	_TIFFsetShortArray(&td->td_sampleinfo, va, td->td_extrasamples);
+	_TIFFsetShortArrayExt(tif, &td->td_sampleinfo, va, td->td_extrasamples);
 	return 1;
 
 #undef EXTRASAMPLE_COREL_UNASSALPHA
@@ -274,7 +290,7 @@ _TIFFVSetField(TIFF* tif, uint32_t tag, va_list ap)
                     "SamplesPerPixel tag value is changing, "
                     "but SMinSampleValue tag was read with a different value. Canceling it");
                 TIFFClrFieldBit(tif,FIELD_SMINSAMPLEVALUE);
-                _TIFFfree(td->td_sminsamplevalue);
+                _TIFFfreeExt(tif, td->td_sminsamplevalue);
                 td->td_sminsamplevalue = NULL;
             }
             if( td->td_smaxsamplevalue != NULL )
@@ -283,7 +299,7 @@ _TIFFVSetField(TIFF* tif, uint32_t tag, va_list ap)
                     "SamplesPerPixel tag value is changing, "
                     "but SMaxSampleValue tag was read with a different value. Canceling it");
                 TIFFClrFieldBit(tif,FIELD_SMAXSAMPLEVALUE);
-                _TIFFfree(td->td_smaxsamplevalue);
+                _TIFFfreeExt(tif, td->td_smaxsamplevalue);
                 td->td_smaxsamplevalue = NULL;
             }
             /* Test if 3 transfer functions instead of just one are now needed
@@ -295,7 +311,7 @@ _TIFFVSetField(TIFF* tif, uint32_t tag, va_list ap)
                         "SamplesPerPixel tag value is changing, "
                         "but TransferFunction was read with a different value. Canceling it");
                     TIFFClrFieldBit(tif,FIELD_TRANSFERFUNCTION);
-                    _TIFFfree(td->td_transferfunction[0]);
+                    _TIFFfreeExt(tif, td->td_transferfunction[0]);
                     td->td_transferfunction[0] = NULL;
             }
         }
@@ -319,15 +335,15 @@ _TIFFVSetField(TIFF* tif, uint32_t tag, va_list ap)
 		break;
 	case TIFFTAG_SMINSAMPLEVALUE:
 		if (tif->tif_flags & TIFF_PERSAMPLE)
-			_TIFFsetDoubleArray(&td->td_sminsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
+			_TIFFsetDoubleArrayExt(tif, &td->td_sminsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
 		else
-			setDoubleArrayOneValue(&td->td_sminsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
+			setDoubleArrayOneValue(tif, &td->td_sminsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
 		break;
 	case TIFFTAG_SMAXSAMPLEVALUE:
 		if (tif->tif_flags & TIFF_PERSAMPLE)
-			_TIFFsetDoubleArray(&td->td_smaxsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
+			_TIFFsetDoubleArrayExt(tif, &td->td_smaxsamplevalue, va_arg(ap, double*), td->td_samplesperpixel);
 		else
-			setDoubleArrayOneValue(&td->td_smaxsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
+			setDoubleArrayOneValue(tif, &td->td_smaxsamplevalue, va_arg(ap, double), td->td_samplesperpixel);
 		break;
 	case TIFFTAG_XRESOLUTION:
         dblval = va_arg(ap, double);
@@ -369,9 +385,9 @@ _TIFFVSetField(TIFF* tif, uint32_t tag, va_list ap)
 		break;
 	case TIFFTAG_COLORMAP:
 		v32 = (uint32_t)(1L << td->td_bitspersample);
-		_TIFFsetShortArray(&td->td_colormap[0], va_arg(ap, uint16_t*), v32);
-		_TIFFsetShortArray(&td->td_colormap[1], va_arg(ap, uint16_t*), v32);
-		_TIFFsetShortArray(&td->td_colormap[2], va_arg(ap, uint16_t*), v32);
+		_TIFFsetShortArrayExt(tif, &td->td_colormap[0], va_arg(ap, uint16_t*), v32);
+		_TIFFsetShortArrayExt(tif, &td->td_colormap[1], va_arg(ap, uint16_t*), v32);
+		_TIFFsetShortArrayExt(tif, &td->td_colormap[2], va_arg(ap, uint16_t*), v32);
 		break;
 	case TIFFTAG_EXTRASAMPLES:
 		if (!setExtraSamples(tif, ap, &v))
@@ -381,7 +397,7 @@ _TIFFVSetField(TIFF* tif, uint32_t tag, va_list ap)
 		td->td_extrasamples =  (((uint16_t) va_arg(ap, uint16_vap)) != 0);
 		if (td->td_extrasamples) {
 			uint16_t sv = EXTRASAMPLE_ASSOCALPHA;
-			_TIFFsetShortArray(&td->td_sampleinfo, &sv, 1);
+			_TIFFsetShortArrayExt(tif, &td->td_sampleinfo, &sv, 1);
 		}
 		break;
 	case TIFFTAG_TILEWIDTH:
@@ -446,7 +462,7 @@ _TIFFVSetField(TIFF* tif, uint32_t tag, va_list ap)
 	case TIFFTAG_SUBIFD:
 		if ((tif->tif_flags & TIFF_INSUBIFD) == 0) {
 			td->td_nsubifd = (uint16_t) va_arg(ap, uint16_vap);
-			_TIFFsetLong8Array(&td->td_subifd, (uint64_t*) va_arg(ap, uint64_t*),
+			_TIFFsetLong8Array(tif, &td->td_subifd, (uint64_t*) va_arg(ap, uint64_t*),
 			    (uint32_t) td->td_nsubifd);
 		} else {
 			TIFFErrorExtR(tif, module,
@@ -467,13 +483,13 @@ _TIFFVSetField(TIFF* tif, uint32_t tag, va_list ap)
 		uint32_t i;
 		v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
 		for (i = 0; i < v; i++)
-			_TIFFsetShortArray(&td->td_transferfunction[i],
+			_TIFFsetShortArrayExt(tif, &td->td_transferfunction[i],
                                va_arg(ap, uint16_t*), 1U << td->td_bitspersample);
 		break;
 	}
 	case TIFFTAG_REFERENCEBLACKWHITE:
 		/* XXX should check for null range */
-		_TIFFsetFloatArray(&td->td_refblackwhite, va_arg(ap, float*), 6);
+		_TIFFsetFloatArrayExt(tif, &td->td_refblackwhite, va_arg(ap, float*), 6);
 		break;
 	case TIFFTAG_INKNAMES:
 		{
@@ -483,7 +499,7 @@ _TIFFVSetField(TIFF* tif, uint32_t tag, va_list ap)
 			ninksinstring = countInkNamesString(tif, v, s);
 			status = ninksinstring > 0;
 			if(ninksinstring > 0 ) {
-				_TIFFsetNString(&td->td_inknames, s, v);
+				_TIFFsetNString(tif, &td->td_inknames, s, v);
 				td->td_inknameslen = v;
 				/* Set NumberOfInks to the value ninksinstring */
 				if (TIFFFieldSet(tif, FIELD_NUMBEROFINKS))
@@ -580,7 +596,7 @@ _TIFFVSetField(TIFF* tif, uint32_t tag, va_list ap)
 			if (td->td_customValues[iCustom].info->field_tag == tag) {
 				tv = td->td_customValues + iCustom;
 				if (tv->value != NULL) {
-					_TIFFfree(tv->value);
+					_TIFFfreeExt(tif, tv->value);
 					tv->value = NULL;
 				}
 				break;
@@ -595,7 +611,7 @@ _TIFFVSetField(TIFF* tif, uint32_t tag, va_list ap)
 
 			td->td_customValueCount++;
 			new_customValues = (TIFFTagValue *)
-			    _TIFFrealloc(td->td_customValues,
+			    _TIFFreallocExt(tif, td->td_customValues,
 			    sizeof(TIFFTagValue) * td->td_customValueCount);
 			if (!new_customValues) {
 				TIFFErrorExtR(tif, module,
@@ -654,7 +670,7 @@ _TIFFVSetField(TIFF* tif, uint32_t tag, va_list ap)
 				ma=(uint32_t)len;
 			}
 			tv->count=ma;
-			setByteArray(&tv->value,mb,ma,1);
+			setByteArray(tif, &tv->value,mb,ma,1);
 		}
 		else
 		{
@@ -899,7 +915,7 @@ _TIFFVSetField(TIFF* tif, uint32_t tag, va_list ap)
 			if (tv2 != NULL) {
 				/* Remove custom field from custom list */
 				if (tv2->value != NULL) {
-					_TIFFfree(tv2->value);
+					_TIFFfreeExt(tif, tv2->value);
 					tv2->value = NULL;
 				}
 				/* Shorten list and close gap in customValues list.
@@ -997,7 +1013,7 @@ TIFFUnsetField(TIFF* tif, uint32_t tag)
 
         if( i < td->td_customValueCount )
         {
-            _TIFFfree(tv->value);
+            _TIFFfreeExt(tif, tv->value);
             for( ; i < td->td_customValueCount-1; i++) {
                 td->td_customValues[i] = td->td_customValues[i+1];
             }
@@ -1406,7 +1422,7 @@ TIFFVGetField(TIFF* tif, uint32_t tag, va_list ap)
 
 #define	CleanupField(member) {		\
     if (td->member) {			\
-	_TIFFfree(td->member);		\
+	_TIFFfreeExt(tif, td->member);		\
 	td->member = 0;			\
     }					\
 }
@@ -1442,7 +1458,7 @@ TIFFFreeDirectory(TIFF* tif)
 	/* Cleanup custom tag values */
 	for( i = 0; i < td->td_customValueCount; i++ ) {
 		if (td->td_customValues[i].value)
-			_TIFFfree(td->td_customValues[i].value);
+			_TIFFfreeExt(tif, td->td_customValues[i].value);
 	}
 
 	td->td_customValueCount = 0;
@@ -1583,9 +1599,9 @@ TIFFDefaultDirectory(TIFF* tif)
 
 		for (i = 0; i < tif->tif_nfieldscompat; i++) {
 				if (tif->tif_fieldscompat[i].allocated_size)
-						_TIFFfree(tif->tif_fieldscompat[i].fields);
+						_TIFFfreeExt(tif, tif->tif_fieldscompat[i].fields);
 		}
-		_TIFFfree(tif->tif_fieldscompat);
+		_TIFFfreeExt(tif, tif->tif_fieldscompat);
 		tif->tif_nfieldscompat = 0;
 		tif->tif_fieldscompat = NULL;
 	}
@@ -1997,7 +2013,7 @@ TIFFUnlinkDirectory(TIFF* tif, uint16_t dirn)
 	 */
 	(*tif->tif_cleanup)(tif);
 	if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
-		_TIFFfree(tif->tif_rawdata);
+		_TIFFfreeExt(tif, tif->tif_rawdata);
 		tif->tif_rawdata = NULL;
 		tif->tif_rawcc = 0;
                 tif->tif_rawdataoff = 0;
diff --git a/libtiff/tif_dirinfo.c b/libtiff/tif_dirinfo.c
index 5b412c2a..76e1b26e 100644
--- a/libtiff/tif_dirinfo.c
+++ b/libtiff/tif_dirinfo.c
@@ -422,16 +422,16 @@ _TIFFSetupFields(TIFF* tif, const TIFFFieldArray* fieldarray)
 			if (fld->field_name != NULL) {
 				if (fld->field_bit == FIELD_CUSTOM &&
 					TIFFFieldIsAnonymous(fld)) {
-					_TIFFfree(fld->field_name);
+					_TIFFfreeExt(tif, fld->field_name);
 					/* caution: tif_fields[i] must not be the beginning of a fields-array. 
 					 *          Otherwise the following tags are also freed with the first free().
 					 */
-					_TIFFfree(fld);
+					_TIFFfreeExt(tif, fld);
 				}
 			}
 		}
 
-		_TIFFfree(tif->tif_fields);
+		_TIFFfreeExt(tif, tif->tif_fields);
 		tif->tif_fields = NULL;
 		tif->tif_nfields = 0;
 	}
@@ -836,7 +836,7 @@ _TIFFCreateAnonField(TIFF *tif, uint32_t tag, TIFFDataType field_type)
 	TIFFField *fld;
 	(void) tif;
 
-	fld = (TIFFField *) _TIFFmalloc(sizeof (TIFFField));
+	fld = (TIFFField *) _TIFFmallocExt(tif, sizeof (TIFFField));
 	if (fld == NULL)
 	    return NULL;
 	_TIFFmemset(fld, 0, sizeof(TIFFField));
@@ -908,9 +908,9 @@ _TIFFCreateAnonField(TIFF *tif, uint32_t tag, TIFFDataType field_type)
 	fld->field_bit = FIELD_CUSTOM;
 	fld->field_oktochange = TRUE;
 	fld->field_passcount = TRUE;
-	fld->field_name = (char *) _TIFFmalloc(32);
+	fld->field_name = (char *) _TIFFmallocExt(tif, 32);
 	if (fld->field_name == NULL) {
-	    _TIFFfree(fld);
+	    _TIFFfreeExt(tif, fld);
 	    return NULL;
 	}
 	fld->field_subfields = NULL;
diff --git a/libtiff/tif_dirread.c b/libtiff/tif_dirread.c
index 92cf6e96..b092b737 100644
--- a/libtiff/tif_dirread.c
+++ b/libtiff/tif_dirread.c
@@ -1127,7 +1127,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryDataAndRealloc(
             }
 #endif
 
-            new_dest = (uint8_t*) _TIFFrealloc(
+            new_dest = (uint8_t*) _TIFFreallocExt(tif,
                             *pdest, already_read + to_read);
             if( new_dest == NULL )
             {
@@ -1228,7 +1228,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryArrayWithLimit(
 				err=TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset, (tmsize_t)datasize, &data);
 			if (err!=TIFFReadDirEntryErrOk)
 			{
-				_TIFFfree(data);
+				_TIFFfreeExt(tif, data);
 				return(err);
 			}
 		}
@@ -1250,7 +1250,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryArrayWithLimit(
 				err=TIFFReadDirEntryDataAndRealloc(tif, (uint64_t)offset, (tmsize_t)datasize, &data);
 			if (err!=TIFFReadDirEntryErrOk)
 			{
-				_TIFFfree(data);
+				_TIFFfreeExt(tif, data);
 				return(err);
 			}
 		}
@@ -1310,7 +1310,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntr
 					err=TIFFReadDirEntryCheckRangeByteSbyte(*m);
 					if (err!=TIFFReadDirEntryErrOk)
 					{
-						_TIFFfree(origdata);
+						_TIFFfreeExt(tif, origdata);
 						return(err);
 					}
 					m++;
@@ -1319,10 +1319,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntr
 				return(TIFFReadDirEntryErrOk);
 			}
 	}
-	data=(uint8_t*)_TIFFmalloc(count);
+	data=(uint8_t*)_TIFFmallocExt(tif, count);
 	if (data==0)
 	{
-		_TIFFfree(origdata);
+		_TIFFfreeExt(tif, origdata);
 		return(TIFFReadDirEntryErrAlloc);
 	}
 	switch (direntry->tdir_type)
@@ -1436,10 +1436,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryByteArray(TIFF* tif, TIFFDirEntr
 			}
 			break;
 	}
-	_TIFFfree(origdata);
+	_TIFFfreeExt(tif, origdata);
 	if (err!=TIFFReadDirEntryErrOk)
 	{
-		_TIFFfree(data);
+		_TIFFfreeExt(tif, data);
 		return(err);
 	}
 	*value=data;
@@ -1486,7 +1486,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEnt
 					err=TIFFReadDirEntryCheckRangeSbyteByte(*m);
 					if (err!=TIFFReadDirEntryErrOk)
 					{
-						_TIFFfree(origdata);
+						_TIFFfreeExt(tif, origdata);
 						return(err);
 					}
 					m++;
@@ -1498,10 +1498,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEnt
 			*value=(int8_t*)origdata;
 			return(TIFFReadDirEntryErrOk);
 	}
-	data=(int8_t*)_TIFFmalloc(count);
+	data=(int8_t*)_TIFFmallocExt(tif, count);
 	if (data==0)
 	{
-		_TIFFfree(origdata);
+		_TIFFfreeExt(tif, origdata);
 		return(TIFFReadDirEntryErrAlloc);
 	}
 	switch (direntry->tdir_type)
@@ -1615,10 +1615,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntrySbyteArray(TIFF* tif, TIFFDirEnt
 			}
 			break;
 	}
-	_TIFFfree(origdata);
+	_TIFFfreeExt(tif, origdata);
 	if (err!=TIFFReadDirEntryErrOk)
 	{
-		_TIFFfree(data);
+		_TIFFfreeExt(tif, data);
 		return(err);
 	}
 	*value=data;
@@ -1670,7 +1670,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEnt
 					err=TIFFReadDirEntryCheckRangeShortSshort(*m);
 					if (err!=TIFFReadDirEntryErrOk)
 					{
-						_TIFFfree(origdata);
+						_TIFFfreeExt(tif, origdata);
 						return(err);
 					}
 					m++;
@@ -1679,10 +1679,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEnt
 				return(TIFFReadDirEntryErrOk);
 			}
 	}
-	data=(uint16_t*)_TIFFmalloc(count * 2);
+	data=(uint16_t*)_TIFFmallocExt(tif, count * 2);
 	if (data==0)
 	{
-		_TIFFfree(origdata);
+		_TIFFfreeExt(tif, origdata);
 		return(TIFFReadDirEntryErrAlloc);
 	}
 	switch (direntry->tdir_type)
@@ -1787,10 +1787,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryShortArray(TIFF* tif, TIFFDirEnt
 			}
 			break;
 	}
-	_TIFFfree(origdata);
+	_TIFFfreeExt(tif, origdata);
 	if (err!=TIFFReadDirEntryErrOk)
 	{
-		_TIFFfree(data);
+		_TIFFfreeExt(tif, data);
 		return(err);
 	}
 	*value=data;
@@ -1837,7 +1837,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEn
 					err=TIFFReadDirEntryCheckRangeSshortShort(*m);
 					if (err!=TIFFReadDirEntryErrOk)
 					{
-						_TIFFfree(origdata);
+						_TIFFfreeExt(tif, origdata);
 						return(err);
 					}
 					m++;
@@ -1851,10 +1851,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEn
 				TIFFSwabArrayOfShort((uint16_t*)(*value), count);
 			return(TIFFReadDirEntryErrOk);
 	}
-	data=(int16_t*)_TIFFmalloc(count * 2);
+	data=(int16_t*)_TIFFmallocExt(tif, count * 2);
 	if (data==0)
 	{
-		_TIFFfree(origdata);
+		_TIFFfreeExt(tif, origdata);
 		return(TIFFReadDirEntryErrAlloc);
 	}
 	switch (direntry->tdir_type)
@@ -1954,10 +1954,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntrySshortArray(TIFF* tif, TIFFDirEn
 			}
 			break;
 	}
-	_TIFFfree(origdata);
+	_TIFFfreeExt(tif, origdata);
 	if (err!=TIFFReadDirEntryErrOk)
 	{
-		_TIFFfree(data);
+		_TIFFfreeExt(tif, data);
 		return(err);
 	}
 	*value=data;
@@ -2009,7 +2009,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntr
 					err=TIFFReadDirEntryCheckRangeLongSlong(*m);
 					if (err!=TIFFReadDirEntryErrOk)
 					{
-						_TIFFfree(origdata);
+						_TIFFfreeExt(tif, origdata);
 						return(err);
 					}
 					m++;
@@ -2018,10 +2018,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntr
 				return(TIFFReadDirEntryErrOk);
 			}
 	}
-	data=(uint32_t*)_TIFFmalloc(count * 4);
+	data=(uint32_t*)_TIFFmallocExt(tif, count * 4);
 	if (data==0)
 	{
-		_TIFFfree(origdata);
+		_TIFFfreeExt(tif, origdata);
 		return(TIFFReadDirEntryErrAlloc);
 	}
 	switch (direntry->tdir_type)
@@ -2123,10 +2123,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryLongArray(TIFF* tif, TIFFDirEntr
 			}
 			break;
 	}
-	_TIFFfree(origdata);
+	_TIFFfreeExt(tif, origdata);
 	if (err!=TIFFReadDirEntryErrOk)
 	{
-		_TIFFfree(data);
+		_TIFFfreeExt(tif, data);
 		return(err);
 	}
 	*value=data;
@@ -2173,7 +2173,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEnt
 					err=TIFFReadDirEntryCheckRangeSlongLong(*m);
 					if (err!=TIFFReadDirEntryErrOk)
 					{
-						_TIFFfree(origdata);
+						_TIFFfreeExt(tif, origdata);
 						return(err);
 					}
 					m++;
@@ -2187,10 +2187,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEnt
 				TIFFSwabArrayOfLong((uint32_t*)(*value), count);
 			return(TIFFReadDirEntryErrOk);
 	}
-	data=(int32_t*)_TIFFmalloc(count * 4);
+	data=(int32_t*)_TIFFmallocExt(tif, count * 4);
 	if (data==0)
 	{
-		_TIFFfree(origdata);
+		_TIFFfreeExt(tif, origdata);
 		return(TIFFReadDirEntryErrAlloc);
 	}
 	switch (direntry->tdir_type)
@@ -2284,10 +2284,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntrySlongArray(TIFF* tif, TIFFDirEnt
 			}
 			break;
 	}
-	_TIFFfree(origdata);
+	_TIFFfreeExt(tif, origdata);
 	if (err!=TIFFReadDirEntryErrOk)
 	{
-		_TIFFfree(data);
+		_TIFFfreeExt(tif, data);
 		return(err);
 	}
 	*value=data;
@@ -2340,7 +2340,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8ArrayWithLimit(
 					err=TIFFReadDirEntryCheckRangeLong8Slong8(*m);
 					if (err!=TIFFReadDirEntryErrOk)
 					{
-						_TIFFfree(origdata);
+						_TIFFfreeExt(tif, origdata);
 						return(err);
 					}
 					m++;
@@ -2349,10 +2349,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8ArrayWithLimit(
 				return(TIFFReadDirEntryErrOk);
 			}
 	}
-	data=(uint64_t*)_TIFFmalloc(count * 8);
+	data=(uint64_t*)_TIFFmallocExt(tif, count * 8);
 	if (data==0)
 	{
-		_TIFFfree(origdata);
+		_TIFFfreeExt(tif, origdata);
 		return(TIFFReadDirEntryErrAlloc);
 	}
 	switch (direntry->tdir_type)
@@ -2451,10 +2451,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryLong8ArrayWithLimit(
 			}
 			break;
 	}
-	_TIFFfree(origdata);
+	_TIFFfreeExt(tif, origdata);
 	if (err!=TIFFReadDirEntryErrOk)
 	{
-		_TIFFfree(data);
+		_TIFFfreeExt(tif, data);
 		return(err);
 	}
 	*value=data;
@@ -2506,7 +2506,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEn
 					err=TIFFReadDirEntryCheckRangeSlong8Long8(*m);
 					if (err!=TIFFReadDirEntryErrOk)
 					{
-						_TIFFfree(origdata);
+						_TIFFfreeExt(tif, origdata);
 						return(err);
 					}
 					m++;
@@ -2520,10 +2520,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEn
 				TIFFSwabArrayOfLong8((uint64_t*)(*value), count);
 			return(TIFFReadDirEntryErrOk);
 	}
-	data=(int64_t*)_TIFFmalloc(count * 8);
+	data=(int64_t*)_TIFFmallocExt(tif, count * 8);
 	if (data==0)
 	{
-		_TIFFfree(origdata);
+		_TIFFfreeExt(tif, origdata);
 		return(TIFFReadDirEntryErrAlloc);
 	}
 	switch (direntry->tdir_type)
@@ -2611,7 +2611,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntrySlong8Array(TIFF* tif, TIFFDirEn
 			}
 			break;
 	}
-	_TIFFfree(origdata);
+	_TIFFfreeExt(tif, origdata);
 	*value=data;
 	return(TIFFReadDirEntryErrOk);
 }
@@ -2655,10 +2655,10 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEnt
 			*value=(float*)origdata;
 			return(TIFFReadDirEntryErrOk);
 	}
-	data=(float*)_TIFFmalloc(count*sizeof(float));
+	data=(float*)_TIFFmallocExt(tif, count*sizeof(float));
 	if (data==0)
 	{
-		_TIFFfree(origdata);
+		_TIFFfreeExt(tif, origdata);
 		return(TIFFReadDirEntryErrAlloc);
 	}
 	switch (direntry->tdir_type)
@@ -2855,7 +2855,7 @@ static enum TIFFReadDirEntryErr TIFFReadDirEntryFloatArray(TIFF* tif, TIFFDirEnt
 			}
 			break;
 	}
-	_TIFFfree(origdata);
+	_TIFFfreeExt(tif, origdata);
 	*value=data;
 	return(TIFFReadDirEntryErrOk);
 }
@@ -2900,10 +2900,10 @@ TIFFReadDirEntryDoubleArray(TIFF* tif, TIFFDirEntry* direntry, double** value)
 			*value=(double*)origdata;
 			return(TIFFReadDirEntryErrOk);
 	}
-	data=(double*)_TIFFmalloc(count*sizeof(double));
+	data=(double*)_TIFFmalloc

(Patch may be truncated, please check the link at the top of this post.)