Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linux/timerfd: stop explicitly casting to c_long for timespec.tv_nsec #189

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mjt0k
Copy link

@mjt0k mjt0k commented Apr 16, 2023

struct timespec.tv_nsec is implementation-specific (even if the doc says it is c_long). For example, on x32 it is i64. Do not explicitly cast nsec value to c_long, use whatever type needed for the target type. This also simplifies the code (avoiding temporary variable).

struct timespec.tv_nsec is implementation-specific (even if the
doc says it is c_long). For example, on x32 it is i64.  Do not
explicitly cast nsec value to c_long, use whatever type needed
for the target type.

Signed-off-by: Michael Tokarev <[email protected]>
// nsec always fits in i32 because subsec_nanos is defined to be less than one billion.
let nsec = dur.subsec_nanos() as i32;
spec.it_value.tv_nsec = libc::c_long::from(nsec);
spec.it_value.tv_nsec = dur.subsec_nanos() as _;
Copy link
Contributor

Choose a reason for hiding this comment

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

Very much personal preference but I find as _ to be quite unreadable.

Suggested change
spec.it_value.tv_nsec = dur.subsec_nanos() as _;
spec.it_value.tv_nsec = dur.subsec_nanos().try_into().unwrap();

// nsec always fits in i32 because subsec_nanos is defined to be less than one billion.
let nsec = int.subsec_nanos() as i32;
spec.it_interval.tv_nsec = libc::c_long::from(nsec);
spec.it_interval.tv_nsec = int.subsec_nanos() as _;
Copy link
Contributor

Choose a reason for hiding this comment

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

Duplicate of 7f7d0f1#r1168297816

Suggested change
spec.it_interval.tv_nsec = int.subsec_nanos() as _;
spec.it_interval.tv_nsec = int.subsec_nanos().try_into().unwrap();

Copy link
Author

Choose a reason for hiding this comment

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

Once again, this is 2nd day since I looked what rust is, to begin with, so I know nothing about the right constructs required here. I'm fine as long as it works. "My" version has been suggested by someone on IRC, - I didn't even know rust requires explicit type conversion between two different integer types.

FWIW, this issue happens on x32 only. On all the other platforms, timespec.tv_nsec is of long datatype. It is documented as being long. But on x32 it is int64_t. Apparently this is a bug in the architecture specifications. Also, x32 is a very rare thing.

Still, I like the code without explicit integer conversion more. It might also be a good idea to retain the comment which was previously there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants