Skip to contentSkip to conversation

ALSA: kunit: Fix sparse warnings

PATCH In review Takashi Iwai 2 participants 2 messages Feb 27, 2024 – Feb 29, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ALSA: kunit: Fix sparse warnings

There were a few sparse warnings about the cast of strong-typed snd_pcm_format_t. Fix them with cast with __force.

For spreading the ugly mess, put them in the definitions WRONG_FORMAT_1 and WRONG_FORMAT_2 and use them in the callers.

Fixes 3e39acf56ede ("ALSA: core: Add sound core KUnit test")
---
 sound/core/sound_kunit.c | 29 +++++++++++++++--------------
 1 file changed, 15 insertions(+), 14 deletions(-)
sound/core/sound_kunit.c +15 -14
index 4212c4a20697..eb90f62228c0 100644
@@ -17,7 +17,8 @@
 	.name = #fmt,								\
 }
 
-#define WRONG_FORMAT (SNDRV_PCM_FORMAT_LAST + 1)
+#define WRONG_FORMAT_1 (__force snd_pcm_format_t)((__force int)SNDRV_PCM_FORMAT_LAST + 1)
+#define WRONG_FORMAT_2 (__force snd_pcm_format_t)-1
 
 #define VALID_NAME "ValidName"
 #define NAME_W_SPEC_CHARS "In%v@1id name"
@@ -104,8 +105,8 @@ static void test_phys_format_size(struct kunit *test)
 				valid_fmt[i].physical_bits);
 	}
 
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT), -EINVAL);
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(-1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT_1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT_2), -EINVAL);
 }
 
 static void test_format_width(struct kunit *test)
@@ -117,8 +118,8 @@ static void test_format_width(struct kunit *test)
 				valid_fmt[i].width);
 	}
 
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT), -EINVAL);
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(-1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_2), -EINVAL);
 }
 
 static void test_format_signed(struct kunit *test)
@@ -132,8 +133,8 @@ static void test_format_signed(struct kunit *test)
 				valid_fmt[i].sd < 0 ? -EINVAL : 1 - valid_fmt[i].sd);
 	}
 
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT), -EINVAL);
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(-1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_2), -EINVAL);
 }
 
 static void test_format_endianness(struct kunit *test)
@@ -147,10 +148,10 @@ static void test_format_endianness(struct kunit *test)
 				valid_fmt[i].le < 0 ? -EINVAL : 1 - valid_fmt[i].le);
 	}
 
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT), -EINVAL);
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(-1), -EINVAL);
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT), -EINVAL);
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(-1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT_1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT_2), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT_1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT_2), -EINVAL);
 }
 
 static void _test_fill_silence(struct kunit *test, struct snd_format_test_data *data,
@@ -177,7 +178,7 @@ static void test_format_fill_silence(struct kunit *test)
 			_test_fill_silence(test, &valid_fmt[j], buffer, buf_samples[i]);
 	}
 
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(WRONG_FORMAT, buffer, 20), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(WRONG_FORMAT_1, buffer, 20), -EINVAL);
 	KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(SNDRV_PCM_FORMAT_LAST, buffer, 0), 0);
 }
 
@@ -272,8 +273,8 @@ static void test_pcm_format_name(struct kunit *test)
 		KUNIT_EXPECT_STREQ(test, name, valid_fmt[i].name);
 	}
 
-	KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT), "Unknown");
-	KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(-1), "Unknown");
+	KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT_1), "Unknown");
+	KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT_2), "Unknown");
 }
 
 static void test_card_add_component(struct kunit *test)
Signature · 1 line
2.35.3
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 86 lines · Takashi Iwai

There were a few sparse warnings about the cast of strong-typed snd_pcm_format_t. Fix them with cast with __force.

For spreading the ugly mess, put them in the definitions WRONG_FORMAT_1 and WRONG_FORMAT_2 and use them in the callers.

Fixes 3e39acf56ede ("ALSA: core: Add sound core KUnit test")
---
  sound/core/sound_kunit.c | 29 +++++++++++++++--------------
  1 file changed, 15 insertions(+), 14 deletions(-)
sound/core/sound_kunit.c +13 -12
index 4212c4a20697..eb90f62228c0 100644
@@ -17,7 +17,8 @@
  	.name = #fmt,								\
  }
 
-#define WRONG_FORMAT (SNDRV_PCM_FORMAT_LAST + 1)
+#define WRONG_FORMAT_1 (__force snd_pcm_format_t)((__force int)SNDRV_PCM_FORMAT_LAST + 1)
+#define WRONG_FORMAT_2 (__force snd_pcm_format_t)-1
 
  #define VALID_NAME "ValidName"
  #define NAME_W_SPEC_CHARS "In%v@1id name"
@@ -104,8 +105,8 @@ static void test_phys_format_size(struct kunit *test)
  				valid_fmt[i].physical_bits);
  	}
 
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT), -EINVAL);
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(-1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT_1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT_2), -EINVAL);
  }
 
  static void test_format_width(struct kunit *test)
@@ -117,8 +118,8 @@ static void test_format_width(struct kunit *test)
  				valid_fmt[i].width);
  	}
 
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT), -EINVAL);
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(-1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_2), -EINVAL);
  }
 
  static void test_format_signed(struct kunit *test)
@@ -132,8 +133,8 @@ static void test_format_signed(struct kunit *test)
  				valid_fmt[i].sd < 0 ? -EINVAL : 1 - valid_fmt[i].sd);
  	}
 
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT), -EINVAL);
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(-1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT_2), -EINVAL);
  }
 
  static void test_format_endianness(struct kunit *test)
@@ -147,10 +148,10 @@ static void test_format_endianness(struct kunit *test)
  				valid_fmt[i].le < 0 ? -EINVAL : 1 - valid_fmt[i].le);
  	}
 
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT), -EINVAL);
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(-1), -EINVAL);
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT), -EINVAL);
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(-1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT_1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT_2), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT_1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT_2), -EINVAL);
  }
 
  static void _test_fill_silence(struct kunit *test, struct snd_format_test_data *data,
@@ -177,7 +178,7 @@ static void test_format_fill_silence(struct kunit *test)
  			_test_fill_silence(test, &valid_fmt[j], buffer, buf_samples[i]);
  	}
 
-	KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(WRONG_FORMAT, buffer, 20), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(WRONG_FORMAT_1, buffer, 20), -EINVAL);
  	KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(SNDRV_PCM_FORMAT_LAST, buffer, 0), 0);
  }
 

sound/core/sound_kunit.c static void test_pcm_format_name(struct kunit *test) · line 280

Quoted 10 lines
sound/core/sound_kunit.c +2 -2
@@ -272,8 +273,8 @@ static void test_pcm_format_name(struct kunit *test)
  		KUNIT_EXPECT_STREQ(test, name, valid_fmt[i].name);
  	}
 
-	KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT), "Unknown");
-	KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(-1), "Unknown");
+	KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT_1), "Unknown");
+	KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT_2), "Unknown");
  }
 
  static void test_card_add_component(struct kunit *test)
Signature · 2 lines
Kind regards,
Ivan Orlov