shithub: libsamplerate

Download patch

ref: eb077a3a3fad1905321e5a0ad1d76c601fba103b
parent: d8d66af6a5e33b2f01fac3e68874c6ba2bfac43f
author: Peter Hoyes <pahoyes@gmail.com>
date: Tue Jun 5 08:53:21 EDT 2018

Allow data_out == NULL if output_frames == 0
Add test for nullptr

--- a/Makefile.am
+++ b/Makefile.am
@@ -78,7 +78,7 @@
 check_PROGRAMS = tests/misc_test tests/termination_test tests/simple_test tests/callback_test \
 	tests/reset_test tests/multi_channel_test tests/snr_bw_test tests/float_short_test \
 	tests/varispeed_test tests/callback_hang_test tests/src-evaluate tests/throughput_test \
-	tests/multichan_throughput_test tests/downsample_test tests/clone_test
+	tests/multichan_throughput_test tests/downsample_test tests/clone_test tests/nullptr_test
 
 check: $(check_PROGRAMS)
 	date
@@ -139,6 +139,9 @@
 
 tests_clone_test_SOURCES = tests/clone_test.c tests/util.c tests/util.h
 tests_clone_test_LDADD = src/libsamplerate.la
+
+tests_nullptr_test_SOURCES = testes/nullptr_test.c tests/util.c tests/util.h
+tests_nullptr_test_LDADD = src/libsamplerate/la
 
 # This program is for evaluating other sample rate converters.
 
--- a/src/samplerate.c
+++ b/src/samplerate.c
@@ -141,7 +141,8 @@
 		return SRC_ERR_BAD_DATA ;
 
 	/* And that data_in and data_out are valid. */
-	if ((data->data_in == NULL && data->input_frames > 0) || data->data_out == NULL)
+	if ((data->data_in == NULL && data->input_frames > 0)
+			|| (data->data_out == NULL && data->output_frames > 0))
 		return SRC_ERR_BAD_DATA_PTR ;
 
 	/* Check src_ratio is in range. */
--- /dev/null
+++ b/tests/nullptr_test.c
@@ -1,0 +1,97 @@
+/*
+** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
+** All rights reserved.
+**
+** This code is released under 2-clause BSD license. Please see the
+** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
+*/
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <samplerate.h>
+
+#include "util.h"
+
+#define	BUFFER_LEN		(1 << 16)
+#define NUM_CHANNELS 	1
+
+static void
+nullptr_test (int converter)
+{	static float input [BUFFER_LEN * NUM_CHANNELS] ;
+	static float output [BUFFER_LEN * NUM_CHANNELS] ;
+
+	SRC_STATE* src_state ;
+	SRC_DATA src_data, src_data2 ;
+
+	int error, frame, ch, index ;
+
+	printf ("        nullptr_test     (%-28s) ....... ", src_get_name (converter)) ;
+	fflush (stdout) ;
+
+	memset (input, 0, sizeof (input)) ;
+	memset (output, 0, sizeof (output)) ;
+
+	if ((src_state = src_new (converter, NUM_CHANNELS, &error)) == NULL)
+	{	printf ("\n\nLine %d : src_new() failed : %s\n\n", __LINE__, src_strerror (error)) ;
+		exit (1) ;
+		} ;
+
+	src_data.src_ratio = 1.1 ;
+	src_data.input_frames = BUFFER_LEN ;
+	src_data.output_frames = BUFFER_LEN ;
+	src_data.data_in = input ;
+	src_data.data_out = output ;
+	src_data.output_frames_gen = 0 ;
+
+	if ((error = src_process (src_state, &src_data)))
+	{	printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
+		exit (1) ;
+		} ;
+
+	//Input is zero-length
+	src_data2 = src_data;
+	src_data2.data_in = NULL;
+	src_data2.input_frames = 0;
+
+	if ((error = src_process (src_state, &src_data2)))
+	{	printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
+		exit (1) ;
+		} ;
+
+	//Output is zero-length
+	src_data2 = src_data;
+	src_data2.data_out = NULL;
+	src_data2.output_frames = 0;
+
+	if ((error = src_process (src_state, &src_data2)))
+	{	printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
+		exit (1) ;
+		} ;
+
+	//Input and output are zero-length
+	src_data2 = src_data;
+	src_data2.data_in = NULL;
+	src_data2.data_out = NULL;
+	src_data2.input_frames = 0;
+	src_data2.output_frames = 0;
+
+	if ((error = src_process (src_state, &src_data2)))
+	{	printf ("\n\nLine %d : %s\n\n", __LINE__, src_strerror (error)) ;
+		exit (1) ;
+		} ;
+
+
+	src_state = src_delete (src_state) ;
+
+	puts ("ok") ;
+} /* clone_test */
+
+int
+main (void)
+{
+	nullptr_test (SRC_ZERO_ORDER_HOLD) ;
+
+	return 0 ;
+} /* main */