Skip to content

Commit

Permalink
Cast 64->32 bit narrowing when building for 32bit system.
Browse files Browse the repository at this point in the history
Some compilers just print a warning for this, while others throw an
error and fail the compilation.
  • Loading branch information
linuxdude42 committed Jan 22, 2024
1 parent 7a26583 commit 859a5bf
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions mythtv/libs/libmythtv/recorders/dvbchannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1589,7 +1589,12 @@ bool DVBChannel::WaitForBackend(std::chrono::milliseconds timeout_ms)
const int fd = m_fdFrontend;
auto seconds = duration_cast<std::chrono::seconds>(timeout_ms);
auto usecs = duration_cast<std::chrono::microseconds>(timeout_ms) - seconds;
#if defined(__x86_64__)
struct timeval select_timeout = { seconds.count(), usecs.count()};
#else
struct timeval select_timeout = { static_cast<int32_t>(seconds.count()),
static_cast<int32_t>(usecs.count()) };
#endif
fd_set fd_select_set;
FD_ZERO( &fd_select_set); // NOLINT(readability-isolate-declaration)
FD_SET (fd, &fd_select_set);
Expand Down

3 comments on commit 859a5bf

@kmdewaal
Copy link
Contributor

Choose a reason for hiding this comment

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

I always like to avoid conditional compilation when possible....
If you write it as follows:

    struct timeval select_timeout = {
         static_cast<typeof(select_timeout.tv_sec)>(seconds.count()),
         static_cast<typeof(select_timeout.tv_usec)>(usecs.count())};

then I think it should work on both 64-bit and 32-bit systems.

@bennettpeter
Copy link
Member

Choose a reason for hiding this comment

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

I think that x86_64 may be too specific. Does this handle other 64 bit setups such as 64 bit android ?

@linuxdude42
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. I didn't realize that select_timeout would be instantiated when the compiler processed the static casts.

Please sign in to comment.