All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alexander Heinrich <hallo@alexanderheinrich.de>
To: tiwai@suse.de
Cc: Alexander Heinrich <hallo@alexanderheinrich.de>,
	broonie@kernel.org, perex@perex.cz, shuah@kernel.org,
	linux-kselftest@vger.kernel.org,
	linux-kernel-mentees@lists.linuxfoundation.org
Subject: [PATCH v2] kselftest/alsa: Fix -Wformat compiler warnings
Date: Fri, 24 Mar 2023 17:01:58 +0100	[thread overview]
Message-ID: <20230324160158.4076-1-hallo@alexanderheinrich.de> (raw)

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


WARNING: multiple messages have this Message-ID (diff)
From: Alexander Heinrich <hallo@alexanderheinrich.de>
To: tiwai@suse.de
Cc: shuah@kernel.org, perex@perex.cz, broonie@kernel.org,
	linux-kselftest@vger.kernel.org,
	linux-kernel-mentees@lists.linuxfoundation.org
Subject: [PATCH v2] kselftest/alsa: Fix -Wformat compiler warnings
Date: Fri, 24 Mar 2023 17:01:58 +0100	[thread overview]
Message-ID: <20230324160158.4076-1-hallo@alexanderheinrich.de> (raw)

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

             reply	other threads:[~2023-03-24 16:12 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-24 16:01 Alexander Heinrich [this message]
2023-03-24 16:01 ` [PATCH v2] kselftest/alsa: Fix -Wformat compiler warnings Alexander Heinrich
2023-03-24 16:33 ` Mark Brown
2023-03-24 16:33   ` Mark Brown

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230324160158.4076-1-hallo@alexanderheinrich.de \
    --to=hallo@alexanderheinrich.de \
    --cc=broonie@kernel.org \
    --cc=linux-kernel-mentees@lists.linuxfoundation.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=perex@perex.cz \
    --cc=shuah@kernel.org \
    --cc=tiwai@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.