f2e-spec icon indicating copy to clipboard operation
f2e-spec copied to clipboard

Undefined behavior in Audio::playMusic

Open Lord-Kamina opened this issue 5 years ago • 6 comments

Around 2009 and 2010; when the fading system was implemented in several leaps and with the Audio system as a whole being heavily refactored at the same time, we went from if (fadeTime) > 0) m_streams.back()->fadein(fadeTime); (see 7db5142)

To m_streams.back()->fadein(fadeTime); in ab6978f.

Then in early 2010, 04c09a2 changed it to m.fadeRate = fadeTime / 48000.0f (which in practice is what getSR() would return.)

Until finally e9e9e09 changed it up and flipped the operands, Leaving us with m->fadeRate = 1.0 / getSR() / fadeTime;

Which, when called from ScreenSing::enter() (or whatever that is called) effectively means we're dividing by 0.0.

@Tronic I doubt this was intentional; can you explain why you flipped the operands back then and how do you think we should fix it?

Lord-Kamina avatar Apr 27 '20 19:04 Lord-Kamina

I think that m.fadeRate = fadeTime / 48000.0f was simply a bug fixed soon after. You need to divide by duration for correct results (i.e. slower rate with longer time).

Since this is floating-point division, it is not UB. Dividing by zero gets you +inf which actually works out just the way it is supposed to (you get fadeLevel = 1.0 on the first iteration).

Tronic avatar Apr 28 '20 06:04 Tronic

Division by zero is UB in language itself, it may just be optionally supported. (see https://stackoverflow.com/questions/42926763/the-behaviour-of-floating-point-division-by-zero ). That said, maybe platforms we are working on are all supporting it...

OznOg avatar May 03 '20 13:05 OznOg

The warning was removed in Clang 9 because they, like every damn compiler and CPU do support float division by zero as per Annex F. If this is a problem with an older Clang release, I suggest disabling the warning explicitly.

Tronic avatar May 04 '20 12:05 Tronic

ok, lets go with that :) Do we add a comment to say it is on purpose (for future devs?)

OznOg avatar May 04 '20 18:05 OznOg

Do we add a comment to say it is on purpose (for future devs?)

Yes please!

Baklap4 avatar May 08 '20 10:05 Baklap4

Todo

  • [ ] Add a comment to say this division by 0 is intentional.
  • [ ] Optional: Disable the warning explicitly (in line).

Baklap4 avatar Aug 15 '21 19:08 Baklap4