News for hackers. Stuff that matters.
Re:Massacring C Pointers by ChrisSD on Tuesday June 26, 2018 @03:11AM (#17398513)
C11 introduced strnlen_s, a "safe" replacement to strlen.
Re:Massacring C Pointers by beefhash on Tuesday June 26, 2018 @01:04PM (#17402284)
strnlen_s is in Annex K, which is an optional part of the standard. Incidentally, no libc in widespread usage has implemented it.
Re:Massacring C Pointers by ChrisSD on Tuesday June 26, 2018 @02:07PM (#17402783)
True. The non standard strnlen is however more widespread. Microsoft implements strnlen_s in terms of strnlen like so.

    _String == 0 ? 0 : strnlen(_String, _MaxCount)
Re:Massacring C Pointers by colejohnson66 on Tuesday June 26, 2018 @09:47AM (#17400524)
What’s safer about it? Doesn’t `strlen' just scan until the null character, then go

    nullCharPtr - str
? Is there something unsafe with that?
Re:Massacring C Pointers by saagarjha on Tuesday June 26, 2018 @11:12AM (#17401161)
strnlen_s takes a parameter for the maximum number of characters to scan. This way, it won’t overflow the buffer you provide it if there’s no trailing null byte.
Re:Massacring C Pointers by ChrisSD on Tuesday June 26, 2018 @10:11AM (#17400708)
What if there is no null?
Re:Massacring C Pointers by slrz on Tuesday June 26, 2018 @05:25PM (#17404127)
Then you probably shouldn't feed it to the str* functions at all. Use the mem* functions instead.
Re:Massacring C Pointers by ChrisSD on Tuesday June 26, 2018 @05:55PM (#17404303)
That's the same as saying you should never use the str* functions. Nothing in C guarantees a string must have a null byte. As you're well aware, C-strings aren't a separate type that's distinct from char arrays.

Sure you can say that good programmers will always ensure a C-string is a C-string but there's decades of programming history that shows that's not true in practice.

Re:Massacring C Pointers by saagarjha on Wednesday June 27, 2018 @02:49AM (#17406223)
Then use a language other than C that doesn't shoehorn string handling into char , as I've mentioned in other comments. If you're have a char with no terminating null byte and you hand it to a function with "str" and its name, you're going to have a bad time.
Re:Massacring C Pointers by a1369209993 on Tuesday June 26, 2018 @04:10PM (#17403619)
If there isn't a NUL, then you're asking for a property relevantly distict from length of a datum relevantly distinct from a C-string. Use `strchr(s,0x00)` or `memchr(s,0x00,zs)`.