News for hackers. Stuff that matters.
Re:The Little C Function From Hell by ajross on Tuesday June 19, 2012 @11:23AM (#4132278)
"For something specific you need it" meaning ... a negative number, like an array or memory address offset? I mean, sure, I agree that you should be doing anything sensitive to 2's complement behavior on unsigned quantities. And if you know the values are positive-definite, unsigned has much cleaner behavior. And I'd even entertain the argument that unsigned should have been the default in the language spec and that signed quantities are the ones that should have specially declared types.

But... your advice as written is just insane. They are real, and required routinely. You can't just apply this as a "for dummies" rule without essentially ruling out half of the code they'll need to write.

Re:The Little C Function From Hell by raverbashing on Tuesday June 19, 2012 @12:08PM (#4132567)
". a negative number, like an array or memory address offset? "

A negative number yes, but not really a memory offset (you shouldn't mix negative numbers and memory offset, really)

But yeah, if you're doing "math" go for it, but it's on rare occasions where you need negative numbers (subtraction yes)

The most common case I remember may be sound samples, where you have signed chars.

For all other cases you would be using floating point or decimal numbers

Re:The Little C Function From Hell by Natsu on Tuesday June 19, 2012 @04:40PM (#4134033)
> For all other cases you would be using floating point or decimal numbers

If I'm trying to avoid mathematical anomalies, floating point is not what I would run to... "Equal" is a matter of degree, you have to be careful with anything near zero and you can't carelessly mix together numbers that are a few orders of magnitude different than each other.

Re:The Little C Function From Hell by raverbashing on Wednesday June 20, 2012 @02:57AM (#4135921)
Absolutely

But for most of "math" you would go for floating point. You won't reinvent some fixed point math using integers just because...

Re:The Little C Function From Hell by tedunangst on Tuesday June 19, 2012 @01:45PM (#4133101)
signed usually promotes to unsigned in nice ways, such that if you really want to store -1 in your unsigned, it will just work. I've found using all unsigned types, with the occasional signed value jammed into them, is less error prone than mixing and matching types throughout the program. ymmv, and of course, here be the odd dragon or two.
Re:The Little C Function From Hell by lawnchair_larry on Tuesday June 19, 2012 @09:16PM (#4135157)
My goodness, no, this is terrible advice. Never do this. Go fix all the code you wrote immediately. It is full of security vulnerabilities. I'm not kidding, this is so bad.
Re:The Little C Function From Hell by cperciva on Wednesday June 20, 2012 @04:09AM (#4136042)
I'm with the OpenBSD developer on this one.
Re:The Little C Function From Hell by lawnchair_larry on Thursday June 21, 2012 @04:50PM (#4144119)
I'm pretty confused. I didn't know who Ted was, but a quick google search shows he is an OpenBSD dev and worked for Coverity. Coverity itself will flag this error. Now you are backing up that position. Historically, this exact thing has been the cause of many security vulnerabilities. It's especially precarious with the POSIX API due to many functions returning negative for error. I recall OpenSSH making sweeping changes to rid the code of signed ints being used for error, for this reason.

Can you explain why you would advocate this? Am I misunderstanding you, or missing something?

I replied to the other comment in this thread with an openbsd vulnerability caused by doing what is being advocated (I did choose openbsd to be funny).

Re:The Little C Function From Hell by cperciva on Thursday June 21, 2012 @07:45PM (#4144707)
Looks to me like the vulnerability you linked to demonstrates the exact opposite of what you think it demonstrates: The problem is that an unsigned value (a length) was being stored in a signed integer type, allowing it to pass a validation check by masquerading as a negative value.
Re:The Little C Function From Hell by lawnchair_larry on Friday June 22, 2012 @09:12PM (#4149721)
Well, no, select() takes a signed value for the length (it is actually not a length, but the number of descriptors, later used to derive a length), and there is no changing that interface obviously. This is the source of the "negative value" in this example. The problem arises because internally, openbsd "jammed a negative value into an unsigned int", as Ted put it, and made it a very large positive value, leading to an overflow.
Re:The Little C Function From Hell by cperciva on Saturday June 23, 2012 @11:46PM (#4152532)
If the bounds check was performed after casting to unsigned, there would have been no problem. The vulnerability occurred because a bounds check was incorrectly performed on a signed value.
Re:The Little C Function From Hell by rwallace on Wednesday June 20, 2012 @04:39AM (#4136114)
Can you give some examples of why you think this is more prone to security vulnerabilities than using signed types?
Re:The Little C Function From Hell by lawnchair_larry on Thursday June 21, 2012 @03:46PM (#4143857)
Re:The Little C Function From Hell by rwallace on Friday June 22, 2012 @10:59AM (#4147006)
Ah, thanks. From reading the summary, it seems that case would have been prevented by using signed integers throughout, but would also have been prevented by using unsigned integers throughout?