In article
,
snnopy67 wrote:
> For the life of me I can't see why the following code hangs in
> fprintf():
>
> // convert file name to c string, making sure it is plain ascii
> NSData * fn_data = [file dataUsingEncoding: NSASCIIStringEncoding
> allowLossyConversion: YES];
> char fn_buf[1000];
> unsigned int len = [fn_data length];
> len = (len < 999) ? len : 999;
> [fn_data getBytes: fn_buf length: len];
> fn_buf[len] = 0;
> fprintf(stderr, fn_buf );
>
> And when it hangs in fprintf(), I can look at fn_buf in the debugger
> and it is a well-formed C-string ...
That is not sufficient for safely making that call. Consider:
fprintf( stderr, "%s%d%6f");
The safe call is:
fprintf( stderr, "%s", fn_buf);
Using a non-constant string as second argument to fprintf (or first
argument to printf) almost never is a good idea. See also
<http://en.wikipedia.org/wiki/Format_string_attack>
Reinder
>> Stay informed about: fprintf hangs