TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Why should we use a precise-width integer type?

2 点作者 mnem超过 5 年前

1 comment

wahern超过 5 年前
&gt; Actually, after working on this I got a report mentioning that my changes to move from imprecise-width to precise-width types fixed a security issue. This made me realize that more existing issues could still be fixed this way, and I started fixing other ones reported via ClusterFuzz after that.<p>The author found a security issue simply because the conversion required a careful code review. The security issue didn&#x27;t stem from using native integer types. In fact, I&#x27;d argue that the security issue stemmed from the original programmer making the kind of assumptions that are typical when you have the mindset of fixed-width types. To avoid multiplicative overflow they used an intermediate type that was twice the width. And in fact this aspect of it was correct. What was broken was their logic for detecting overflow of a second multiplication operation. Using fixed-width types wouldn&#x27;t have fixed that issue. The actual fix simply used base::CheckedNumeric as the type, which checks overflow in a library via operator overloading. The underlying type is irrelevant, though it&#x27;s not clear to me why they ended up choosing int32_t for the template type given that the function argument parameters are still unsigned int. That suggests some hidden dependencies and assumptions on callers that remain undeclared.<p>Nowhere did this author show an example of why switching from native types to fixed-width types improved anything. It&#x27;s unsurprising that a bug was discovered as a result of doing a careful, focused code review. If they had switched their focus from changing types to instead reviewing all multiplication operations, I bet they&#x27;d have found many more bugs. Also, what we don&#x27;t know is if the conversions introduced any issues as it could take years for those to shake out, if at all.