News for hackers. Stuff that matters.
Re:Massacring C Pointers by oddity on Tuesday June 26, 2018 @02:06AM (#17398254)
From a purely safety-minded perspective, this function has a hidden bound of r[] past which the function becomes unsafe, but it does not check. Not only does it not check, the design of the function means that there is no possible way for it to check safely. s and t are both pointers to characters? How long are the strings they might represent supposed to be? Who knows? This code is incredibly reckless and it's dangerous to introduce students to such carelessly designed examples when first impressions will define how they program for a few years or so.

Ok, so it's reasonable to think that this is supposed to be a teaching example and that considering these concepts might be a bit too early in the process. This leads into the second problem and one that is more subjective: this code is incredibly dense and relies on enough quirks of C that it's almost never going to be clear to a beginner reader what they are supposed to take away from it. It's maybe useful as a quiz question on C syntax and semantics, but there are enough barriers to understanding what the code is supposed to do that the amount of explaining the text would need to do to describe what the code is doing is most likely prohibitively long. Instructive examples should be unambiguous in what they are trying to show, otherwise students will be confused and potentially conflate issues in a way that is difficult to untangle later.

Edit: Ha! I spent so long looking at the first half of the function I totally missed that it was returning r! So, not only does this code have minor issues here and there from its careless implementation, it has a fundamental flaw that, if it were to work, would do so only by accident. I can only imagine that a student might walk away from this example thinking that C functions can return arrays and possibly misunderstand scoping in C.

Re:Massacring C Pointers by saagarjha on Tuesday June 26, 2018 @02:15AM (#17398281)
I'm sorry, but I don't think you've quite gotten the reasons why it was lambasted:

> Not only does it not check, the design of the function means that there is no possible way for it to check safely. s and t are both pointers to characters? How long are the strings they might represent supposed to be?

strlen

> This leads into the second problem and one that is more subjective: this code is incredibly dense and relies on enough quirks of C that it's almost never going to be clear to a beginner reader what they are supposed to take away from it.

Most "nice" C functions are much terser than this.

Re:Massacring C Pointers by oddity on Tuesday June 26, 2018 @02:19AM (#17398296)
strlen is not safe and the design of this function does not permit it to ever be safe. That is my point.

Most "nice" C functions are also not used as teaching examples. There's a difference in how one writes C code for production use and instructive use.

Re:Massacring C Pointers by saagarjha on Tuesday June 26, 2018 @02:25AM (#17398318)
I take it you come from a higher level language, where null termination would seem risky. In C, however, strlen is considered safe (as opposed to say strcpy, strcat, etc. which do have "safe" replacements). As for terse examples being given to beginners, here's the example The C Programming Language gives for strcpy:

    void strcpy(char *s, char *t) {
    	while ((*s++ = *t++) != '\0') ;
    }
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)`.
Re:Massacring C Pointers by toopsss on Thursday June 28, 2018 @02:44PM (#17419414)
You haven’t read K&R C I’m going to guess. Terseness is a traditional component of C and UNIX (cf creat). I don’t care to debate the merits but that is a fact. Most C books in the 80s were written in this style. Furthermore in the days or micros code was generally terse for many and varied reasons. There were people typing code on membrane keyboards.