News for hackers. Stuff that matters.
Re:Massacring C Pointers by badrabbit on Tuesday June 26, 2018 @02:05AM (#17398252)
There are a few bugs in it. It returns something on the local functions stack (r) which gets torn down after return. It uses the insecure strcpy allowing the caller to stack overflow the destination. It increments t in the loop after dereferencing it which only changes the value pointed to by t. x and y are improperly initialized, it null terminates r without checking for x's value(again a buffer overflow) and there is no upper bound limit checking in the for loop(second for() parameter). I'm sure there are other bugs too but I haven't looked at it too closely.

But,yeah... It's a massacare.

Re:Massacring C Pointers by Figs on Tuesday June 26, 2018 @03:20AM (#17398542)
> It increments t in the loop after dereferencing it which only changes the value pointed to by t.

This is wrong. Postfix increment has a higher precedence than the dereference operator.

Re:Massacring C Pointers by badrabbit on Tuesday June 26, 2018 @02:40PM (#17403032)
Are you sure? I'll need to test this. Always thought postfix increment and decrement applied after everything else was evaluated,and that's why prefix versions exist.
Re:Massacring C Pointers by Figs on Tuesday June 26, 2018 @05:49PM (#17404265)
Yes. Here is a table that lists operator precedence in C: https://en.cppreference.com/w/c/language/operator_precedence

Note that postfix increment is at the highest precedence and the dereference operator is at the second highest precedence.

The postfix operators behave in a bit of an interesting way; the expression "t++" evaluates to just "t" but increments t as a side effect. Consequently, it appears to update t after the expression it is part of, even though it is one of the first things evaluated.

If you actually test the function, you will find that it works provided that you make r static or global instead of a local stack variable -- and, of course, carefully mind the restriction on the length of the input strings to avoid overflowing the buffer.

Re:Massacring C Pointers by badrabbit on Wednesday June 27, 2018 @12:47PM (#17409673)
Thank you! I learned something new.
Re:Massacring C Pointers by saagarjha on Tuesday June 26, 2018 @02:16AM (#17398285)
> x and y are improperly initialized

How so?

Re:Massacring C Pointers by badrabbit on Tuesday June 26, 2018 @02:25PM (#17402912)
I said that because I forgot the strlen was there. I was wrong.