Skip to content

Commit

Permalink
Negative random value where positive value is required
Browse files Browse the repository at this point in the history
The function MythRandom(), introduced in commit 31a811e,
does create a 32-bit random value which is returned in an uint32_t.
This new function does replace the Linux system call random() which returns an int.
Note that int value returned by random() is always positive.
In places where an int is required the output of MythRandom() is cast to an int.
This value can however be negative and will be approximately 50% of the times it is called.
This is now fixed in the places where it had not been fixed alreay.
  • Loading branch information
kmdewaal committed Jan 9, 2021
1 parent 5bc3e4a commit 181a1b8
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion mythtv/libs/libmythfreemheg/Programs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ void MHResidentProgram::CallProgram(bool fIsFork, const MHObjectRef &success, co
{
int nLimit = GetInt(args.GetAt(0), engine);
MHParameter *pResInt = args.GetAt(1);
int r = static_cast<int>(MythRandom()) % (nLimit + 1);
int r = static_cast<int>(MythRandom() % (nLimit + 1));
engine->FindObject(
*(pResInt->GetReference()))->SetVariableValue(r);
SetSuccessFlag(success, true, engine);
Expand Down
2 changes: 1 addition & 1 deletion mythtv/libs/libmythupnp/ssdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ bool SSDP::ProcessSearchRequest( const QStringMap &sHeaders,

nMX = (nMX > 120) ? 120 : nMX;

int nNewMX = (0 + (static_cast<int>(MythRandom()) % nMX)) * 1000;
int nNewMX = (0 + static_cast<int>(MythRandom() % nMX)) * 1000;

// ----------------------------------------------------------------------
// See what they are looking for...
Expand Down
2 changes: 1 addition & 1 deletion mythtv/programs/mythfrontend/playbackbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2254,7 +2254,7 @@ void PlaybackBox::playSelectedPlaylist(bool Random)
QList<uint> tmp = m_playList;
while (!tmp.isEmpty())
{
uint i = static_cast<int>(MythRandom()) % tmp.size();
uint i = MythRandom() % tmp.size();
m_playListPlay.append(tmp[i]);
tmp.removeAll(tmp[i]);
}
Expand Down
2 changes: 1 addition & 1 deletion mythtv/programs/mythfrontend/videodlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3197,7 +3197,7 @@ void VideoDialog::playVideoWithTrailers()
while (!trailers.isEmpty() && i < trailersToPlay)
{
++i;
QString trailer = trailers.takeAt(static_cast<int>(MythRandom()) % trailers.size());
QString trailer = trailers.takeAt(static_cast<int>(MythRandom() % trailers.size()));

LOG(VB_GENERAL, LOG_DEBUG,
QString("Random trailer to play will be: %1").arg(trailer));
Expand Down

2 comments on commit 181a1b8

@linuxdude42
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like it make sense to also limit MythRandom to the range from 0 to 2^31 - 1. That would match the behavior of the random() system call it replaced, and should prevent any similar problems in the future.

@kmdewaal
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would possibly break the code in other places where MythRandom is now used. I did check all usage of MythRandom and I think everything is OK now.

Please sign in to comment.