Apply libtiff fixes for CVE-2022-0908.

Bug: chromium:1323687
Change-Id: Ie94f0084d333ec4ce5d0cddec647b2b5f94d3f7b
Reviewed-on: https://pdfium-review.googlesource.com/c/pdfium/+/93570
Reviewed-by: Tom Sepez <tsepez@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Auto-Submit: Lei Zhang <thestig@chromium.org>
diff --git a/third_party/libtiff/0035-memcpy-nullptr-zero-size.patch b/third_party/libtiff/0035-memcpy-nullptr-zero-size.patch
new file mode 100644
index 0000000..141269e
--- /dev/null
+++ b/third_party/libtiff/0035-memcpy-nullptr-zero-size.patch
@@ -0,0 +1,16 @@
+diff --git a/third_party/libtiff/tif_dirread.c b/third_party/libtiff/tif_dirread.c
+index a09ac44e1..58c918181 100644
+--- a/third_party/libtiff/tif_dirread.c
++++ b/third_party/libtiff/tif_dirread.c
+@@ -5088,7 +5088,10 @@ TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp, int recover)
+ 								_TIFFfree(data);
+ 							return(0);
+ 						}
+-						_TIFFmemcpy(o,data,(uint32)dp->tdir_count);
++						if (dp->tdir_count > 0 )
++						{
++							_TIFFmemcpy(o,data,(uint32)dp->tdir_count);
++						}
+ 						o[(uint32)dp->tdir_count]=0;
+ 						if (data!=0)
+ 							_TIFFfree(data);
diff --git a/third_party/libtiff/README.pdfium b/third_party/libtiff/README.pdfium
index 57bfebc..8a6bd5b 100644
--- a/third_party/libtiff/README.pdfium
+++ b/third_party/libtiff/README.pdfium
@@ -18,3 +18,4 @@
 0032-builtin-codecs.patch: make _TIFFBuiltinCODECS const data.
 0033-avail-out-overflow.patch: signed comparison in PixarLogDecode().
 0034-memcpy-nullptr.patch: Check size before calling memcpy().
+0035-memcpy-nullptr-zero-size.patch: Check for nullptr and zero size before calling memcpy().
diff --git a/third_party/libtiff/tif_dirread.c b/third_party/libtiff/tif_dirread.c
index a09ac44..58c9181 100644
--- a/third_party/libtiff/tif_dirread.c
+++ b/third_party/libtiff/tif_dirread.c
@@ -5088,7 +5088,10 @@
 								_TIFFfree(data);
 							return(0);
 						}
-						_TIFFmemcpy(o,data,(uint32)dp->tdir_count);
+						if (dp->tdir_count > 0 )
+						{
+							_TIFFmemcpy(o,data,(uint32)dp->tdir_count);
+						}
 						o[(uint32)dp->tdir_count]=0;
 						if (data!=0)
 							_TIFFfree(data);