Skip to content

Commit d570216

Browse files
spangtiwai
authored andcommitted
ALSA: Make snd_BUG_ON() always evaluate and return the conditional expression
Having snd_BUG_ON() only evaluate its conditional when CONFIG_SND_DEBUG is set leads to frequent bugs, since other similar macros in the kernel have different behavior. Let's make snd_BUG_ON() act like those macros so it will stop being accidentally misused. Signed-off-by: Christine Spang <christine.spang@oracle.com> Signed-off-by: Takashi Iwai <tiwai@suse.de>
1 parent 8ba955c commit d570216

2 files changed

Lines changed: 13 additions & 23 deletions

File tree

‎Documentation/DocBook/writing-an-alsa-driver.tmpl‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6164,14 +6164,12 @@ struct _snd_pcm_runtime {
61646164

61656165
<para>
61666166
The macro takes an conditional expression to evaluate.
6167-
When <constant>CONFIG_SND_DEBUG</constant>, is set, the
6168-
expression is actually evaluated. If it's non-zero, it shows
6169-
the warning message such as
6167+
When <constant>CONFIG_SND_DEBUG</constant>, is set, if the
6168+
expression is non-zero, it shows the warning message such as
61706169
<computeroutput>BUG? (xxx)</computeroutput>
6171-
normally followed by stack trace. It returns the evaluated
6172-
value.
6173-
When no <constant>CONFIG_SND_DEBUG</constant> is set, this
6174-
macro always returns zero.
6170+
normally followed by stack trace.
6171+
6172+
In both cases it returns the evaluated value.
61756173
</para>
61766174

61776175
</section>

‎include/sound/core.h‎

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -379,18 +379,10 @@ void __snd_printk(unsigned int level, const char *file, int line,
379379
* snd_BUG_ON - debugging check macro
380380
* @cond: condition to evaluate
381381
*
382-
* When CONFIG_SND_DEBUG is set, this macro evaluates the given condition,
383-
* and call WARN() and returns the value if it's non-zero.
384-
*
385-
* When CONFIG_SND_DEBUG is not set, this just returns zero, and the given
386-
* condition is ignored.
387-
*
388-
* NOTE: the argument won't be evaluated at all when CONFIG_SND_DEBUG=n.
389-
* Thus, don't put any statement that influences on the code behavior,
390-
* such as pre/post increment, to the argument of this macro.
391-
* If you want to evaluate and give a warning, use standard WARN_ON().
382+
* Has the same behavior as WARN_ON when CONFIG_SND_DEBUG is set,
383+
* otherwise just evaluates the conditional and returns the value.
392384
*/
393-
#define snd_BUG_ON(cond) WARN((cond), "BUG? (%s)\n", __stringify(cond))
385+
#define snd_BUG_ON(cond) WARN_ON((cond))
394386

395387
#else /* !CONFIG_SND_DEBUG */
396388

@@ -400,11 +392,11 @@ __printf(2, 3)
400392
static inline void _snd_printd(int level, const char *format, ...) {}
401393

402394
#define snd_BUG() do { } while (0)
403-
static inline int __snd_bug_on(int cond)
404-
{
405-
return 0;
406-
}
407-
#define snd_BUG_ON(cond) __snd_bug_on(0 && (cond)) /* always false */
395+
396+
#define snd_BUG_ON(condition) ({ \
397+
int __ret_warn_on = !!(condition); \
398+
unlikely(__ret_warn_on); \
399+
})
408400

409401
#endif /* CONFIG_SND_DEBUG */
410402

0 commit comments

Comments
 (0)