News for hackers. Stuff that matters.
Re:The hijacking flaw that lurked in Intel chips is worse th... by geofft on Sunday May 07, 2017 @12:44PM (#14286257)
Yeah, but if you are writing that pseudocode, it's obvious that is_a_prefix_of is the wrong operation.

The problem is that strncmp is in fact a prefix-comparison function when used with n calculated one way, but also it's the "safer" version of strcmp when used with n calculated another way, and it's easy to confuse the two uses.

In any other string implementation, you'd just use the most obviously-named compare function, there would be no need for a "safer" version of that function (there'd just be one reasonable comparison function), and it would do the right thing.

Re:The hijacking flaw that lurked in Intel chips is worse th... by jjnoakes on Sunday May 07, 2017 @01:31PM (#14286464)
> also it's the "safer" version of strcmp when used with n calculated another way

This isn't true; first off it isn't "safer" than strcmp() at all (this isn't strcpy vs strncpy after all), and second, no length argument to strncmp() will make it act like strcmp(). In order to get strcmp(), you have to also check the lengths are the same first.

    strlen(a) == strlen(b) && strncmp(a, b, strlen(either))
This is less safe than using strcmp() when you want equality (as this Intel issue shows) because it is easy to forget the length check. strcmp() does it implicitly for you.

I agree the names are not ideal (in fact I write my own streq() and strpfx() routines because I think it reads easier and it is also easy to get the return value of strcmp() wrong), but again, that has nothing to do with separating the string's pointer and its length.

Re:The hijacking flaw that lurked in Intel chips is worse th... by geofft on Sunday May 07, 2017 @01:45PM (#14286526)
I agree with you, but please argue with the other person who objected to my comment saying that strncmp was obviously the safer version of strcmp. :-)

An API where experienced users don't even agree what the function is supposed to be is a bad API.

Re:The hijacking flaw that lurked in Intel chips is worse th... by jjnoakes on Sunday May 07, 2017 @09:14PM (#14288618)
How do you know all parties involved in this discussion are experienced?