News for hackers. Stuff that matters.
Re:Re: C as used/implemented in practice by aidenn0 on Monday July 13, 2015 @08:00PM (#9881985)
Are you on a 64-bit target? Then a uint32_t gets converted to a 64-bit signed value before any arithmetic operator is applied. There are some real pitfalls here, though they are often exaggerated.

Do you turn off the TBAA in your compiler? In my experience most systems programmers either turn it off, or don't know the rules.

[edit] I forgot int is typically 32-bits on 64-bit targets. The same argument would still apply for uint16_t and smaller though.

Re:Re: C as used/implemented in practice by stephencanon on Monday July 13, 2015 @09:54PM (#9882385)
> uint32_t gets converted to a 64-bit signed value before any arithmetic operator is applied.

Only if int is > 32 bits on your platform, which is quite rare these days.

Re:Re: C as used/implemented in practice by dbaupp on Monday July 13, 2015 @08:13PM (#9882044)
Aren't numeric promotions only applied in mixed expressions? (However, I'm not a C language-lawyer, so I could very easily be incorrect.)
Re:Re: C as used/implemented in practice by aidenn0 on Monday July 13, 2015 @10:02PM (#9882417)
All 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.
Re:Re: C as used/implemented in practice by vtsrh on Monday July 13, 2015 @11:49PM (#9882775)
Well 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?
Re:Re: C as used/implemented in practice by lmm on Tuesday July 14, 2015 @05:57AM (#9883789)
The 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.
Re:Re: C as used/implemented in practice by vtsrh on Tuesday July 14, 2015 @09:21AM (#9884618)
Keep the old code, instead just start using it for new one.
Re:Re: C as used/implemented in practice by lmm on Tuesday July 14, 2015 @10:37AM (#9885173)
Most 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.
Re:Re: C as used/implemented in practice by vtsrh on Tuesday July 14, 2015 @11:10AM (#9885478)
Use it for new code. Libraries are written too.
Re:Re: C as used/implemented in practice by lmm on Tuesday July 14, 2015 @12:44PM (#9886228)
Libraries are usually older than the applications that use them. New libraries can be, and often are, written in new languages.
Re:Re: C as used/implemented in practice by vtsrh on Tuesday July 14, 2015 @01:22PM (#9886508)
https://news.ycombinator.com/item?id=9885478

Looks like you hit an infinite loop. Better luck next time.

Re:Re: C as used/implemented in practice by pcwalton on Monday July 13, 2015 @08:55PM (#9882206)
No, sadly. In some cases (floating point) what happens is even implementation-dependent (though queryable with FLT_EVAL_METHOD).
Re:Re: C as used/implemented in practice by cremno on Monday July 13, 2015 @09:03PM (#9882241)
What pitfalls do you mean (assuming 32-bit wide int and uint32_t is unsigned int as both are very common)?
Re:Re: C as used/implemented in practice by _kst_ on Monday July 13, 2015 @09:02PM (#9882239)
uint32_t is promoted to a signed 64-bit value only if int is 64 bits. Even on 64-bit systems, int is typically 32 bits.

Does TBAA mean type-based alias analysis?

https://en.wikipedia.org/wiki/Alias_analysis#Type-based_alia...

(EDIT: I keep forgetting HN doesn't support markdown.)