Review: adium 3497:ab9a06c5cc58: Bonjour bytes

Peter Hosey boredzo at adium.im
Tue Nov 9 06:18:46 UTC 2010


http://hg.adium.im/adium/rev/ab9a06c5cc58

> Patch from an anonymous source to use bytes instead of an NSString for this raw data, thereby silencing a Clang warning about null bytes in an NSString. The bytes are simply tacked onto an NSMutableData.

> diff -r da267665039b -r 0a9e33894575 Plugins/Bonjour/libezv/Private Classes/AWEzvRendezvousData.m
> --- a/Plugins/Bonjour/libezv/Private Classes/AWEzvRendezvousData.m	Mon Nov 08 20:52:06 2010 -0500
> +++ b/Plugins/Bonjour/libezv/Private Classes/AWEzvRendezvousData.m	Mon Nov 08 21:12:33 2010 -0600
> @@ -38,11 +38,11 @@
> 
> /* subnegotiation that appears at start of rendezvous packet */
> /*                             Reserved version? */
> -NSString	*subn = @"subn\x00\x00\x00\x01";
> +const char subn[] = "subn\x00\x00\x00\x01";
> 
> /* end of subnegotation. significance of value unknown */
> /*                        Reserved unknown       */
> -NSString	*endn = @"\x00\x00\x00\x00";
> +const char endn[] = "\x00\x00\x00\x00";
> 
> /* initialization, create our dictionary */
> -(AWEzvRendezvousData *) init 
> @@ -70,7 +70,7 @@
>     self = [self init];
> 
>     /* check that the length is ok */
> -    if ([data length] < ([subn length] + 4 + [endn length])) {
> +    if ([data length] < (sizeof(subn) + 4 + sizeof(endn))) {
> 	AWEzvLog(@"Invalid rendezvous announcement: length %u", [data length]);
> 		[self autorelease];
> 	return nil;
> @@ -100,7 +100,7 @@
>     fieldCount = ntohl(fieldCount);
> 
>     /* read fields from data */
> -    for (i = [subn length] + 4 + [endn length] + 4; i < [data length];) {
> +    for (i = sizeof(subn) + 4 + sizeof(endn) + 4; i < [data length];) {
> 	int binFlag = 0;
> 	
> 	/* read length of field name */
> @@ -361,9 +361,9 @@
>     data = [[NSMutableData alloc] init];
>     [data autorelease];
>     /* add the subnegotiation string */
> -    [data appendBytes:[subn UTF8String] length:[subn length]];
> +    [data appendBytes:subn length:sizeof(subn)];
>     [data appendBytes:&serialBE length:4];
> -    [data appendBytes:[endn UTF8String] length:[endn length]];
> +    [data appendBytes:endn length:sizeof(subn)];
>     /* add a field containing the number of fields for the rest of the data */
>     keycount = (UInt32)[keys count] + 1; /* +1 for slumming field */
>     keycount = htonl(keycount);


The output is now wrong.

Remember that C strings are null-terminated, and a C array (e.g., const char []) initialized with one will include the null terminator. Therefore, the C-array subn contains one more byte than the NSString subn contained characters. The same is true of endn.

There are two possible fixes:

1. Use strlen instead of sizeof.
2. Declare an enum symbol for the desired number of characters, and use each symbol both to declare the length of the corresponding array and to replace the sizeof expressions.




More information about the devel mailing list