News for hackers. Stuff that matters.
Re:Type-Safe Printf for C by AlexanderDhoore on Tuesday December 14, 2021 @01:12PM (#29555068)
How is this safer than enabling all warnings in GCC or clang?

  'Type-safe' in this context does not mean that you get more compile errors, but that the format specifier does not need to specify the argument type, but just defines the print format. In fact, format strings with this library will have less compile-time checking (namely none) than with modern compilers for standard printf. This approach is still safer.
EDIT The answer is at the bottom apparently. Maybe put that up higher?
Re:Type-Safe Printf for C by edflsafoiewq on Tuesday December 14, 2021 @01:19PM (#29555158)
> There is absolutely no chance to give a wrong format specifier and access the stack (like printf does via stdarg.h) in undefined ways. This is particularly true for multi-arch development where with printf you need to be careful about length specifiers, and you might not get a warning on your machine, but the next person will and it will crash there. I usually need to compile for a few times on multiple architectures to get the integer length correct, e.g., %u vs %lu vs. %llu vs. %zu.
Re:Type-Safe Printf for C by kevin_thibedeau on Tuesday December 14, 2021 @03:25PM (#29557078)
This is really annoying on architectures like ARM32 where size_t is closely related to unsigned int but uint32_t is long unsigned int and gets flagged as a different type. It becomes a real problem when using a stripped down printf like the one in newlib that doesn't support %zu.
Re:Type-Safe Printf for C by tinkersleep on Tuesday December 14, 2021 @03:35PM (#29557232)
Exactly! Or on Windows 64-bit, where 'long' is 32-bit and 'size_t' is 'unsigned long long'.
Re:Type-Safe Printf for C by GeorgeTirebiter on Tuesday December 14, 2021 @04:44PM (#29558328)
The Real Problem (tm) is: specifiers like 'char' and 'int' etc should not be allowed; they 'should be' things like c8 or i16 or u64 --- that is, specify the #of bits for that dataype in the type specifier. This is what sys/stdint.h is trying to fix.

What maybe 'should' happen in C2x is: 'int' is defined as i16, 'long' as i32, 'long long' as i64 etc and then see which programs break. Because it's perfectly OK to have 16-bit 'ints' on a 64-bit arch. (size_t is what you use to deal with architecture-specific chunks). And then remove all this 'int' etc crap from C. (Obv, some 'compat switch' would need to exist, but you get the idea.)

Re:Type-Safe Printf for C by kevin_thibedeau on Tuesday December 14, 2021 @04:59PM (#29558549)
No that should not happen. Integer types that adapt to the platform word size enhance portability. Nobody wants a 32-bit default int on an 8-bit platform and using uint8_t or uint16_t can introduce performance regressions on wider platforms. The traditional integer types are perfectly suited for scenarios where the exact width doesn't matter and you know the guaranteed minimum is good enough.
Re:Type-Safe Printf for C by dmitrygr on Tuesday December 14, 2021 @10:43PM (#29561824)
To address the problem you mention, uint_fast8_t and co exist. It is at least 8 bits big, but whatever type is fastest that fits that requirement. So on an 8-bit system it is a uint8_t. on a 32-bit ARM is is a uint32_t. there is also uint_fast16_t and so on...
Re:Type-Safe Printf for C by GeorgeTirebiter on Thursday December 16, 2021 @03:26PM (#29583417)
As the programmer, I don't really care what the h/w does; if I specify u8 and every operation produces results 'as if' the type were 8 actual unsigned bits, eg, when using an underlying u32 type -- great. From 'my model' -- it's still a u8.

But there must be no conceptual 'leaking' (behaviors that I experience using e.g. uint_fast8_t that are in any way different from behaviors I experience when using u8 ).

I don't especially care how the HW works; because my 'virtual machine' is C. (yes, yes, I know Reality intrudes, and sometimes you need to get closer to the machine. But, isn't this because of 'leaking' between 'virtual machine' and 'actual machine' that I mention above?)

Re:Type-Safe Printf for C by arka2147483647 on Tuesday December 14, 2021 @05:57PM (#29559293)
I would argue that most code nowdays iplicitly assumes that int is 32bit’s long, and wont work correctly in a 8bit platform anyways. If ’platform size conforming’ ints are used, they probably should be opt-in, instead of opt-out.
Re:Type-Safe Printf for C by kevin_thibedeau on Tuesday December 14, 2021 @07:53PM (#29560507)
That's great for people weaned on Java's false promise of a uniform type system. Then you find out you want the behavior of unsigned integer overflow and have to jump through contortions to get it. You can't set a single standard for the default that works universally.
Re:Type-Safe Printf for C by kevin_thibedeau on Tuesday December 14, 2021 @04:55PM (#29558499)
For Windows the reason they couldn't switch to LP64 is because they screwed up the type system with LONG and allowed it to be incorporated into OS structs. That prevents long from being 64-bit for the sake of rationality.
Re:Type-Safe Printf for C by thebruce87m on Tuesday December 14, 2021 @05:56PM (#29559273)
Can’t you just use the inttypes.h along with stdint fixed width types to avoid the multiple compiles?

This stackoverflow answer gives an example: https://stackoverflow.com/questions/7597025/difference-betwe...

Re:Type-Safe Printf for C by edflsafoiewq on Tuesday December 14, 2021 @06:07PM (#29559400)
Of course if you just do everything correctly you don't need safety. But if you are fallible it is nice to have.
Re:Type-Safe Printf for C by tinkersleep on Tuesday December 14, 2021 @03:10PM (#29556841)
Ok, thanks for the hint. I put the most important infos into the intro: you just don't need 'll', 'l', 'z' modifiers for specifying sizeof(operand), as the compiler does that via _Generic.
Re:Type-Safe Printf for C by eps on Tuesday December 14, 2021 @04:45PM (#29558345)
Also put an example in the first pageful. I almost lost hope while scrolling through the wall of format spec when I finally saw the first example.
Re:Type-Safe Printf for C by tinkersleep on Wednesday December 15, 2021 @03:24AM (#29563321)
OK, yes, good idea.