Merge "libFLAC: update from Xiph upstream"
diff --git a/include/share/alloc.h b/include/share/alloc.h
index 5e1f1e2..3b70749 100644
--- a/include/share/alloc.h
+++ b/include/share/alloc.h
@@ -153,11 +153,21 @@
 	return malloc(size1*size2);
 }
 
+static inline void *safe_realloc_(void *ptr, size_t size)
+{
+	void *oldptr = ptr;
+	void *newptr = realloc(ptr, size);
+	if(size > 0 && newptr == 0)
+		free(oldptr);
+	return newptr;
+}
 static inline void *safe_realloc_add_2op_(void *ptr, size_t size1, size_t size2)
 {
 	size2 += size1;
-	if(size2 < size1)
+	if(size2 < size1) {
+		free(ptr);
 		return 0;
+	}
 	return realloc(ptr, size2);
 }
 
@@ -192,7 +202,7 @@
 		return realloc(ptr, 0); /* preserve POSIX realloc(ptr, 0) semantics */
 	if(size1 > SIZE_MAX / size2)
 		return 0;
-	return realloc(ptr, size1*size2);
+	return safe_realloc_(ptr, size1*size2);
 }
 
 /* size1 * (size2 + size3) */
diff --git a/libFLAC/bitreader.c b/libFLAC/bitreader.c
index f61229b..67d0f37 100644
--- a/libFLAC/bitreader.c
+++ b/libFLAC/bitreader.c
@@ -418,12 +418,14 @@
 
 FLAC__bool FLAC__bitreader_read_raw_int32(FLAC__BitReader *br, FLAC__int32 *val, unsigned bits)
 {
+	FLAC__uint32 uval, mask;
 	/* OPT: inline raw uint32 code here, or make into a macro if possible in the .h file */
-	if(!FLAC__bitreader_read_raw_uint32(br, (FLAC__uint32*)val, bits))
+	if(!FLAC__bitreader_read_raw_uint32(br, &uval, bits))
 		return false;
-	/* sign-extend: */
-	*val <<= (32-bits);
-	*val >>= (32-bits);
+	/* sign-extend *val assuming it is currently bits wide. */
+	/* From: https://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend */
+	mask = 1u << (bits - 1);
+	*val = (uval ^ mask) - mask;
 	return true;
 }
 
