sndfile-to-text: Fix large stack allocation

This commit is contained in:
evpobr 2021-03-16 16:54:17 +05:00
parent 6fb0313563
commit 8ff8ed6967

View File

@ -60,12 +60,18 @@ print_usage (char *progname)
} /* print_usage */
static void
static int
convert_to_text (SNDFILE * infile, FILE * outfile, int channels, int full_precision)
{ float buf [BLOCK_SIZE] ;
{ float *buf ;
sf_count_t frames ;
int k, m, readcount ;
buf = malloc (BLOCK_SIZE * sizeof (float)) ;
if (buf == NULL)
{ printf ("Error : Out of memory.\n\n") ;
return 1 ;
} ;
frames = BLOCK_SIZE / channels ;
while ((readcount = (int) sf_readf_float (infile, buf, frames)) > 0)
@ -79,7 +85,9 @@ convert_to_text (SNDFILE * infile, FILE * outfile, int channels, int full_precis
} ;
} ;
return ;
free (buf) ;
return 0 ;
} /* convert_to_text */
int
@ -89,6 +97,7 @@ main (int argc, char * argv [])
FILE *outfile = NULL ;
SF_INFO sfinfo ;
int full_precision = 0 ;
int ret = 1 ;
progname = strrchr (argv [0], '/') ;
progname = progname ? progname + 1 : argv [0] ;
@ -97,7 +106,7 @@ main (int argc, char * argv [])
{ case 4 :
if (!strcmp ("--full-precision", argv [3]))
{ print_usage (progname) ;
return 1 ;
goto cleanup ;
} ;
full_precision = 1 ;
argv++ ;
@ -105,7 +114,7 @@ main (int argc, char * argv [])
break ;
default:
print_usage (progname) ;
return 1 ;
goto cleanup ;
} ;
infilename = argv [1] ;
@ -114,19 +123,19 @@ main (int argc, char * argv [])
if (strcmp (infilename, outfilename) == 0)
{ printf ("Error : Input and output filenames are the same.\n\n") ;
print_usage (progname) ;
return 1 ;
goto cleanup ;
} ;
if (infilename [0] == '-')
{ printf ("Error : Input filename (%s) looks like an option.\n\n", infilename) ;
print_usage (progname) ;
return 1 ;
goto cleanup ;
} ;
if (outfilename [0] == '-')
{ printf ("Error : Output filename (%s) looks like an option.\n\n", outfilename) ;
print_usage (progname) ;
return 1 ;
goto cleanup ;
} ;
memset (&sfinfo, 0, sizeof (sfinfo)) ;
@ -134,22 +143,25 @@ main (int argc, char * argv [])
if ((infile = sf_open (infilename, SFM_READ, &sfinfo)) == NULL)
{ printf ("Not able to open input file %s.\n", infilename) ;
puts (sf_strerror (NULL)) ;
return 1 ;
goto cleanup ;
} ;
/* Open the output file. */
if ((outfile = fopen (outfilename, "w")) == NULL)
{ printf ("Not able to open output file %s : %s\n", outfilename, sf_strerror (NULL)) ;
return 1 ;
goto cleanup ;
} ;
fprintf (outfile, "# Converted from file %s.\n", infilename) ;
fprintf (outfile, "# Channels %d, Sample rate %d\n", sfinfo.channels, sfinfo.samplerate) ;
convert_to_text (infile, outfile, sfinfo.channels, full_precision) ;
ret = convert_to_text (infile, outfile, sfinfo.channels, full_precision) ;
cleanup :
sf_close (infile) ;
fclose (outfile) ;
if (outfile != NULL)
fclose (outfile) ;
return 0 ;
} /* main */