News for hackers. Stuff that matters.
Re:Try Objective-C by eridius on Thursday May 09, 2013 @08:26PM (#5683809)
Very cute. I did the first 9 challenges. I am rather worried though that you're teaching people that `NSLog(var)` is acceptable, because it's a horribly dangerous thing to do if var contains untrusted input.
Re:Try Objective-C by danenania on Thursday May 09, 2013 @09:24PM (#5684009)
A good solution to this is to use always use a debug-mode dependent macro for logging[1]:

#ifdef DEBUG

#define DLog(fmt, ...) NSLog((@"%s [Line %d] " fmt),__PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__)

#else

# define DLog(...)

#endif

Put that in your [Project]-Prefix.pch file to make it available globally. Aside from letting you log without fear, it gives you better performance in production, and you can get extra context for your output.

1: http://stackoverflow.com/questions/969130/how-to-print-out-t...

Re:Try Objective-C by rubymaverick on Thursday May 09, 2013 @11:01PM (#5684314)
Yup, I agree with you, but I wanted to introduce referencing a variable before introducing formatted strings and placeholders. We actually went back and forth on this a couple times.
Re:Try Objective-C by coldtea on Thursday May 09, 2013 @08:39PM (#5683845)
Huh? How so?
Re:Try Objective-C by eridius on Thursday May 09, 2013 @10:39PM (#5684232)
If the variable's value contains any format args, e.g. %s, then NSLog (or printf, or whatever string formatting function you're using) will look for another argument, which you didn't actually pass, so it starts looking at garbage stack values.

Besides causing incorrect output, this may crash your program, or potentially be a vector for an exploit.

Re:Try Objective-C by joshschreuder on Thursday May 09, 2013 @11:57PM (#5684528)
Is there any reason NSLog doesn't filter input to escape these format args?
Re:Try Objective-C by Someone on Friday May 10, 2013 @02:39AM (#5684934)
It can't. http://en.wikipedia.org/wiki/Uncontrolled_format_string:

"In particular, the varargs mechanism allows functions to accept any number of arguments (e.g. printf) by "popping" as many arguments off the call stack as they wish, trusting the early arguments to indicate how many additional arguments are to be popped, and of what types."

NSLog is similar; it has to trust its first argument to be consistent with the number and types of arguments actually passed to it.

If you call NSLog(x), but do not have complete control over the value of x, you cannot guarantee that.

Re:Try Objective-C by lmz on Friday May 10, 2013 @12:35AM (#5684655)
Because it's the format string, which the programmer is supposed to control?

NSLog(untrusted); bad. NSLog("The user entered %s", untrusted); OK.

Re:Try Objective-C by eridius on Friday May 10, 2013 @02:11AM (#5684870)
Because that would defeat the entire purpose of NSLog.
Re:Try Objective-C by jcromartie on Thursday May 09, 2013 @08:53PM (#5683901)