diff --git a/libFLAC/bitwriter.c b/libFLAC/bitwriter.c
index 76be1bc..dcdd93e 100644
--- a/libFLAC/bitwriter.c
+++ b/libFLAC/bitwriter.c
@@ -520,7 +520,9 @@
 
 	while(nvals) {
 		/* fold signed to unsigned; actual formula is: negative(v)? -2v-1 : 2v */
-		uval = (*vals<<1) ^ (*vals>>31);
+		uval = *vals;
+		uval <<= 1;
+		uval ^= (*vals>>31);
 
 		msbits = uval >> parameter;
 
diff --git a/libFLAC/format.c b/libFLAC/format.c
index 5215c56..0b1d10b 100644
--- a/libFLAC/format.c
+++ b/libFLAC/format.c
@@ -39,6 +39,7 @@
 #include <string.h> /* for memset() */
 #include "FLAC/assert.h"
 #include "FLAC/format.h"
+#include "share/alloc.h"
 #include "share/compat.h"
 #include "private/format.h"
 #include "private/macros.h"
@@ -274,6 +275,9 @@
 
 	FLAC__ASSERT(0 != seek_table);
 
+	if (seek_table->num_points == 0)
+		return 0;
+
 	/* sort the seekpoints */
 	qsort(seek_table->points, seek_table->num_points, sizeof(FLAC__StreamMetadata_SeekPoint), (int (*)(const void *, const void *))seekpoint_compare_);
 
@@ -573,17 +577,10 @@
 	FLAC__ASSERT(object->capacity_by_order > 0 || (0 == object->parameters && 0 == object->raw_bits));
 
 	if(object->capacity_by_order < max_partition_order) {
-		void *oldptr;
-		oldptr = object->parameters;
-		if(0 == (object->parameters = realloc(object->parameters, sizeof(unsigned)*(1 << max_partition_order)))) {
-			free(oldptr);
+		if(0 == (object->parameters = safe_realloc_(object->parameters, sizeof(unsigned)*(1 << max_partition_order))))
 			return false;
-		}
-		oldptr = object->raw_bits;
-		if(0 == (object->raw_bits = realloc(object->raw_bits, sizeof(unsigned)*(1 << max_partition_order)))) {
-			free(oldptr);
+		if(0 == (object->raw_bits = safe_realloc_(object->raw_bits, sizeof(unsigned)*(1 << max_partition_order))))
 			return false;
-		}
 		memset(object->raw_bits, 0, sizeof(unsigned)*(1 << max_partition_order));
 		object->capacity_by_order = max_partition_order;
 	}
diff --git a/libFLAC/md5.c b/libFLAC/md5.c
index 051efe1..e9013a9 100644
--- a/libFLAC/md5.c
+++ b/libFLAC/md5.c
@@ -499,14 +499,12 @@
 		return false;
 
 	if (ctx->capacity < bytes_needed) {
-		FLAC__byte *tmp = realloc(ctx->internal_buf.p8, bytes_needed);
-		if (0 == tmp) {
-			free(ctx->internal_buf.p8);
-			if (0 == (ctx->internal_buf.p8 = safe_malloc_(bytes_needed)))
+		if (0 == (ctx->internal_buf.p8 = safe_realloc_(ctx->internal_buf.p8, bytes_needed))) {
+			if (0 == (ctx->internal_buf.p8 = safe_malloc_(bytes_needed))) {
+				ctx->capacity = 0;
 				return false;
+			}
 		}
-		else
-			ctx->internal_buf.p8 = tmp;
 		ctx->capacity = bytes_needed;
 	}
 
diff --git a/libFLAC/stream_decoder.c b/libFLAC/stream_decoder.c
index 4a4be2e..77036ba 100644
--- a/libFLAC/stream_decoder.c
+++ b/libFLAC/stream_decoder.c
@@ -763,9 +763,7 @@
 	FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
 
 	if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
-		void *oldptr = decoder->private_->metadata_filter_ids;
 		if(0 == (decoder->private_->metadata_filter_ids = safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) {
-			free(oldptr);
 			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
 			return false;
 		}
@@ -824,9 +822,7 @@
 	FLAC__ASSERT(0 != decoder->private_->metadata_filter_ids);
 
 	if(decoder->private_->metadata_filter_ids_count == decoder->private_->metadata_filter_ids_capacity) {
-		void *oldptr = decoder->private_->metadata_filter_ids;
 		if(0 == (decoder->private_->metadata_filter_ids = safe_realloc_mul_2op_(decoder->private_->metadata_filter_ids, decoder->private_->metadata_filter_ids_capacity, /*times*/2))) {
-			free(oldptr);
 			decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
 			return false;
 		}
@@ -1660,7 +1656,6 @@
 {
 	FLAC__uint32 i, x;
 	FLAC__uint64 xx;
-	void *oldptr;
 
 	FLAC__ASSERT(FLAC__bitreader_is_consumed_byte_aligned(decoder->private_->input));
 
@@ -1671,9 +1666,7 @@
 	decoder->private_->seek_table.data.seek_table.num_points = length / FLAC__STREAM_METADATA_SEEKPOINT_LENGTH;
 
 	/* use realloc since we may pass through here several times (e.g. after seeking) */
-	oldptr = decoder->private_->seek_table.data.seek_table.points;
 	if(0 == (decoder->private_->seek_table.data.seek_table.points = safe_realloc_mul_2op_(decoder->private_->seek_table.data.seek_table.points, decoder->private_->seek_table.data.seek_table.num_points, /*times*/sizeof(FLAC__StreamMetadata_SeekPoint)))) {
-		free(oldptr);
 		decoder->protected_->state = FLAC__STREAM_DECODER_MEMORY_ALLOCATION_ERROR;
 		return false;
 	}
@@ -2122,7 +2115,7 @@
 #if 1
 						mid = decoder->private_->output[0][i];
 						side = decoder->private_->output[1][i];
-						mid <<= 1;
+						mid = ((uint32_t) mid) << 1;
 						mid |= (side & 1); /* i.e. if 'side' is odd... */
 						decoder->private_->output[0][i] = (mid + side) >> 1;
 						decoder->private_->output[1][i] = (mid - side) >> 1;
@@ -2548,8 +2541,10 @@
 
 	if(wasted_bits && do_full_decode) {
 		x = decoder->private_->frame.subframes[channel].wasted_bits;
-		for(i = 0; i < decoder->private_->frame.header.blocksize; i++)
-			decoder->private_->output[channel][i] <<= x;
+		for(i = 0; i < decoder->private_->frame.header.blocksize; i++) {
+			uint32_t val = decoder->private_->output[channel][i];
+			decoder->private_->output[channel][i] = (val << x);
+		}
 	}
 
 	return true;