5 ms·
Aren't numeric promotions only applied in mixed expressions? (However, I'm not a C language-lawyer, so I could very easily be incorrect.)
by dbaupp 11y ago
Aren't numeric promotions only applied in mixed expressions? (However, I'm not a C language-lawyer, so I could very easily be incorrect.)
- pcwalton 11y agoNo, sadly. In some cases (floating point) what happens is even implementation-dependent (though queryable with FLT_EVAL_METHOD).
- aidenn0 11y agoAll smaller than int types are promoted to int before calculations are done. I was wrong that uint32_t is smaller than int though. For an actual example: uint32_t combineTwoUint16(uint16_t x, uint16_t y) { return x<<16 | y; } If int is larger than 16 bits, then this is technically undefined behavior in the case that x is greater than 2^15 as it's signed integer overflow.
- vtsrh 11y agoWell the solution is simple: return (x+0U)<<16 | (y+0U); Would it be insane to have a inline library for every arithmetic operation, that would handle such cases and offer addition optional functionality?
- lmm 11y agoThe intersection of people who are willing to rewrite all their arithmetic to use such a library with people who are not willing to switch to a non-C language is rather small.
- vtsrh 11y agoKeep the old code, instead just start using it for new one.
- lmm 11y agoMost projects still written in C are those that make extensive use of C libraries. Making the application code immune doesn't actually reduce the vulnerability surface much - much of the vulnerability comes in the libraries the application calls.
- vtsrh 11y agoUse it for new code. Libraries are written too.
- lmm 11y agoLibraries are usually older than the applications that use them. New libraries can be, and often are, written in new languages.
- vtsrh 11y agohttps://news.ycombinator.com/item?id=9885478 https://news.ycombinator.com/item?id=9885478 Looks like you hit an infinite loop. Better luck next time.