Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / February 2008



Tip: Looking for answers? Try searching our database.

fprintf hangs

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
snnopy67 - 22 Feb 2008 21:14 GMT
For the life of me I can't see why the following code hangs in
fprintf():

- (bool) matchesPattern: (NSString *) file
{
    unsigned int npatterns = [skipPatterns_ count];
    unsigned int i;
    for ( i = 0; i < npatterns; i ++ )
    {
        const char * pattern = [[skipPatterns_ objectAtIndex: i]
cStringUsingEncoding: NSASCIIStringEncoding];
        if ( ! pattern )
            return FALSE;
        // 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 );
        if ( fnmatch( pattern, fn_buf, 0 /*FNM_LEADING_DIR*/ ) == 0 )
        {
            // NSLog(@"match %s - %@", pattern, file );
            return TRUE;
        }
    }
    return FALSE;
}

The function matchesPattern gets called many times without problems
(apparently).
And when it hangs in fprintf(), I can look at fn_buf in the debugger
and it is a well-formed C-string ...

Does anyone have any idea, why this might occasionally hang? Or what I
can try?

All hints and suggestions will be appreciated.

Best regards,
Gabriel.
Reinder Verlinde - 22 Feb 2008 21:27 GMT
In article
<3ca40801-9360-4e74-8761-f7c42313012c@e6g2000prf.googlegroups.com>,

> For the life of me I can't see why the following code hangs in
> fprintf():
[quoted text clipped - 11 lines]
> 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
snnopy67 - 22 Feb 2008 21:54 GMT
> That is not sufficient for safely making that call. Consider:
>
>     fprintf( stderr, "%s%d%6f");

Thanks a lot for your response!
Yes, you are right.
The fprintf() call was just meant for debugging.
The strings I printed were just path names and didn't contain any
"funny" characters.

Still, I changed it to
   fprintf(stderr, "%s\n", fn_buf );
but to no avail. I still hangs ... :-(

Best regards,
Gabriel.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.