Snap for 8564071 from 2536fe25a69f90b7c380dc4ddff269403ebe9a0b to mainline-conscrypt-release

Change-Id: If6bfb23aa42e495689b8cea964fea9cb2a108690
diff --git a/Android.bp b/Android.bp
index af7b348..71406d8 100644
--- a/Android.bp
+++ b/Android.bp
@@ -59,6 +59,13 @@
         },
     },
     min_sdk_version: "29",
+    apex_available: [
+        "//apex_available:platform",
+        "com.android.media",
+        "com.android.media.swcodec",
+        "test_com.android.media.swcodec",
+    ],
+
 }
 
 cc_library_headers {
@@ -72,6 +79,13 @@
         },
     },
     min_sdk_version: "29",
+    apex_available: [
+        "//apex_available:platform",
+        "com.android.media",
+        "com.android.media.swcodec",
+        "test_com.android.media.swcodec",
+    ],
+
 }
 
 subdirs = ["src/libFLAC"]
diff --git a/METADATA b/METADATA
index b760b2b..4acf938 100644
--- a/METADATA
+++ b/METADATA
@@ -9,10 +9,7 @@
     value: "https://github.com/xiph/flac.git"
   }
   version: "bfd4f13f3eb1f4d4f9f723e343f63eceedb267ba"
-  # Would be RESTRICTED save for GFDL in:
-  #   README
-  #   COPYING.FDL
-  #   doc/html/flac.css
+  license_note: "Would be RESTRICTED save for GFDL in: README, COPYING.FDL, and doc/html/flac.css"
   license_type: BY_EXCEPTION_ONLY
   last_upgrade_date {
     year: 2020
diff --git a/src/libFLAC/Android.bp b/src/libFLAC/Android.bp
index 3d32da2..a08fae0 100644
--- a/src/libFLAC/Android.bp
+++ b/src/libFLAC/Android.bp
@@ -75,4 +75,11 @@
         },
     },
     min_sdk_version: "29",
+    apex_available: [
+        "//apex_available:platform",
+        "com.android.media",
+        "com.android.media.swcodec",
+        "test_com.android.media.swcodec",
+    ],
+
 }
diff --git a/src/libFLAC/stream_decoder.c b/src/libFLAC/stream_decoder.c
index 6707017..51a438c 100644
--- a/src/libFLAC/stream_decoder.c
+++ b/src/libFLAC/stream_decoder.c
@@ -2996,7 +2996,7 @@
 	FLAC__uint64 first_frame_offset = decoder->private_->first_frame_offset, lower_bound, upper_bound, lower_bound_sample, upper_bound_sample, this_frame_sample;
 	FLAC__int64 pos = -1;
 	int i;
-	uint32_t approx_bytes_per_frame;
+	FLAC__int64 approx_bytes_per_frame;
 	FLAC__bool first_seek = true;
 	const FLAC__uint64 total_samples = FLAC__stream_decoder_get_total_samples(decoder);
 	const uint32_t min_blocksize = decoder->private_->stream_info.data.stream_info.min_blocksize;
@@ -3041,6 +3041,12 @@
 	upper_bound = stream_length;
 	upper_bound_sample = total_samples > 0 ? total_samples : target_sample /*estimate it*/;
 
+	/* seeking beyond the end of the stream */
+	if(target_sample > upper_bound_sample) {
+		decoder->protected_->state = FLAC__STREAM_DECODER_SEEK_ERROR;
+		return false;
+	}
+
 	/*
 	 * Now we refine the bounds if we have a seektable with
 	 * suitable points.  Note that according to the spec they
@@ -3169,7 +3175,7 @@
 				return false;
 			}
 			/* our last move backwards wasn't big enough, try again */
-			approx_bytes_per_frame = approx_bytes_per_frame? approx_bytes_per_frame * 2 : 16;
+			approx_bytes_per_frame = (approx_bytes_per_frame > (upper_bound - lower_bound)) ? (upper_bound - lower_bound) : approx_bytes_per_frame ? approx_bytes_per_frame * 2 : 16;
 			continue;
 		}
 		/* allow one seek over upper bound, so we can get a correct upper_bound_sample for streams with unknown total_samples */