• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Where's the buffer overrun?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Where's the buffer overrun?


  • Subject: Where's the buffer overrun?
  • From: Nick Zitzmann <email@hidden>
  • Date: Wed, 19 Mar 2008 17:38:49 -0600

I'm probably missing something that's obvious but not so much to me right now...

I have an NSArray category method that takes an NSArray of NSString objects, and returns a C array of C strings (char * const *). This is so I can build a C array for some functions that require a C array.

But I'm running into a problem. With an NSArray containing two NSStrings, "-c" and "/path/to/file", Guard Malloc is complaining that there is a buffer overrun in my code. I know where it is, but what is causing this buffer overrun and what can I do to fix it? Unfortunately I can't switch to using GC at this time.

Here's my code:

- (char * const *)cArrayUsingEncoding:(NSStringEncoding)encoding
{
size_t length = ([self count]+1)*sizeof(char **);
char **returnArray = NSZoneMalloc([self zone], length);
NSUInteger i;
const NSUInteger count = [self count];

[NSMutableData dataWithBytesNoCopy:returnArray length:length]; // add returnArray to the autorelease pool
for (i = 0 ; i < count ; i++)
{
NSString *string = [self objectAtIndex:i];
NSMutableData *data;

NSAssert([string isKindOfClass:[NSString class]], @"Array must be 100% composed of strings");
data = [[[string dataUsingEncoding:encoding] mutableCopy] autorelease];
[data appendByte:'\0']; // null-terminate the string
returnArray[i] = [data mutableBytes]; <-- GUARD MALLOC CRASHES HERE; i IS ZERO
}
returnArray[count] = NULL; // null-terminate the array
return returnArray;
}


Nick Zitzmann
<http://www.chronosnet.com/>

_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Where's the buffer overrun?
      • From: Chris Suter <email@hidden>
  • Prev by Date: populate a custom sub-class with data from bindings
  • Next by Date: Re: Network communication with NSFileHandle & NSSocketPort
  • Previous by thread: populate a custom sub-class with data from bindings
  • Next by thread: Re: Where's the buffer overrun?
  • Index(es):
    • Date
    • Thread