linux-kernel-mentees.lists.linuxfoundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] kselftest/alsa: Fix -Wformat compiler warnings
@ 2023-03-24 16:01 Alexander Heinrich
  2023-03-24 16:33 ` Mark Brown
  0 siblings, 1 reply; 2+ messages in thread
From: Alexander Heinrich @ 2023-03-24 16:01 UTC (permalink / raw)
  To: tiwai; +Cc: shuah, perex, broonie, linux-kselftest, linux-kernel-mentees

Fix compiler warnings caused by mixing long and unsigned int values:
Change type of variables rate, channels, period_size and buffer_size to
unsigned int (as used by rrate).

Signed-off-by: Alexander Heinrich <hallo@alexanderheinrich.de>
---
Hi Takashi,
Thank you for your feedback on my first version of the patch!
I have changed the variables to unsigned int, removed the casts and
changed the relevant format specifiers to %d.

This produced a new warnings in two places, where the 'rate' and 'channels' 
variables were being multiplied with 'snd_pcm_format_physical_width()', because
this function returns an int (and can return a negative value on failure).
I added a check for negative values, so that the return value can safely
be cast to unsigned int.

For the string to int conversion functions, the return values for the test cases
in pcm-test.conf are non-negative and in the range of unsigned int, so I just
cast them to unsigned int.

Please let me know if I can further improve on my patch (it is my first)!
Thanks and greetings,
Alex

 tools/testing/selftests/alsa/pcm-test.c | 41 ++++++++++++++-----------
 1 file changed, 23 insertions(+), 18 deletions(-)

diff --git a/tools/testing/selftests/alsa/pcm-test.c b/tools/testing/selftests/alsa/pcm-test.c
index 3e390fe67eb9..d4420789837c 100644
--- a/tools/testing/selftests/alsa/pcm-test.c
+++ b/tools/testing/selftests/alsa/pcm-test.c
@@ -259,7 +259,7 @@ static void test_pcm_time(struct pcm_data *data, enum test_class class,
 {
 	char name[64], key[128], msg[256];
 	const char *cs;
-	int i, err;
+	int i, err, phys_width;
 	snd_pcm_t *handle = NULL;
 	snd_pcm_access_t access = SND_PCM_ACCESS_RW_INTERLEAVED;
 	snd_pcm_format_t format, old_format;
@@ -267,8 +267,7 @@ static void test_pcm_time(struct pcm_data *data, enum test_class class,
 	unsigned char *samples = NULL;
 	snd_pcm_sframes_t frames;
 	long long ms;
-	long rate, channels, period_size, buffer_size;
-	unsigned int rrate;
+	unsigned int rate, channels, period_size, buffer_size, rrate;
 	snd_pcm_uframes_t rperiod_size, rbuffer_size, start_threshold;
 	timestamp_t tstamp;
 	bool pass = false;
@@ -308,12 +307,15 @@ static void test_pcm_time(struct pcm_data *data, enum test_class class,
 		ksft_exit_fail_msg("Wrong format '%s'\n", cs);
 	conf_get_string_array(pcm_cfg, "alt_formats", NULL,
 				alt_formats, ARRAY_SIZE(alt_formats), NULL);
-	rate = conf_get_long(pcm_cfg, "rate", NULL, 48000);
-	channels = conf_get_long(pcm_cfg, "channels", NULL, 2);
-	period_size = conf_get_long(pcm_cfg, "period_size", NULL, 4096);
-	buffer_size = conf_get_long(pcm_cfg, "buffer_size", NULL, 16384);
-
-	samples = malloc((rate * channels * snd_pcm_format_physical_width(format)) / 8);
+	rate = (unsigned int)conf_get_long(pcm_cfg, "rate", NULL, 48000);
+	channels = (unsigned int)conf_get_long(pcm_cfg, "channels", NULL, 2);
+	period_size = (unsigned int)conf_get_long(pcm_cfg, "period_size", NULL, 4096);
+	buffer_size = (unsigned int)conf_get_long(pcm_cfg, "buffer_size", NULL, 16384);
+
+	phys_width = snd_pcm_format_physical_width(format);
+	if (phys_width < 0)
+		ksft_exit_fail_msg("Unknown PCM format\n");
+	samples = malloc((rate * channels * (unsigned int)phys_width) / 8);
 	if (!samples)
 		ksft_exit_fail_msg("Out of memory\n");
 	snd_pcm_format_set_silence(format, samples, rate * channels);
@@ -357,8 +359,11 @@ static void test_pcm_time(struct pcm_data *data, enum test_class class,
 						 snd_pcm_access_name(access),
 						 snd_pcm_format_name(old_format),
 						 snd_pcm_format_name(format));
+				phys_width = snd_pcm_format_physical_width(format);
+				if (phys_width < 0)
+					ksft_exit_fail_msg("Unknown PCM format\n");
 				samples = realloc(samples, (rate * channels *
-							    snd_pcm_format_physical_width(format)) / 8);
+								  (unsigned int)phys_width) / 8);
 				if (!samples)
 					ksft_exit_fail_msg("Out of memory\n");
 				snd_pcm_format_set_silence(format, samples, rate * channels);
@@ -371,29 +376,29 @@ static void test_pcm_time(struct pcm_data *data, enum test_class class,
 	}
 	err = snd_pcm_hw_params_set_channels(handle, hw_params, channels);
 	if (err < 0) {
-		snprintf(msg, sizeof(msg), "snd_pcm_hw_params_set_channels %ld: %s", channels, snd_strerror(err));
+		snprintf(msg, sizeof(msg), "snd_pcm_hw_params_set_channels %d: %s", channels, snd_strerror(err));
 		goto __close;
 	}
 	rrate = rate;
 	err = snd_pcm_hw_params_set_rate_near(handle, hw_params, &rrate, 0);
 	if (err < 0) {
-		snprintf(msg, sizeof(msg), "snd_pcm_hw_params_set_rate %ld: %s", rate, snd_strerror(err));
+		snprintf(msg, sizeof(msg), "snd_pcm_hw_params_set_rate %d: %s", rate, snd_strerror(err));
 		goto __close;
 	}
 	if (rrate != rate) {
-		snprintf(msg, sizeof(msg), "rate mismatch %ld != %ld", rate, rrate);
+		snprintf(msg, sizeof(msg), "rate mismatch %d != %d", rate, rrate);
 		goto __close;
 	}
 	rperiod_size = period_size;
 	err = snd_pcm_hw_params_set_period_size_near(handle, hw_params, &rperiod_size, 0);
 	if (err < 0) {
-		snprintf(msg, sizeof(msg), "snd_pcm_hw_params_set_period_size %ld: %s", period_size, snd_strerror(err));
+		snprintf(msg, sizeof(msg), "snd_pcm_hw_params_set_period_size %d: %s", period_size, snd_strerror(err));
 		goto __close;
 	}
 	rbuffer_size = buffer_size;
 	err = snd_pcm_hw_params_set_buffer_size_near(handle, hw_params, &rbuffer_size);
 	if (err < 0) {
-		snprintf(msg, sizeof(msg), "snd_pcm_hw_params_set_buffer_size %ld: %s", buffer_size, snd_strerror(err));
+		snprintf(msg, sizeof(msg), "snd_pcm_hw_params_set_buffer_size %d: %s", buffer_size, snd_strerror(err));
 		goto __close;
 	}
 	err = snd_pcm_hw_params(handle, hw_params);
@@ -428,14 +433,14 @@ static void test_pcm_time(struct pcm_data *data, enum test_class class,
 		goto __close;
 	}
 
-	ksft_print_msg("%s.%s.%d.%d.%d.%s hw_params.%s.%s.%ld.%ld.%ld.%ld sw_params.%ld\n",
+	ksft_print_msg("%s.%s.%d.%d.%d.%s hw_params.%s.%s.%d.%d.%d.%d sw_params.%ld\n",
 		         test_class_name, test_name,
 			 data->card, data->device, data->subdevice,
 			 snd_pcm_stream_name(data->stream),
 			 snd_pcm_access_name(access),
 			 snd_pcm_format_name(format),
-			 (long)rate, (long)channels,
-			 (long)rperiod_size, (long)rbuffer_size,
+			 rate, channels,
+			 rperiod_size, rbuffer_size,
 			 (long)start_threshold);
 
 	/* Set all the params, actually run the test */
-- 
2.34.1

_______________________________________________
Linux-kernel-mentees mailing list
Linux-kernel-mentees@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH v2] kselftest/alsa: Fix -Wformat compiler warnings
  2023-03-24 16:01 [PATCH v2] kselftest/alsa: Fix -Wformat compiler warnings Alexander Heinrich
@ 2023-03-24 16:33 ` Mark Brown
  0 siblings, 0 replies; 2+ messages in thread
