summaryrefslogtreecommitdiff
path: root/libhw_cr/host_util.h
diff options
context:
space:
mode:
authorLuke T. Shumaker <lukeshu@lukeshu.com>2025-04-22 18:51:59 -0600
committerLuke T. Shumaker <lukeshu@lukeshu.com>2025-05-06 11:53:17 -0600
commit24e5d0ec1219e2dbb4b9510ef20833092a2b3871 (patch)
tree01bbcc34c6190fa1c35b2625e9ba1744b1447606 /libhw_cr/host_util.h
parentf09b7435b3a5222597d27238226d23ec0cbd5bd2 (diff)
wip: Build with -Wconversionlukeshu/safe-conversion
I think this found a real bug in the dhcp packet parser. I don't think anything called lib9p_str{,n}() values that could be big enough, but their bounds-checking was broken.
Diffstat (limited to 'libhw_cr/host_util.h')
-rw-r--r--libhw_cr/host_util.h18
1 files changed, 11 insertions, 7 deletions
diff --git a/libhw_cr/host_util.h b/libhw_cr/host_util.h
index 02c04dc..3d56e97 100644
--- a/libhw_cr/host_util.h
+++ b/libhw_cr/host_util.h
@@ -19,18 +19,22 @@ typedef struct timespec host_ns_time_t;
static inline host_us_time_t ns_to_host_us_time(uint64_t time_ns) {
host_us_time_t ret;
- ret.tv_sec = time_ns
- /NS_PER_S;
- ret.tv_usec = (time_ns - ((uint64_t)ret.tv_sec)*NS_PER_S)
- /(NS_PER_S/US_PER_S);
+ ret.tv_sec = LM_SAFEDOWNCAST(typeof(ret.tv_sec),
+ time_ns
+ /NS_PER_S);
+ ret.tv_usec = LM_SAFEDOWNCAST(typeof(ret.tv_usec),
+ (time_ns - ((uint64_t)ret.tv_sec)*NS_PER_S)
+ /(NS_PER_S/US_PER_S));
return ret;
}
static inline host_ns_time_t ns_to_host_ns_time(uint64_t time_ns) {
host_ns_time_t ret;
- ret.tv_sec = time_ns
- /NS_PER_S;
- ret.tv_nsec = time_ns - ((uint64_t)ret.tv_sec)*NS_PER_S;
+ ret.tv_sec = LM_SAFEDOWNCAST(typeof(ret.tv_sec),
+ time_ns
+ /NS_PER_S);
+ ret.tv_nsec = LM_SAFEDOWNCAST(typeof(ret.tv_nsec),
+ time_ns - ((uint64_t)ret.tv_sec)*NS_PER_S);
return ret;
}