Is using unsigned integer overflow good practice?

后端 未结 12 545
耶瑟儿~
耶瑟儿~ 2020-12-01 15:55

I was reading the C Standard the other day, and noticed that unlike signed integer overflow (which is undefined), unsigned integer overflow is well defined. I\'ve seen it us

12条回答
  •  感动是毒
    2020-12-01 16:46

    It's fine to rely on overflow as long as you know WHEN it will occur ...

    I, for example, had troubles with C implementation of MD5 when migrating to a more recent compiler... The code did expect overflow but it also expected 32 bits ints.

    With 64 bits the results were wrong !

    Fortunately that's what automated tests are for : I caught the problem early but this could have been a real horror story if gone unnoticed.

    You could argue "but this happens rarely" : yes but that's what makes it even more dangerous ! When there is a bug, everybody is suspicious of code written in the last few days. No one is suspicious f code that "just worked for years" and usually no one still knows how it works...

提交回复
热议问题