From: Mark Brown @ 2023-03-24 16:33 UTC (permalink / raw)
  To: Alexander Heinrich
  Cc: tiwai, linux-kernel-mentees, shuah, linux-kselftest, perex


[-- Attachment #1.1: Type: text/plain, Size: 894 bytes --]

On Fri, Mar 24, 2023 at 05:01:58PM +0100, Alexander Heinrich wrote:

> For the string to int conversion functions, the return values for the test cases
> in pcm-test.conf are non-negative and in the range of unsigned int, so I just
> cast them to unsigned int.

That sounds like you're masking some issue rather than fixing it,
but...

> Please let me know if I can further improve on my patch (it is my first)!

It might be easier to split the different kinds of change up into
separate patches, it'd make it easier to follow what's going on.

> +				phys_width = snd_pcm_format_physical_width(format);
> +				if (phys_width < 0)
> +					ksft_exit_fail_msg("Unknown PCM format\n");
>  				samples = realloc(samples, (rate * channels *
> -							    snd_pcm_format_physical_width(format)) / 8);
> +								  (unsigned int)phys_width) / 8);

...this isn't a formatting function, it's realloc()?

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

[-- Attachment #2: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Linux-kernel-mentees mailing list
Linux-kernel-mentees@lists.linuxfoundation.org
https://lists.linuxfoundation.org/mailman/listinfo/linux-kernel-mentees

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-03-24 16:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-24 16:01 [PATCH v2] kselftest/alsa: Fix -Wformat compiler warnings Alexander Heinrich
2023-03-24 16:33 ` Mark Brown

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).