Welcome to MacForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

fprintf hangs

 
   Macintosh computer (Home) -> Programmer Help RSS
Next:  OS X 10.5 for Mac mini?  
Author Message
snnopy67

External


Since: Feb 22, 2008
Posts: 2



(Msg. 1) Posted: Fri Feb 22, 2008 1:14 pm
Post subject: fprintf hangs
Archived from groups: comp>sys>mac>programmer>help (more info?)

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.

 >> Stay informed about: fprintf hangs 
Back to top
Login to vote
snnopy67

External


Since: Feb 22, 2008
Posts: 2



(Msg. 2) Posted: Fri Feb 22, 2008 1:54 pm
Post subject: Re: fprintf hangs [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

>
> 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.

 >> Stay informed about: fprintf hangs 
Back to top
Login to vote
Reinder Verlinde

External


Since: Feb 18, 2004
Posts: 128



(Msg. 3) Posted: Fri Feb 22, 2008 8:57 pm
Post subject: Re: fprintf hangs [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

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 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Universal binary hangs on opposite architecture of build m.. - Here is a weird one. I am building a Universal Binary - when building it on my PPC G5 it runs fine on that machine but "hangs" on my Intel MacBook. If I build it on my MacBook it runs fine there but "hangs" on my G5. By "hang&...

memory leaking in 10.2.8? - Hi groups, I have a question concerning the memory management in the OS X (10.2.8 to be exact). Either I don't understand the way it works or there is something like major memory leak in the version I referred to (I haven't noticed it with previous..

Question about Bindings and Archiving - I'm referring to the tutorial "Enhanced Currency Converter" (from ADCHome > Documentation > Cocoa > Design Guidelines > Cocoa Bindings. I did it . Works great. Now I'm trying to make it save and open files (since it is based on an N...

Post encodeWithCoder stall? (archiving large graphs part 3) - In order to save large and tangled graphs without getting recursive unarchiving I decided to take the graph apart and archive it in the form of a series of linear arrays. What I have now are two linear arrays holding the elements of the graph. The..

Problem under Panther with NSView: dataWithEPSInsideRect a.. - Hi, our problem comes down to this: We do some painting in a custom view (lines and such) and also have some text (via NSFont -fontWithName and -drawAtPoint). The data is then pulled from the view with dataWithEPSInsideRect and written to an EPS file....
   Macintosh computer (Home) -> Programmer Help All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]