drm/i915: fix potential dangling else problems in for_each_ macros
authorJani Nikula <jani.nikula@intel.com>
Tue, 24 Nov 2015 19:21:56 +0000 (21:21 +0200)
committerDaniel Vetter <daniel.vetter@ffwll.ch>
Wed, 25 Nov 2015 08:29:32 +0000 (09:29 +0100)
commit95150bdf78f330788f97364702920ad0602f92f3
treee4466a356b70d9ef2a41e6cd8e78dc1427324ab6
parent373701b1fc7d7c0013ae4fffd8103615c150751e
drm/i915: fix potential dangling else problems in for_each_ macros

We have serious dangling else bugs waiting to happen in our for_each_
style macros with ifs. Consider, for example,

 #define for_each_power_domain(domain, mask)                         \
         for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
                 if ((1 << (domain)) & (mask))

If this is used in context:

if (condition)
for_each_power_domain(domain, mask);
else
foo();

foo() will be called for each domain *not* in mask, if condition holds,
and not at all if condition doesn't hold.

Fix this by reversing the conditions in the macros, and adding an else
branch for the "for each" block, so that other if/else blocks can't
interfere. Provide a "for_each_if" helper macro to make it easier to get
this right.

v2: move for_each_if to drmP.h in a separate patch.

Reviewed-by: Daniel Vetter <daniel.vetter@ffwll.ch>
Signed-off-by: Jani Nikula <jani.nikula@intel.com>
Link: http://patchwork.freedesktop.org/patch/msgid/1448392916-2281-2-git-send-email-jani.nikula@intel.com
Signed-off-by: Daniel Vetter <daniel.vetter@ffwll.ch>
drivers/gpu/drm/i915/i915_drv.h
drivers/gpu/drm/i915/intel_display.c
drivers/gpu/drm/i915/intel_dsi.h
drivers/gpu/drm/i915/intel_runtime_pm.c