327a27
--- libsndfile-1.0.28/src/rf64.c	2017-04-02 09:43:22.000000000 +0200
327a27
+++ libsndfile-1.0.27/src/rf64.c	2016-04-01 23:08:53.000000000 +0200
327a27
@@ -735,25 +734,27 @@ rf64_write_header (SF_PRIVATE *psf, int
327a27
 
327a27
 #endif
327a27
 
327a27
-	pad_size = psf->dataoffset - 16 - psf->header.indx ;
327a27
-	if (pad_size >= 0)
327a27
-		psf_binheader_writef (psf, "m4z", PAD_MARKER, pad_size, make_size_t (pad_size)) ;
327a27
+	if (psf->header.indx + 8 < psf->dataoffset)
327a27
+	{	/* Add PAD data if necessary. */
327a27
+		int k = psf->dataoffset - 16 - psf->header.indx ;
327a27
+		psf_binheader_writef (psf, "m4z", PAD_MARKER, k, make_size_t (k)) ;
327a27
+		} ;
327a27
 
327a27
 	if (wpriv->rf64_downgrade && (psf->filelength < RIFF_DOWNGRADE_BYTES))
327a27
 		psf_binheader_writef (psf, "tm8", data_MARKER, psf->datalength) ;
327a27
 	else
327a27
 		psf_binheader_writef (psf, "m4", data_MARKER, 0xffffffff) ;
327a27
 
327a27
-	psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
327a27
+	psf_fwrite (psf->header.ptr, psf->header.indx, 1, psf) ;
327a27
 	if (psf->error)
327a27
 		return psf->error ;
327a27
 
327a27
-	if (has_data && psf->dataoffset != psf->header.indx)
327a27
-	{	psf_log_printf (psf, "Oooops : has_data && psf->dataoffset != psf->header.indx\n") ;
327a27
+	if (has_data && psf->dataoffset != psf->header.indx)
327a27
+	{	psf_log_printf (psf, "Oooops : has_data && psf->dataoffset != psf->header.indx\n") ;
327a27
 		return psf->error = SFE_INTERNAL ;
327a27
 		} ;
327a27
 
327a27
-	psf->dataoffset = psf->header.indx ;
327a27
+	psf->dataoffset = psf->header.indx ;
327a27
 
327a27
 	if (NOT (has_data))
327a27
 		psf_fseek (psf, psf->dataoffset, SEEK_SET) ;