拿 unsigned integer 跟 0 做大小比較沒意義嗎?Linus Torvalds 不這麼認為…

有人要動 file system 底下的程式 fcntl.c,原因是要修掉一個 compiler 的 warning: pointless comparison of unsigned integer with zero。但有人說他以前就提出過,卻被 Linus 給回絕。於是這次提出的傢伙就不信邪地再 CC 去問 LinusXD

但是 Linus Torvalds 卻還是認為有留下檢查的必要,底下是他的回覆:

No, please don’t.

The warning is sometimes useful, but when it comes to a construct like

   if (a < 0 || a > X)

the fact that “a” is unsigned does not make the construct silly. First off, it’s (a) very readable and (b) the type of “a” may not be immediately obvious if it’s a user typedef, for example.

In fact, the type of “a” might depend on the architecture, or even compiler flags. Think about “char” – which may or may not be signed depending on ABI and things like -funsigned-char.

In other places, it’s not “unsigned” that is the problem, but the fact that the range of a type is smaller on one architecture than another. So you might have

   inf fn(pid_t a)
   {
      if (a > 0xffff)
      …
   }

which might warn on an architecture where “pid_t” is just sixteen bits wide. Does that make the code wrong? Hell no.

IOW, a lot of the gcc warnings are just not valid, and trying to shut gcc

It’s not even that I will drop the patches, it’s literally that “fixing” the code so that gcc doesn’t complain can be a BUG. We’ve gone through that.

Linus

原始討論串在此:http://kerneltrap.org/mailarchive/linux-fsdevel/2004/11/21/307540/thread 後頭還有一些其他人的意見,可以參考。

歷史上的今天

About mtlin

I'm easygoing and sometimes sentimental, also can be very funny. Geek style but social. A Blogger, a Wikipedian and an Engineer.
This entry was posted in Linux and tagged , , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.