Merge f561f0ba8e76cb6fbf778f8869acebd56622776e on remote branch

Change-Id: Ib0803bf99e429bee78751f44b723d22549dc21ee
diff --git a/README.android b/README.android
index 12f476d..b585b6b 100644
--- a/README.android
+++ b/README.android
@@ -18,3 +18,8 @@
 There's a pull request upstream for this as well. If that's accepted, this
 can be removed as an Android-specific modification.
 https://github.com/libjpeg-turbo/libjpeg-turbo/pull/318
+
+(3) jdapistd.c
+
+Includes a cherry-pick of
+https://github.com/libjpeg-turbo/libjpeg-turbo/commit/6d2e8837b440ce4d8befd805a5abc0d351028d70
diff --git a/jdapistd.c b/jdapistd.c
index 2c808fa..17ce7b6 100644
--- a/jdapistd.c
+++ b/jdapistd.c
@@ -316,6 +316,9 @@
 read_and_discard_scanlines(j_decompress_ptr cinfo, JDIMENSION num_lines)
 {
   JDIMENSION n;
+  JSAMPLE dummy_sample[1] = { 0 };
+  JSAMPROW dummy_row = dummy_sample;
+  JSAMPARRAY scanlines = NULL;
   void (*color_convert) (j_decompress_ptr cinfo, JSAMPIMAGE input_buf,
                          JDIMENSION input_row, JSAMPARRAY output_buf,
                          int num_rows) = NULL;
@@ -325,6 +328,10 @@
   if (cinfo->cconvert && cinfo->cconvert->color_convert) {
     color_convert = cinfo->cconvert->color_convert;
     cinfo->cconvert->color_convert = noop_convert;
+    /* This just prevents UBSan from complaining about adding 0 to a NULL
+     * pointer.  The pointer isn't actually used.
+     */
+    scanlines = &dummy_row;
   }
 
   if (cinfo->cquantize && cinfo->cquantize->color_quantize) {
@@ -333,7 +340,7 @@
   }
 
   for (n = 0; n < num_lines; n++)
-    jpeg_read_scanlines(cinfo, NULL, 1);
+    jpeg_read_scanlines(cinfo, scanlines, 1);
 
   if (color_convert)
     cinfo->cconvert->color_convert = color_convert;